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
|
2020-03-29 15:03:53 +00:00
|
|
|
func (s Service) Wire(server *aicra.Server) error {
|
2020-03-22 12:49:08 +00:00
|
|
|
if !s.DB.HasTable(&model.Article{}) {
|
|
|
|
s.DB.CreateTable(&model.Article{})
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
if err := server.Handle(http.MethodGet, "/articles", s.getAllArticles); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodGet, "/article/{id}", s.getArticleByID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodPost, "/article", s.postArticle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodDelete, "/article/{id}", s.deleteArticle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:24:35 +00:00
|
|
|
type articleList struct {
|
2020-03-29 15:03:53 +00:00
|
|
|
Articles []model.Article
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:24:35 +00:00
|
|
|
func (s Service) getArticlesByAuthor(param struct{ ID uint }) (*articleList, api.Error) {
|
2020-03-22 12:49:08 +00:00
|
|
|
articles := make([]model.Article, 0)
|
2020-03-29 15:03:53 +00:00
|
|
|
s.DB.Where("author = ?", param.ID).Find(&articles)
|
2020-03-29 15:24:35 +00:00
|
|
|
return &articleList{
|
2020-03-29 15:03:53 +00:00
|
|
|
Articles: articles,
|
|
|
|
}, api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:24:35 +00:00
|
|
|
func (s Service) getAllArticles() (*articleList, api.Error) {
|
2020-03-22 12:49:08 +00:00
|
|
|
articles := make([]model.Article, 0)
|
|
|
|
s.DB.Find(&articles)
|
2020-03-29 15:24:35 +00:00
|
|
|
return &articleList{
|
2020-03-29 15:03:53 +00:00
|
|
|
Articles: articles,
|
|
|
|
}, api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:30:36 +00:00
|
|
|
func (s Service) getArticleByID(param struct{ ID uint }) (*model.Article, api.Error) {
|
2020-03-22 12:49:08 +00:00
|
|
|
var article model.Article
|
2020-03-29 15:03:53 +00:00
|
|
|
if s.DB.First(&article, param.ID).RecordNotFound() {
|
|
|
|
return nil, api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:30:36 +00:00
|
|
|
return &article, api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:24:35 +00:00
|
|
|
type createRequest struct {
|
|
|
|
Title string
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:30:36 +00:00
|
|
|
func (s Service) postArticle(param createRequest) (*model.Article, api.Error) {
|
2020-03-29 15:03:53 +00:00
|
|
|
return nil, api.ErrorNotImplemented
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:24:35 +00:00
|
|
|
func (s Service) deleteArticle(param struct{ ID uint }) api.Error {
|
2020-03-29 15:03:53 +00:00
|
|
|
article := model.Article{
|
|
|
|
ID: param.ID,
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
s.DB.Delete(&article)
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|