11package main
22
33import (
4+ "encoding/json"
45 "fmt"
56 "gin-rest-api-example/database/models"
67 "gin-rest-api-example/repository"
@@ -15,19 +16,25 @@ func main() {
1516 if err != nil {
1617 log .Fatal (err )
1718 }
19+ db .LogMode (true )
1820 defer db .Close ()
1921
2022 //db.DropTable(&models.Follow{}, &models.User{})
2123 //db.AutoMigrate(&models.Follow{}, &models.User{})
2224 //testUsers(db)
2325
24- // db.DropTable(&models.Comment{}, &models.ArticleTag{}, &models.Tag{}, &models.ArticleFavorite{},
25- // &models.Article{}, &models.Follow{}, &models.User{})
26- // db.AutoMigrate(&models.Follow{}, &models.User{}, &models.Article{}, &models.ArticleFavorite{}, &models.Tag{},
27- // &models.ArticleTag{}, &models.Comment{})
26+ db .DropTable (& models.Comment {}, & models.ArticleTag {}, & models.Tag {}, & models.ArticleFavorite {},
27+ & models.Article {}, & models.Follow {}, & models.User {})
28+ db .AutoMigrate (& models.Follow {}, & models.User {}, & models.Article {}, & models.ArticleFavorite {}, & models.Tag {},
29+ & models.ArticleTag {}, & models.Comment {})
2830 testArticles (db )
2931}
3032
33+ // user : u1, u2
34+ // articles (title, tags, author, liked user)
35+ // - "title" / ["Tag1", "Tag2"] / u1 / u2
36+ // - "title2" / ["Tag1", "Tag3"] / u1 / u2
37+ // - "title3" / ["Tag3", "Tag4"] / u2 / u1
3138func testArticles (db * gorm.DB ) {
3239 userRepo := repository .NewUserRepository (db )
3340 articleRepo := repository .NewArticleRepository (db )
@@ -42,44 +49,85 @@ func testArticles(db *gorm.DB) {
4249 }
4350 _ = userRepo .Save (u1 )
4451
52+ fmt .Println ("Try to save user2" )
53+ u2 := & models.User {
54+ 55+ Username : "user2" ,
56+ Password : "user2" ,
57+ Bio : "user2 bio" ,
58+ Image : "user2 image" ,
59+ }
60+ _ = userRepo .Save (u2 )
61+
4562 fmt .Println ("Try to save article1" )
4663 a := & models.Article {
4764 Title : "title" ,
4865 Description : "description" ,
4966 Body : "body" ,
5067 Author : * u1 ,
5168 AuthorID : u1 .ID ,
52- Tags : []models.Tag {
69+ Tags : []models.Tag {
5370 {
54- Name : "Tag1" ,
71+ Name : "Tag1" ,
5572 },
5673 {
57- Name : "Tag2" ,
74+ Name : "Tag2" ,
5875 },
5976 },
60- Comment : nil ,
77+ Comment : nil ,
6178 }
6279 a .UpdateSlug ()
6380 _ = articleRepo .SaveArticle (a )
81+ articleRepo .UpdateFavorite (& models.ArticleFavorite {
82+ UserID : u2 .ID ,
83+ ArticleID : a .ID ,
84+ })
6485
65- fmt .Println ("Try to save article1 " )
86+ fmt .Println ("Try to save article2 " )
6687 a2 := & models.Article {
6788 Title : "title2" ,
6889 Description : "description2" ,
6990 Body : "body2" ,
7091 AuthorID : u1 .ID ,
71- Tags : []models.Tag {
92+ Tags : []models.Tag {
7293 {
73- Name : "Tag1" ,
94+ Name : "Tag1" ,
7495 },
7596 {
76- Name : "Tag3" ,
97+ Name : "Tag3" ,
7798 },
7899 },
79- Comment : nil ,
100+ Comment : nil ,
80101 }
81102 a2 .UpdateSlug ()
82103 _ = articleRepo .SaveArticle (a2 )
104+ articleRepo .UpdateFavorite (& models.ArticleFavorite {
105+ UserID : u2 .ID ,
106+ ArticleID : a2 .ID ,
107+ })
108+
109+ fmt .Println ("Try to save article3" )
110+ a3 := & models.Article {
111+ Title : "title3" ,
112+ Description : "description3" ,
113+ Body : "body3" ,
114+ AuthorID : u2 .ID ,
115+ Tags : []models.Tag {
116+ {
117+ Name : "Tag3" ,
118+ },
119+ {
120+ Name : "Tag4" ,
121+ },
122+ },
123+ Comment : nil ,
124+ }
125+ a3 .UpdateSlug ()
126+ _ = articleRepo .SaveArticle (a3 )
127+ _ = articleRepo .UpdateFavorite (& models.ArticleFavorite {
128+ UserID : u1 .ID ,
129+ ArticleID : a3 .ID ,
130+ })
83131
84132 fmt .Println ("Try to save comment1" )
85133 c := & models.Comment {
@@ -88,6 +136,55 @@ func testArticles(db *gorm.DB) {
88136 AuthorID : u1 .ID ,
89137 }
90138 _ = articleRepo .SaveOne (c )
139+
140+ fmt .Println (">>>> FindArticleBySlug" )
141+ find , _ := articleRepo .FindArticleBySlug (a .Slug )
142+ b , _ := json .Marshal (find )
143+ fmt .Println (string (b ))
144+
145+ fmt .Println (">>>> FindArticles" )
146+ articles , count , _ := articleRepo .FindArticles (repository.Pageable {
147+ Offset : 0 ,
148+ Limit : 1 ,
149+ })
150+ fmt .Println ("total count :" , count )
151+ for _ , a := range articles {
152+ b , _ := json .Marshal (a )
153+ fmt .Println (string (b ))
154+ }
155+
156+ fmt .Println (">>>> FindArticles by tag : Tag1" )
157+ articles , count , _ = articleRepo .FindArticlesByTag ("Tag1" , repository.Pageable {
158+ Offset : 0 ,
159+ Limit : 1 ,
160+ })
161+ fmt .Println ("total count :" , count )
162+ for _ , a := range articles {
163+ b , _ := json .Marshal (a )
164+ fmt .Println (string (b ))
165+ }
166+
167+ fmt .Println (">>>> FindArticles by author : user1" )
168+ articles , count , _ = articleRepo .FindArticlesByAuthor ("user1" , repository.Pageable {
169+ Offset : 0 ,
170+ Limit : 1 ,
171+ })
172+ fmt .Println ("total count :" , count )
173+ for _ , a := range articles {
174+ b , _ := json .Marshal (a )
175+ fmt .Println (string (b ))
176+ }
177+
178+ fmt .Println (">>>> FindArticles by favorited by" , u2 .Username )
179+ articles , count , _ = articleRepo .FindArticlesByFavoritedUsername (u2 .Username , repository.Pageable {
180+ Offset : 0 ,
181+ Limit : 1 ,
182+ })
183+ fmt .Println ("total count :" , count )
184+ for _ , a := range articles {
185+ b , _ := json .Marshal (a )
186+ fmt .Println (string (b ))
187+ }
91188}
92189
93190func testUsers (db * gorm.DB ) {
0 commit comments