update according to new api changes

This commit is contained in:
Adrien Marquès 2020-03-28 15:02:42 +01:00
parent fca88ccf82
commit 473e46ee19
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 44 additions and 58 deletions

View File

@ -20,66 +20,62 @@ func (s Service) Wire(server *aicra.Server) {
s.DB.CreateTable(&model.Article{}) s.DB.CreateTable(&model.Article{})
} }
server.HandleFunc(http.MethodGet, "/articles", s.getAllArticles) server.Handle(http.MethodGet, "/articles", s.getAllArticles)
server.HandleFunc(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor) server.Handle(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor)
server.HandleFunc(http.MethodGet, "/article/{id}", s.getArticleByID) server.Handle(http.MethodGet, "/article/{id}", s.getArticleByID)
server.HandleFunc(http.MethodPost, "/article", s.postArticle) server.Handle(http.MethodPost, "/article", s.postArticle)
server.HandleFunc(http.MethodDelete, "/article/{id}", s.deleteArticle) server.Handle(http.MethodDelete, "/article/{id}", s.deleteArticle)
} }
func (s Service) getArticlesByAuthor(req api.Request, res *api.Response) { func (s Service) getArticlesByAuthor(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("author_id") id, err := req.Param.GetUint("author_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "author_id") return api.ErrorInvalidParam
return
} }
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Where("author = ?", id).Find(&articles) s.DB.Where("author = ?", id).Find(&articles)
res.SetData("articles", articles) res.SetData("articles", articles)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) getAllArticles(req api.Request, res *api.Response) { func (s Service) getAllArticles(req api.Request, res *api.Response) api.Error {
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Find(&articles) s.DB.Find(&articles)
res.SetData("articles", articles) res.SetData("articles", articles)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) getArticleByID(req api.Request, res *api.Response) { func (s Service) getArticleByID(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("article_id") id, err := req.Param.GetUint("article_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "article_id") return api.ErrorInvalidParam
return
} }
var article model.Article var article model.Article
if s.DB.First(&article, id).RecordNotFound() { if s.DB.First(&article, id).RecordNotFound() {
res.SetError(api.ErrorNoMatchFound()) return api.ErrorNoMatchFound
return
} }
res.SetData("id", article.ID) res.SetData("id", article.ID)
res.SetData("title", article.Title) res.SetData("title", article.Title)
res.SetData("body", article.Body) res.SetData("body", article.Body)
res.SetData("author", article.Author) res.SetData("author", article.Author)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) postArticle(req api.Request, res *api.Response) { func (s Service) postArticle(req api.Request, res *api.Response) api.Error {
return api.ErrorNotImplemented
} }
func (s Service) deleteArticle(req api.Request, res *api.Response) { func (s Service) deleteArticle(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id") id, err := req.Param.GetUint("user_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "user_id") return api.ErrorInvalidParam
return
} }
article := model.Article{} article := model.Article{}
article.ID = id article.ID = id
s.DB.Delete(&article) s.DB.Delete(&article)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }

View File

