Skip to content

Commit b86f893

Browse files
added new models
1 parent 995c31d commit b86f893

File tree

6 files changed

+254
-11
lines changed

6 files changed

+254
-11
lines changed

internal/models/annotation.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/lib/pq"
7+
"gorm.io/gorm"
8+
)
9+
10+
type Annotation struct {
11+
ID uint `json:"id" gorm:"primaryKey"`
12+
BookID uint `json:"book_id" gorm:"not null;foreignKey:BookID"`
13+
AuthorID uint `json:"author_id" gorm:"not null;foreignKey:AuthorID"`
14+
Quote string `json:"quote" gorm:"type:text;not null"`
15+
PageNumber *int `json:"page,omitempty"`
16+
Thoughts string `json:"thoughts,omitempty" gorm:"type:text"`
17+
IsPublic bool `json:"is_public" gorm:"default:true"`
18+
LikesCount int `json:"likes_count" gorm:"default:0"`
19+
Tags pq.StringArray `json:"tags" gorm:"type:text[]"`
20+
Book Book `json:"book" gorm:"foreignKey:BookID"`
21+
Author User `json:"author" gorm:"foreignKey:AuthorID"`
22+
23+
CreatedAt time.Time `json:"created_at"`
24+
UpdatedAt time.Time `json:"updated_at"`
25+
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
26+
}

internal/models/book.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
type Book struct {
10+
ID uint `json:"id" gorm:"primaryKey"`
11+
Title string `json:"title" gorm:"size:255;not null"`
12+
Author *string `json:"author,omitempty" gorm:"size:255"`
13+
CoverURL *string `json:"cover_url,omitempty" gorm:"type:text"`
14+
Genre *string `json:"genre,omitempty" gorm:"size:100"`
15+
Pages *int `json:"pages,omitempty"`
16+
PublishedYear *int `json:"published_year,omitempty"`
17+
ISBN *string `json:"isbn,omitempty" gorm:"size:20;uniqueIndex"`
18+
Description *string `json:"description,omitempty" gorm:"type:text"`
19+
Rating *float32 `json:"rating,omitempty" gorm:"type:decimal(2,1)"`
20+
21+
CreatedAt time.Time `json:"created_at"`
22+
UpdatedAt time.Time `json:"updated_at"`
23+
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
24+
}

internal/models/club.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package models
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
7+
"github.com/lib/pq"
8+
"gorm.io/gorm"
9+
)
10+
11+
type CurrentBook struct {
12+
Title string `json:"title"`
13+
Author *string `json:"author,omitempty"`
14+
CoverURL *string `json:"cover_url,omitempty"`
15+
BookID *uint `json:"book_id,omitempty"`
16+
Progress *int `json:"progress,omitempty"`
17+
}
18+
19+
type NextMeeting struct {
20+
Date *time.Time `json:"date,omitempty"`
21+
Location *string `json:"location,omitempty"`
22+
Topic *string `json:"topic,omitempty"`
23+
}
24+
25+
type ClubMembership struct {
26+
ID uint `json:"id" gorm:"primaryKey"`
27+
UserID uint `json:"user_id"`
28+
ClubID uint `json:"club_id"`
29+
Role string `json:"role" gorm:"default:'member'"`
30+
JoinedAt time.Time `json:"joined_at" gorm:"autoCreateTime"`
31+
User User `json:"user" gorm:"foreignKey:UserID"`
32+
}
33+
34+
type Club struct {
35+
ID uint `json:"id" gorm:"primaryKey"`
36+
Name string `json:"name" gorm:"size:100;not null;unique"`
37+
Description string `json:"description" gorm:"type:text"`
38+
Location *string `json:"location" gorm:"size:255"`
39+
Genre *string `json:"genre" gorm:"size:100"`
40+
CoverImageURL *string `json:"cover_image_url" gorm:"type:text"`
41+
IsPrivate bool `json:"is_private" gorm:"default:false"`
42+
MaxMembers int `json:"max_members" gorm:"default:100"`
43+
MembersCount int `json:"members_count" gorm:"default:0"`
44+
Rating float32 `json:"rating" gorm:"default:0"`
45+
Tags pq.StringArray `json:"tags" gorm:"type:text[]"`
46+
OwnerID uint `json:"owner_id"`
47+
CurrentBook json.RawMessage `json:"current_book" gorm:"type:jsonb"`
48+
NextMeeting json.RawMessage `json:"next_meeting" gorm:"type:jsonb"`
49+
Owner User `json:"owner" gorm:"foreignKey:OwnerID"`
50+
Moderators []User `json:"moderators" gorm:"many2many:club_moderators;"`
51+
Members []ClubMembership `json:"members" gorm:"foreignKey:ClubID"`
52+
Posts []Post `json:"posts,omitempty" gorm:"foreignKey:ClubID"`
53+
54+
CreatedAt time.Time `json:"created_at"`
55+
UpdatedAt time.Time `json:"updated_at"`
56+
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
57+
}
58+
59+
type CreateClubRequest struct {
60+
Name string `json:"name" validate:"required,min=3,max=100"`
61+
Description string `json:"description" validate:"max=1000"`
62+
Location *string `json:"location" validate:"omitempty,max=255"`
63+
Genre *string `json:"genre" validate:"omitempty,max=100"`
64+
CoverImageURL *string `json:"cover_image_url" validate:"omitempty,url"`
65+
IsPrivate bool `json:"is_private"`
66+
MaxMembers int `json:"max_members" validate:"gte=1,lte=1000"`
67+
Tags pq.StringArray `json:"tags" validate:"dive,max=50"`
68+
CurrentBook *CurrentBook `json:"current_book"`
69+
NextMeeting *NextMeeting `json:"next_meeting"`
70+
}
71+
72+
type UpdateClubRequest struct {
73+
Name *string `json:"name" validate:"omitempty,min=3,max=100"`
74+
Description *string `json:"description" validate:"omitempty,max=1000"`
75+
Location *string `json:"location" validate:"omitempty,max=255"`
76+
Genre *string `json:"genre" validate:"omitempty,max=100"`
77+
CoverImageURL *string `json:"cover_image_url" validate:"omitempty,url"`
78+
IsPrivate *bool `json:"is_private"`
79+
MaxMembers *int `json:"max_members" validate:"omitempty,gte=1,lte=1000"`
80+
Tags *pq.StringArray `json:"tags" validate:"omitempty,dive,max=50"`
81+
CurrentBook *CurrentBook `json:"current_book"`
82+
NextMeeting *NextMeeting `json:"next_meeting"`
83+
}

internal/models/comment.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
type Comment struct {
10+
ID uint `json:"id" gorm:"primaryKey"`
11+
PostID uint `json:"post_id" gorm:"not null; foreignKey:PostID"`
12+
AuthorID uint `json:"author_id" gorm:"not null;foreignKey:AuthorID"`
13+
Content string `json:"content" gorm:"type:text;not null"`
14+
LikesCount int `json:"likes_count" gorm:"default:0"`
15+
Author User `json:"author" gorm:"foreignKey:AuthorID"`
16+
Likes []CommentLike `json:"likes,omitempty" gorm:"foreignKey:CommentID"`
17+
18+
CreatedAt time.Time `json:"created_at"`
19+
UpdatedAt time.Time `json:"updated_at"`
20+
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
21+
}
22+
23+
type CommentLike struct {
24+
ID uint `json:"id" gorm:"primaryKey"`
25+
UserID uint `json:"user_id"`
26+
CommentID uint `json:"comment_id"`
27+
User User `json:"user" gorm:"foreignKey:UserID"`
28+
Comment Comment `json:"comment" gorm:"foreignKey:CommentID"`
29+
30+
CreatedAt time.Time `json:"created_at"`
31+
UpdatedAt time.Time `json:"updated_at"`
32+
}

internal/models/post.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
type Post struct {
10+
ID uint `json:"id" gorm:"primaryKey"`
11+
Title string `json:"title" gorm:"size:255;not null"`
12+
Content string `json:"content" gorm:"type:text;not null"`
13+
Type string `json:"type" gorm:"not null" validate:"required,oneof=discussion announcement event poll review" default:"discussion"`
14+
IsPinned bool `json:"is_pinned" gorm:"default:false"`
15+
LikesCount int `json:"likes_count" gorm:"default:0"`
16+
CommentsCount int `json:"comments_count" gorm:"default:0"`
17+
ViewsCount int `json:"views_count" gorm:"default:0"`
18+
AuthorID uint `json:"author_id"`
19+
ClubID uint `json:"club_id"`
20+
Author User `json:"author" gorm:"foreignKey:AuthorID"`
21+
Comments []Comment `json:"comments,omitempty" gorm:"foreignKey:PostID"`
22+
Likes []PostLike `json:"likes,omitempty" gorm:"foreignKey:PostID"`
23+
24+
CreatedAt time.Time `json:"created_at"`
25+
UpdatedAt time.Time `json:"updated_at"`
26+
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
27+
}
28+
29+
type PostLike struct {
30+
ID uint `json:"id" gorm:"primaryKey"`
31+
UserID uint `json:"user_id"`
32+
PostID uint `json:"post_id"`
33+
User User `json:"user" gorm:"foreignKey:UserID"`
34+
Post Post `json:"post" gorm:"foreignKey:PostID"`
35+
36+
CreatedAt time.Time `json:"created_at"`
37+
UpdatedAt time.Time `json:"updated_at"`
38+
}

internal/models/user.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@ package models
33
import (
44
"time"
55

6+
"github.com/lib/pq"
67
"gorm.io/gorm"
78
)
89

