upgrade to v0.3.0 dynamic handler management
This commit is contained in:
parent
32afe3ae05
commit
f31adca326
8
main.go
8
main.go
|
@ -38,8 +38,12 @@ func main() {
|
|||
articleService := &article.Service{DB: db}
|
||||
|
||||
// 4. wire services
|
||||
userService.Wire(server)
|
||||
articleService.Wire(server)
|
||||
if err = userService.Wire(server); err != nil {
|
||||
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
|
||||
httpServer, err := server.ToHTTPServer()
|
||||
|
|
|
@ -15,66 +15,87 @@ type Service struct {
|
|||
}
|
||||
|
||||
// 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{}) {
|
||||
s.DB.CreateTable(&model.Article{})
|
||||
}
|
||||
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
||||
func (s Service) getArticlesByAuthor(req api.Request, res *api.Response) api.Error {
|
||||
id, err := req.Param.GetUint("author_id")
|
||||
if err != nil {
|
||||
return api.ErrorInvalidParam
|
||||
type iByID struct {
|
||||
ID uint
|
||||
}
|
||||
|
||||
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)
|
||||
s.DB.Where("author = ?", id).Find(&articles)
|
||||
res.SetData("articles", articles)
|
||||
return api.ErrorSuccess
|
||||
s.DB.Where("author = ?", param.ID).Find(&articles)
|
||||
return &oArticleList{
|
||||
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)
|
||||
s.DB.Find(&articles)
|
||||
res.SetData("articles", articles)
|
||||
return 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
|
||||
return &oArticleList{
|
||||
Articles: articles,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
func (s Service) getArticleByID(param iByID) (*oArticle, api.Error) {
|
||||
var article model.Article
|
||||
if s.DB.First(&article, id).RecordNotFound() {
|
||||
return api.ErrorNoMatchFound
|
||||
if s.DB.First(&article, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
res.SetData("id", article.ID)
|
||||
res.SetData("title", article.Title)
|
||||
res.SetData("body", article.Body)
|
||||
res.SetData("author", article.Author)
|
||||
return api.ErrorSuccess
|
||||
return &oArticle{
|
||||
ID: article.ID,
|
||||
Title: article.Title,
|
||||
Body: article.Body,
|
||||
Author: article.Author,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
func (s Service) postArticle(req api.Request, res *api.Response) api.Error {
|
||||
return api.ErrorNotImplemented
|
||||
func (s Service) postArticle(param iCreate) (*oArticle, api.Error) {
|
||||
return nil, api.ErrorNotImplemented
|
||||
}
|
||||
|
||||
func (s Service) deleteArticle(req api.Request, res *api.Response) api.Error {
|
||||
id, err := req.Param.GetUint("user_id")
|
||||
if err != nil {
|
||||
return api.ErrorInvalidParam
|
||||
func (s Service) deleteArticle(param iByID) api.Error {
|
||||
article := model.Article{
|
||||
ID: param.ID,
|
||||
}
|
||||
|
||||
article := model.Article{}
|
||||
article.ID = id
|
||||
s.DB.Delete(&article)
|
||||
|
||||
return api.ErrorSuccess
|
||||
|
|
|
@ -15,109 +15,132 @@ type Service struct {
|
|||
}
|
||||
|
||||
// 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{}) {
|
||||
s.DB.CreateTable(&model.User{})
|
||||
}
|
||||
|
||||
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)
|
||||
if err := server.Handle(http.MethodGet, "/user/{id}", s.getUserByID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := server.Handle(http.MethodGet, "/users", s.getAllUsers); err != nil {
|
||||
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 {
|
||||
users := make([]model.User, 0)
|
||||
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 oUserList struct {
|
||||
Users []model.User
|
||||
}
|
||||
|
||||
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
|
||||
if s.DB.First(&user, id).RecordNotFound() {
|
||||
return api.ErrorNoMatchFound
|
||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
res.SetData("id", user.ID)
|
||||
res.SetData("username", user.Username)
|
||||
res.SetData("firstname", user.Firstname)
|
||||
res.SetData("lastname", user.Lastname)
|
||||
res.SetData("articles", user.Articles)
|
||||
return api.ErrorSuccess
|
||||
return &oUser{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
Lastname: user.Lastname,
|
||||
Articles: user.Articles,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
func (s Service) createUser(param iCreate) (*oUser, api.Error) {
|
||||
user := model.User{
|
||||
Username: param.Username,
|
||||
Firstname: param.Firstname,
|
||||
Lastname: param.Lastname,
|
||||
}
|
||||
|
||||
s.DB.Create(&user)
|
||||
if s.DB.Last(&user).RecordNotFound() {
|
||||
return api.ErrorNoMatchFound
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
res.SetData("id", user.ID)
|
||||
res.SetData("username", user.Username)
|
||||
res.SetData("firstname", user.Firstname)
|
||||
res.SetData("lastname", user.Lastname)
|
||||
res.SetData("articles", user.Articles)
|
||||
return 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
|
||||
return &oUser{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
Lastname: user.Lastname,
|
||||
Articles: user.Articles,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
|
||||
var user model.User
|
||||
if s.DB.First(&user, id).RecordNotFound() {
|
||||
return api.ErrorNoMatchFound
|
||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
// override with updated values
|
||||
if updated, err := req.Param.GetString("username"); err == nil {
|
||||
user.Username = updated
|
||||
if param.Username != nil {
|
||||
user.Username = *param.Username
|
||||
}
|
||||
if updated, err := req.Param.GetString("firstname"); err == nil {
|
||||
user.Firstname = updated
|
||||
if param.Firstname != nil {
|
||||
user.Firstname = *param.Firstname
|
||||
}
|
||||
if updated, err := req.Param.GetString("lastname"); err == nil {
|
||||
user.Lastname = updated
|
||||
if param.Lastname != nil {
|
||||
user.Lastname = *param.Lastname
|
||||
}
|
||||
|
||||
// update
|
||||
if s.DB.Save(&user).RowsAffected < 1 {
|
||||
return api.ErrorFailure
|
||||
}
|
||||
return api.ErrorSuccess
|
||||
return nil, api.ErrorFailure
|
||||
}
|
||||
|
||||
func (s Service) deleteUser(req api.Request, res *api.Response) api.Error {
|
||||
id, err := req.Param.GetUint("user_id")
|
||||
if err != nil {
|
||||
return api.ErrorInvalidParam
|
||||
return &oUser{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
Lastname: user.Lastname,
|
||||
Articles: user.Articles,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
user := model.User{}
|
||||
user.ID = id
|
||||
func (s Service) deleteUser(param iByID) api.Error {
|
||||
user := model.User{
|
||||
ID: param.ID,
|
||||
}
|
||||
s.DB.Delete(&user)
|
||||
return api.ErrorSuccess
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue