aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/model
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/web/model')
-rw-r--r--cmd/web/model/article.go52
-rw-r--r--cmd/web/model/comment.go17
-rw-r--r--cmd/web/model/tag.go9
-rw-r--r--cmd/web/model/user.go59
4 files changed, 137 insertions, 0 deletions
diff --git a/cmd/web/model/article.go b/cmd/web/model/article.go
new file mode 100644
index 0000000..1475a0c
--- /dev/null
+++ b/cmd/web/model/article.go
@@ -0,0 +1,52 @@
+package model
+
+import (
+ "gorm.io/gorm"
+)
+
+type Article struct {
+ gorm.Model
+ Slug string `gorm:"uniqueIndex;not null"`
+ Title string `gorm:"not null" validate:"required"`
+ Description string `validate:"required"`
+ Body string `validate:"required"`
+ User User `validate:"-"`
+ UserID uint
+ Comments []Comment
+ Favorites []User `gorm:"many2many:article_favorite;"`
+ Tags []Tag `gorm:"many2many:article_tag;"`
+ IsFavorited bool `gorm:"-"`
+}
+
+func (Article Article) GetFormattedCreatedAt() string {
+ dateLayout := "Jan 02, 2006"
+ return Article.CreatedAt.Format(dateLayout)
+}
+
+func (Article Article) GetFavoriteCount() int {
+ return len(Article.Favorites)
+}
+
+func (Article Article) FavoritedBy(id uint) bool {
+ if Article.Favorites == nil {
+ return false
+ }
+
+ for _, u := range Article.Favorites {
+ if u.ID == id {
+ return true
+ }
+ }
+
+ return false
+}
+
+func (Article Article) GetTagsAsCommaSeparated() string {
+ tagsText := ""
+
+ for i := 0; i < len(Article.Tags); i++ {
+ tagsText += Article.Tags[i].Name + ","
+ }
+
+ return tagsText
+}
diff --git a/cmd/web/model/comment.go b/cmd/web/model/comment.go
new file mode 100644
index 0000000..c979edc
--- /dev/null
+++ b/cmd/web/model/comment.go
@@ -0,0 +1,17 @@
+package model
+
+import "gorm.io/gorm"
+
+type Comment struct {
+ gorm.Model
+ Article Article `validate:"-"`
+ ArticleID uint
+ User User `validate:"-"`
+ UserID uint
+ Body string `validate:"required"`
+}
+
+func (Comment Comment) GetFormattedCreatedAt() string {
+ dateLayout := "Jan 02, 2006"
+ return Comment.CreatedAt.Format(dateLayout)
+}
diff --git a/cmd/web/model/tag.go b/cmd/web/model/tag.go
new file mode 100644
index 0000000..db38a07
--- /dev/null
+++ b/cmd/web/model/tag.go
@@ -0,0 +1,9 @@
+package model
+
+import "gorm.io/gorm"
+
+type Tag struct {
+ gorm.Model
+ Name string `gorm:"uniqueIndex"`
+ Articles []Article `gorm:"many2many:article_tag;"`
+}
diff --git a/cmd/web/model/user.go b/cmd/web/model/user.go
new file mode 100644
index 0000000..6a5a388
--- /dev/null
+++ b/cmd/web/model/user.go
@@ -0,0 +1,59 @@
+package model
+
+import (
+ "golang.org/x/crypto/bcrypt"
+ "gorm.io/gorm"
+)
+
+type User struct {
+ gorm.Model
+ ID uint
+ Name string `validate:"required"`
+ Username string `gorm:"uniqueIndex;not nul"`
+ Email string `gorm:"uniqueIndex;not null" validate:"required,email"`
+ Password string `gorm:"not null"`
+ Bio string
+ Image string
+ Followers []Follow `gorm:"foreignKey:FollowerID"`
+ Followings []Follow `gorm:"foreignKey:FollowingID"`
+ Favorites []Article `gorm:"many2many:article_favorite;"`
+}
+
+type Follow struct {
+ Follower User
+ FollowerID uint `gorm:"column:user_id;primaryKey" sql:"type:int not null"`
+ Following User
+ FollowingID uint `gorm:"column:follower_id;primaryKey" sql:"type:int not null"`
+}
+
+func (u *User) HashPassword() {
+ h, _ := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
+ u.Password = string(h)
+}
+
+func (u *User) CheckPassword(plain string) bool {
+ err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(plain))
+ return err == nil
+}
+
+func (u User) FollowedBy(id uint) bool {
+ if u.Followers == nil {
+ return false
+ }
+
+ for _, f := range u.Followers {
+ if f.FollowingID == id {
+ return true
+ }
+ }
+
+ return false
+}
+
+func (u User) FollowersCount() int {
+ return len(u.Followers)
+}
+
+func (Follow) TableName() string {
+ return "user_follower"
+}