910
type User struct {
10-
ID uint `json:"id" gorm:"primaryKey"`
11-
Username string `json:"username" gorm:"uniqueIndex;not null" validate:"required,min=3,max=50"`
12-
Email string `json:"email" gorm:"uniqueIndex;not null" validate:"required,email"`
11+
ID uint `json:"id" gorm:"primaryKey"`
12+
Username string `json:"username" gorm:"uniqueIndex;not null" validate:"required,min=3,max=50"`
13+
Email string `json:"email" gorm:"uniqueIndex;not null" validate:"required,email"`
1314
PasswordHash string `json:"-" gorm:"not null"`
14-
FirstName string `json:"first_name" validate:"min=2,max=50"`
15-
LastName string `json:"last_name" validate:"min=2,max=50"`
16-
IsActive bool `json:"is_active" gorm:"default:true"`
17-
Role string `json:"role" validate:"required,oneof=admin user moderator support superuser" gorm:"default:'user'"`
18-
CreatedAt time.Time `json:"created_at"`
19-
UpdatedAt time.Time `json:"updated_at"`
15+
FirstName string `json:"first_name" validate:"min=2,max=50"`
16+
LastName string `json:"last_name" validate:"min=2,max=50"`
17+
IsActive bool `json:"is_active" gorm:"default:true"`
18+
Role string `json:"role" validate:"required,oneof=admin user moderator support superuser" gorm:"default:'user'"`
19+
20+
AvatarURL *string `json:"avatar_url" gorm:"type:text"`
21+
Location *string `json:"location" gorm:"size:255"`
22+
FavoriteGenres pq.StringArray `json:"favorite_genres" gorm:"type:text[]"`
23+
BooksRead int `json:"books_read" gorm:"default:0"`
24+
Bio *string `json:"bio" gorm:"type:text"`
25+
ReadingGoal int `json:"reading_goal" gorm:"default:0"`
26+
Badges pq.StringArray `json:"badges" gorm:"type:text[]"`
27+
IsOnline bool `json:"is_online" gorm:"default:false"`
28+
LastSeen *time.Time `json:"last_seen"`
29+
30+
OwnedClubs []Club `json:"owned_clubs,omitempty" gorm:"foreignKey:OwnerID"`
31+
ClubMemberships []ClubMembership `json:"club_memberships,omitempty" gorm:"foreignKey:UserID"`
32+
Posts []Post `json:"posts,omitempty" gorm:"foreignKey:AuthorID"`
33+
Comments []Comment `json:"comments,omitempty" gorm:"foreignKey:AuthorID"`
34+
Annotations []Annotation `json:"annotations,omitempty" gorm:"foreignKey:UserID"`
35+
PostLikes []PostLike `json:"post_likes,omitempty" gorm:"foreignKey:UserID"`
36+
CommentLikes []CommentLike `json:"comment_likes,omitempty" gorm:"foreignKey:UserID"`
37+
38+
CreatedAt time.Time `json:"created_at"`
39+
UpdatedAt time.Time `json:"updated_at"`
2040
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
2141
}
2242

@@ -27,7 +47,7 @@ type UserResponse struct {
2747
FirstName string `json:"first_name"`
2848
LastName string `json:"last_name"`
2949
IsActive bool `json:"is_active"`
30-
Role string `json:"role"`
50+
Role string `json:"role"`
3151
CreatedAt time.Time `json:"created_at"`
3252
}
3353

@@ -43,6 +63,26 @@ type RegisterRequest struct {
4363
FirstName string `json:"first_name" validate:"required,min=2,max=50"`
4464
LastName string `json:"last_name" validate:"required,min=2,max=50"`
4565
Role string `json:"role" validate:"omitempty,oneof=admin user moderator support superuser" gorm:"default:'user'"`
66+
67+
Location string `json:"location"`
68+
FavoriteGenres []string `json:"favorite_genres"`
69+
Bio string `json:"bio"`
70+
ReadingGoal int `json:"reading_goal"`
71+
}
72+
73+
type UpdateUserRequest struct {
74+
Username *string `json:"username" validate:"omitempty,min=3,max=50"`
75+
Email *string `json:"email" validate:"omitempty,email"`
76+
Password *string `json:"password" validate:"omitempty,min=6"`
77+
FirstName *string `json:"first_name" validate:"omitempty,min=2,max=50"`
78+
LastName *string `json:"last_name" validate:"omitempty,min=2,max=50"`
79+
AvatarURL *string `json:"avatar_url" validate:"omitempty,url"`
80+
Location *string `json:"location" validate:"omitempty,max=255"`
81+
FavoriteGenres *[]string `json:"favorite_genres"`
82+
Bio *string `json:"bio" validate:"omitempty"`
83+
ReadingGoal *int `json:"reading_goal" validate:"omitempty,gte=0"`
84+
Role *string `json:"role" validate:"omitempty,oneof=admin user moderator support superuser"`
85+
IsActive *bool `json:"is_active"`
4686
}
4787

4888
func (u *User) ToResponse() UserResponse {
@@ -56,4 +96,4 @@ func (u *User) ToResponse() UserResponse {
5696
Role: u.Role,
5797
CreatedAt: u.CreatedAt,
5898
}
59-
}
99+
}

0 commit comments

Comments
 (0)