2020-03-22 12:49:08 +00:00
|
|
|
package article
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.xdrm.io/go/aicra"
|
|
|
|
"git.xdrm.io/go/aicra/api"
|
|
|
|
"git.xdrm.io/go/tiny-url-ex/service/model"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service to manage users
|
|
|
|
type Service struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wire services to their paths
|
|
|
|
func (s Service) Wire(server *aicra.Server) {
|
|
|
|
if !s.DB.HasTable(&model.Article{}) {
|
|
|
|
s.DB.CreateTable(&model.Article{})
|
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
server.Handle(http.MethodGet, "/articles", s.getAllArticles)
|
|
|
|
server.Handle(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor)
|
|
|
|
server.Handle(http.MethodGet, "/article/{id}", s.getArticleByID)
|
|
|
|
server.Handle(http.MethodPost, "/article", s.postArticle)
|
|
|
|
server.Handle(http.MethodDelete, "/article/{id}", s.deleteArticle)
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) getArticlesByAuthor(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("author_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
articles := make([]model.Article, 0)
|
|
|
|
s.DB.Where("author = ?", id).Find(&articles)
|
|
|
|
res.SetData("articles", articles)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) getAllArticles(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
articles := make([]model.Article, 0)
|
|
|
|
s.DB.Find(&articles)
|
|
|
|
res.SetData("articles", articles)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) getArticleByID(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("article_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var article model.Article
|
|
|
|
if s.DB.First(&article, id).RecordNotFound() {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.SetData("id", article.ID)
|
|
|
|
res.SetData("title", article.Title)
|
|
|
|
res.SetData("body", article.Body)
|
|
|
|
res.SetData("author", article.Author)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) postArticle(req api.Request, res *api.Response) api.Error {
|
|
|
|
return api.ErrorNotImplemented
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) deleteArticle(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("user_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
article := model.Article{}
|
|
|
|
article.ID = id
|
|
|
|
s.DB.Delete(&article)
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|