@ -20,30 +20,28 @@ func (s Service) Wire(server *aicra.Server) {
s.DB.CreateTable(&model.User{}) s.DB.CreateTable(&model.User{})
} }
server.HandleFunc(http.MethodGet, "/users", s.getAllUsers) server.Handle(http.MethodGet, "/users", s.getAllUsers)
server.HandleFunc(http.MethodGet, "/user/{id}", s.getUserByID) server.Handle(http.MethodGet, "/user/{id}", s.getUserByID)
server.HandleFunc(http.MethodPost, "/user", s.createUser) server.Handle(http.MethodPost, "/user", s.createUser)
server.HandleFunc(http.MethodPut, "/user/{id}", s.updateUser) server.Handle(http.MethodPut, "/user/{id}", s.updateUser)
server.HandleFunc(http.MethodDelete, "/user/{id}", s.deleteUser) server.Handle(http.MethodDelete, "/user/{id}", s.deleteUser)
} }
func (s Service) getAllUsers(req api.Request, res *api.Response) { func (s Service) getAllUsers(req api.Request, res *api.Response) api.Error {
users := make([]model.User, 0) users := make([]model.User, 0)
s.DB.Find(&users) s.DB.Find(&users)
res.SetData("users", users) res.SetData("users", users)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) getUserByID(req api.Request, res *api.Response) { func (s Service) getUserByID(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id") id, err := req.Param.GetUint("user_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "user_id") return api.ErrorInvalidParam
return
} }
var user model.User var user model.User
if s.DB.First(&user, id).RecordNotFound() { if s.DB.First(&user, id).RecordNotFound() {
res.SetError(api.ErrorNoMatchFound()) return api.ErrorNoMatchFound
return
} }
res.SetData("id", user.ID) res.SetData("id", user.ID)
@ -51,32 +49,28 @@ func (s Service) getUserByID(req api.Request, res *api.Response) {
res.SetData("firstname", user.Firstname) res.SetData("firstname", user.Firstname)
res.SetData("lastname", user.Lastname) res.SetData("lastname", user.Lastname)
res.SetData("articles", user.Articles) res.SetData("articles", user.Articles)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) createUser(req api.Request, res *api.Response) { func (s Service) createUser(req api.Request, res *api.Response) api.Error {
var user model.User var user model.User
var err error var err error
user.Username, err = req.Param.GetString("username") user.Username, err = req.Param.GetString("username")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "username") return api.ErrorInvalidParam
return
} }
user.Firstname, err = req.Param.GetString("firstname") user.Firstname, err = req.Param.GetString("firstname")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "firstname") return api.ErrorInvalidParam
return
} }
user.Lastname, err = req.Param.GetString("lastname") user.Lastname, err = req.Param.GetString("lastname")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "lastname") return api.ErrorInvalidParam
return
} }
s.DB.Create(&user) s.DB.Create(&user)
if s.DB.Last(&user).RecordNotFound() { if s.DB.Last(&user).RecordNotFound() {
res.SetError(api.ErrorNoMatchFound()) return api.ErrorNoMatchFound
return
} }
res.SetData("id", user.ID) res.SetData("id", user.ID)
@ -84,20 +78,18 @@ func (s Service) createUser(req api.Request, res *api.Response) {
res.SetData("firstname", user.Firstname) res.SetData("firstname", user.Firstname)
res.SetData("lastname", user.Lastname) res.SetData("lastname", user.Lastname)
res.SetData("articles", user.Articles) res.SetData("articles", user.Articles)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) updateUser(req api.Request, res *api.Response) { func (s Service) updateUser(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id") id, err := req.Param.GetUint("user_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "user_id") return api.ErrorInvalidParam
return
} }
var user model.User var user model.User
if s.DB.First(&user, id).RecordNotFound() { if s.DB.First(&user, id).RecordNotFound() {
res.SetError(api.ErrorNoMatchFound()) return api.ErrorNoMatchFound
return
} }
// override with updated values // override with updated values
@ -113,21 +105,19 @@ func (s Service) updateUser(req api.Request, res *api.Response) {
// update // update
if s.DB.Save(&user).RowsAffected < 1 { if s.DB.Save(&user).RowsAffected < 1 {
res.SetError(api.ErrorFailure()) return api.ErrorFailure
return
} }
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }
func (s Service) deleteUser(req api.Request, res *api.Response) { func (s Service) deleteUser(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id") id, err := req.Param.GetUint("user_id")
if err != nil { if err != nil {
res.SetError(api.ErrorInvalidParam(), "user_id") return api.ErrorInvalidParam
return
} }
user := model.User{} user := model.User{}
user.ID = id user.ID = id
s.DB.Delete(&user) s.DB.Delete(&user)
res.SetError(api.ErrorSuccess()) return api.ErrorSuccess
} }