aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/model/article.go
diff options
context:
space:
mode:
authorVikas Kushwaha <dev@vikas.rocks>2025-02-11 16:31:08 +0530
committerVikas Kushwaha <dev@vikas.rocks>2025-02-11 16:31:08 +0530
commit57eb8f6712361a3bf75983ce153fac4846dc0273 (patch)
tree269a168d59c917c4e313c819e2b4c3ff8175f912 /cmd/web/model/article.go
Initial commit
Diffstat (limited to 'cmd/web/model/article.go')
-rw-r--r--cmd/web/model/article.go52
1 files changed, 52 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
+}