upgrade to v0.3.0 dynamic handler management

This commit is contained in:
Adrien Marquès 2020-03-29 17:03:53 +02:00
parent 32afe3ae05
commit f31adca326
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
3 changed files with 161 additions and 113 deletions

View File

@ -38,8 +38,12 @@ func main() {
articleService := &article.Service{DB: db} articleService := &article.Service{DB: db}
// 4. wire services // 4. wire services
userService.Wire(server) if err = userService.Wire(server); err != nil {
articleService.Wire(server) log.Fatalf("/!\\ cannot wire service 'user': %s", err)
}
if err = articleService.Wire(server); err != nil {
log.Fatalf("/!\\ cannot wire service 'article': %s", err)
}
// 5. create http server // 5. create http server
httpServer, err := server.ToHTTPServer() httpServer, err := server.ToHTTPServer()

View File

@ -15,66 +15,87 @@ type Service struct {
} }
// Wire services to their paths // Wire services to their paths
func (s Service) Wire(server *aicra.Server) { func (s Service) Wire(server *aicra.Server) error {
if !s.DB.HasTable(&model.Article{}) { if !s.DB.HasTable(&model.Article{}) {
s.DB.CreateTable(&model.Article{}) s.DB.CreateTable(&model.Article{})
} }
server.Handle(http.MethodGet, "/articles", s.getAllArticles) if err := server.Handle(http.MethodGet, "/articles", s.getAllArticles); err != nil {
server.Handle(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor) return err
server.Handle(http.MethodGet, "/article/{id}", s.getArticleByID) }
server.Handle(http.MethodPost, "/article", s.postArticle) if err := server.Handle(http.MethodGet, "/user/{id}/articles", s.getArticlesByAuthor); err != nil {
server.Handle(http.MethodDelete, "/article/{id}", s.deleteArticle) 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
} }
func (s Service) getArticlesByAuthor(req api.Request, res *api.Response) api.Error { type iByID struct {
id, err := req.Param.GetUint("author_id") ID uint
if err != nil {
return api.ErrorInvalidParam
} }
type oArticle struct {
ID uint
Author uint
Title string
Body string
Score uint
}
type iCreate struct {
Title string
Body string
}
type oArticleList struct {
Articles []model.Article
}
func (s Service) getArticlesByAuthor(param iByID) (*oArticleList, api.Error) {
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Where("author = ?", id).Find(&articles) s.DB.Where("author = ?", param.ID).Find(&articles)
res.SetData("articles", articles) return &oArticleList{
return api.ErrorSuccess Articles: articles,
}, api.ErrorSuccess
} }
func (s Service) getAllArticles(req api.Request, res *api.Response) api.Error { func (s Service) getAllArticles() (*oArticleList, api.Error) {
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Find(&articles) s.DB.Find(&articles)
res.SetData("articles", articles) return &oArticleList{
return api.ErrorSuccess Articles: articles,
} }, api.ErrorSuccess
func (s Service) getArticleByID(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("article_id")
if err != nil {
return api.ErrorInvalidParam
} }
func (s Service) getArticleByID(param iByID) (*oArticle, api.Error) {
var article model.Article var article model.Article
if s.DB.First(&article, id).RecordNotFound() { if s.DB.First(&article, param.ID).RecordNotFound() {
return api.ErrorNoMatchFound return nil, api.ErrorNoMatchFound
} }
res.SetData("id", article.ID) return &oArticle{
res.SetData("title", article.Title) ID: article.ID,
res.SetData("body", article.Body) Title: article.Title,
res.SetData("author", article.Author) Body: article.Body,
return api.ErrorSuccess Author: article.Author,
}, api.ErrorSuccess
} }
func (s Service) postArticle(req api.Request, res *api.Response) api.Error { func (s Service) postArticle(param iCreate) (*oArticle, api.Error) {
return api.ErrorNotImplemented return nil, api.ErrorNotImplemented
} }
func (s Service) deleteArticle(req api.Request, res *api.Response) api.Error { func (s Service) deleteArticle(param iByID) api.Error {
id, err := req.Param.GetUint("user_id") article := model.Article{
if err != nil { ID: param.ID,
return api.ErrorInvalidParam
} }
article := model.Article{}
article.ID = id
s.DB.Delete(&article) s.DB.Delete(&article)
return api.ErrorSuccess return api.ErrorSuccess

View File

@ -15,109 +15,132 @@ type Service struct {
} }
// Wire services to their paths // Wire services to their paths
func (s Service) Wire(server *aicra.Server) { func (s Service) Wire(server *aicra.Server) error {
if !s.DB.HasTable(&model.User{}) { if !s.DB.HasTable(&model.User{}) {
s.DB.CreateTable(&model.User{}) s.DB.CreateTable(&model.User{})
} }
server.Handle(http.MethodGet, "/users", s.getAllUsers) if err := server.Handle(http.MethodGet, "/user/{id}", s.getUserByID); err != nil {
server.Handle(http.MethodGet, "/user/{id}", s.getUserByID) return err
server.Handle(http.MethodPost, "/user", s.createUser) }
server.Handle(http.MethodPut, "/user/{id}", s.updateUser) if err := server.Handle(http.MethodGet, "/users", s.getAllUsers); err != nil {
server.Handle(http.MethodDelete, "/user/{id}", s.deleteUser) return err
}
if err := server.Handle(http.MethodPost, "/user", s.createUser); err != nil {
return err
}
if err := server.Handle(http.MethodPut, "/user/{id}", s.updateUser); err != nil {
return err
}
if err := server.Handle(http.MethodDelete, "/user/{id}", s.deleteUser); err != nil {
return err
}
return nil
} }
func (s Service) getAllUsers(req api.Request, res *api.Response) api.Error { type oUserList struct {
users := make([]model.User, 0) Users []model.User
s.DB.Find(&users)
res.SetData("users", users)
return api.ErrorSuccess
}
func (s Service) getUserByID(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id")
if err != nil {
return api.ErrorInvalidParam
} }
type oUser struct {
ID uint
Username string
Firstname string
Lastname string
Articles interface{}
}
type iByID struct {
ID uint
}
type iCreate struct {
Username string
Firstname string
Lastname string
}
type iUpdate struct {
ID uint
Username *string
Firstname *string
Lastname *string
}
func (s Service) getAllUsers() (*oUserList, api.Error) {
users := oUserList{}
s.DB.Find(&users.Users)
return &users, api.ErrorSuccess
}
func (s Service) getUserByID(param iByID) (*oUser, api.Error) {
var user model.User var user model.User
if s.DB.First(&user, id).RecordNotFound() { if s.DB.First(&user, param.ID).RecordNotFound() {
return api.ErrorNoMatchFound return nil, api.ErrorNoMatchFound
} }
res.SetData("id", user.ID) return &oUser{
res.SetData("username", user.Username) ID: user.ID,
res.SetData("firstname", user.Firstname) Username: user.Username,
res.SetData("lastname", user.Lastname) Firstname: user.Firstname,
res.SetData("articles", user.Articles) Lastname: user.Lastname,
return api.ErrorSuccess Articles: user.Articles,
}, api.ErrorSuccess
} }
func (s Service) createUser(req api.Request, res *api.Response) api.Error { func (s Service) createUser(param iCreate) (*oUser, api.Error) {
var user model.User user := model.User{
var err error Username: param.Username,
user.Username, err = req.Param.GetString("username") Firstname: param.Firstname,
if err != nil { Lastname: param.Lastname,
return api.ErrorInvalidParam
}
user.Firstname, err = req.Param.GetString("firstname")
if err != nil {
return api.ErrorInvalidParam
}
user.Lastname, err = req.Param.GetString("lastname")
if err != nil {
return api.ErrorInvalidParam
} }
s.DB.Create(&user) s.DB.Create(&user)
if s.DB.Last(&user).RecordNotFound() { if s.DB.Last(&user).RecordNotFound() {
return api.ErrorNoMatchFound return nil, api.ErrorNoMatchFound
} }
res.SetData("id", user.ID) return &oUser{
res.SetData("username", user.Username) ID: user.ID,
res.SetData("firstname", user.Firstname) Username: user.Username,
res.SetData("lastname", user.Lastname) Firstname: user.Firstname,
res.SetData("articles", user.Articles) Lastname: user.Lastname,
return api.ErrorSuccess Articles: user.Articles,
} }, api.ErrorSuccess
func (s Service) updateUser(req api.Request, res *api.Response) api.Error {
id, err := req.Param.GetUint("user_id")
if err != nil {
return api.ErrorInvalidParam
} }
func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
var user model.User var user model.User
if s.DB.First(&user, id).RecordNotFound() { if s.DB.First(&user, param.ID).RecordNotFound() {
return api.ErrorNoMatchFound return nil, api.ErrorNoMatchFound
} }
// override with updated values // override with updated values
if updated, err := req.Param.GetString("username"); err == nil { if param.Username != nil {
user.Username = updated user.Username = *param.Username
} }
if updated, err := req.Param.GetString("firstname"); err == nil { if param.Firstname != nil {
user.Firstname = updated user.Firstname = *param.Firstname
} }
if updated, err := req.Param.GetString("lastname"); err == nil { if param.Lastname != nil {
user.Lastname = updated user.Lastname = *param.Lastname
} }
// update // update
if s.DB.Save(&user).RowsAffected < 1 { if s.DB.Save(&user).RowsAffected < 1 {
return api.ErrorFailure return nil, api.ErrorFailure
}
return api.ErrorSuccess
} }
func (s Service) deleteUser(req api.Request, res *api.Response) api.Error { return &oUser{
id, err := req.Param.GetUint("user_id") ID: user.ID,
if err != nil { Username: user.Username,
return api.ErrorInvalidParam Firstname: user.Firstname,
Lastname: user.Lastname,
Articles: user.Articles,
}, api.ErrorSuccess
} }
user := model.User{} func (s Service) deleteUser(param iByID) api.Error {
user.ID = id user := model.User{
ID: param.ID,
}
s.DB.Delete(&user) s.DB.Delete(&user)
return api.ErrorSuccess return api.ErrorSuccess
} }