update according to new api changes
This commit is contained in:
parent
fca88ccf82
commit
473e46ee19
|
@ -20,66 +20,62 @@ func (s Service) Wire(server *aicra.Server) {
|
|||
s.DB.CreateTable(&model.Article{})
|
||||
}
|
||||
|
||||
server.HandleFunc(http.MethodGet, "/articles", s.getAllArticles)
|
||||
server.HandleFunc(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor)
|
||||
server.HandleFunc(http.MethodGet, "/article/{id}", s.getArticleByID)
|
||||
server.HandleFunc(http.MethodPost, "/article", s.postArticle)
|
||||
server.HandleFunc(http.MethodDelete, "/article/{id}", s.deleteArticle)
|
||||
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)
|
||||
}
|
||||
|
||||
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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "author_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
articles := make([]model.Article, 0)
|
||||
s.DB.Where("author = ?", id).Find(&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)
|
||||
s.DB.Find(&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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "article_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
var article model.Article
|
||||
if s.DB.First(&article, id).RecordNotFound() {
|
||||
res.SetError(api.ErrorNoMatchFound())
|
||||
return
|
||||
return api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
res.SetData("id", article.ID)
|
||||
res.SetData("title", article.Title)
|
||||
res.SetData("body", article.Body)
|
||||
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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
article := model.Article{}
|
||||
article.ID = id
|
||||
s.DB.Delete(&article)
|
||||
|
||||
res.SetError(api.ErrorSuccess())
|
||||
return api.ErrorSuccess
|
||||
}
|
||||
|
|
|
@ -20,30 +20,28 @@ func (s Service) Wire(server *aicra.Server) {
|
|||
s.DB.CreateTable(&model.User{})
|
||||
}
|
||||
|
||||
server.HandleFunc(http.MethodGet, "/users", s.getAllUsers)
|
||||
server.HandleFunc(http.MethodGet, "/user/{id}", s.getUserByID)
|
||||
server.HandleFunc(http.MethodPost, "/user", s.createUser)
|
||||
server.HandleFunc(http.MethodPut, "/user/{id}", s.updateUser)
|
||||
server.HandleFunc(http.MethodDelete, "/user/{id}", s.deleteUser)
|
||||
server.Handle(http.MethodGet, "/users", s.getAllUsers)
|
||||
server.Handle(http.MethodGet, "/user/{id}", s.getUserByID)
|
||||
server.Handle(http.MethodPost, "/user", s.createUser)
|
||||
server.Handle(http.MethodPut, "/user/{id}", s.updateUser)
|
||||
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)
|
||||
s.DB.Find(&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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
var user model.User
|
||||
if s.DB.First(&user, id).RecordNotFound() {
|
||||
res.SetError(api.ErrorNoMatchFound())
|
||||
return
|
||||
return api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
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("lastname", user.Lastname)
|
||||
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 err error
|
||||
user.Username, err = req.Param.GetString("username")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "username")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
user.Firstname, err = req.Param.GetString("firstname")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "firstname")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
user.Lastname, err = req.Param.GetString("lastname")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "lastname")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
s.DB.Create(&user)
|
||||
if s.DB.Last(&user).RecordNotFound() {
|
||||
res.SetError(api.ErrorNoMatchFound())
|
||||
return
|
||||
return api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
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("lastname", user.Lastname)
|
||||
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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
var user model.User
|
||||
if s.DB.First(&user, id).RecordNotFound() {
|
||||
res.SetError(api.ErrorNoMatchFound())
|
||||
return
|
||||
return api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
// override with updated values
|
||||
|
@ -113,21 +105,19 @@ func (s Service) updateUser(req api.Request, res *api.Response) {
|
|||
|
||||
// update
|
||||
if s.DB.Save(&user).RowsAffected < 1 {
|
||||
res.SetError(api.ErrorFailure())
|
||||
return
|
||||
return api.ErrorFailure
|
||||
}
|
||||
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")
|
||||
if err != nil {
|
||||
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||||
return
|
||||
return api.ErrorInvalidParam
|
||||
}
|
||||
|
||||
user := model.User{}
|
||||
user.ID = id
|
||||
s.DB.Delete(&user)
|
||||
res.SetError(api.ErrorSuccess())
|
||||
return api.ErrorSuccess
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue