feat: user & article: dev complete

This commit is contained in:
fallen-angle
2022-02-27 16:36:33 +08:00
parent 4f3b16ab9d
commit 80ca1cd46e
33 changed files with 2373 additions and 185 deletions

View File

@@ -1,37 +1,49 @@
package article
import (
"nCovTrack-Backend/global"
"nCovTrack-Backend/models"
"nCovTrack-Backend/utils"
)
func GetArticleList() *[]models.BackArticle {
var articles []models.BackArticle
global.Db.Omit("content").Find(&articles)
return &articles
//ListPublishedArticles list the articles published, use to show the articles to all people
func ListPublishedArticles() *[]map[string]interface{} {
article := models.ListField[models.BackArticle]([]map[string]interface{}{{"is_publish": 0}}, true, "content")
if *article == nil {
article = &[]map[string]interface{}{}
}
return article
}
func SaveArticle(article *models.BackArticle) (ok bool) {
return utils.Upsert(article)
//ListAllArticles list all articles, will show the articles not published of the user
// TODO: need only show the user's not published article
func ListAllArticles() *[]map[string]interface{} {
article := models.ListField[models.BackArticle]([]map[string]interface{}{{}}, true, "content")
if *article == nil {
article = &[]map[string]interface{}{}
}
return article
}
//SaveArticle save the articles
func SaveArticle(article map[string]interface{}) (ok bool) {
models.BeforeSave(article, -1)
ok, rows := models.Upsert[models.BackArticle](article)
return ok && rows != 0
}
//DeleteArticle delete article by id
func DeleteArticle(id int) (ok bool) {
tx := global.Db.Delete(&models.BackArticle{}, id)
if tx.Error != nil {
panic(tx.Error)
}
if tx.RowsAffected == 0 {
return false
}
return false
ok, rowsAffected := models.DeleteById[models.BackArticle](id)
return ok && rowsAffected != 0
}
func GetArticleById(id int) *models.BackArticle {
var article models.BackArticle
tx := global.Db.Limit(1).Find(&article, id)
if tx.RowsAffected == 0 {
return nil
}
return &article
//GetArticleById get an article
func GetArticleById(id int) map[string]interface{} {
return models.Get[models.BackArticle]([]map[string]interface{}{{"id": id}})
}
//PublishArticle publish an article
func PublishArticle(id int) (ok bool) {
colMap := map[string]interface{}{"id": id, "is_publish": 1}
ok, rowsAffected := models.Upsert[models.BackArticle](colMap)
return ok && rowsAffected != 0
}