Compare commits

...

3 Commits

5 changed files with 202 additions and 154 deletions

View File

@ -7,7 +7,7 @@
"info": "returns the list of existing users", "info": "returns the list of existing users",
"in": {}, "in": {},
"out": { "out": {
"users": { "info": "users list", "type": "any" } "users": { "info": "users list", "type": "any", "name": "Users" }
} }
}, },
{ {
@ -16,14 +16,14 @@
"scope": [], "scope": [],
"info": "returns info about an existing user", "info": "returns info about an existing user",
"in": { "in": {
"{id}": { "info": "the target user id", "name": "user_id", "type": "uint" } "{id}": { "info": "the target user id", "name": "ID", "type": "uint" }
}, },
"out": { "out": {
"id": { "info": "user id", "type": "uint" }, "id": { "info": "user id", "type": "uint", "name": "ID" },
"username": { "info": "username", "type": "string(3,30)" }, "username": { "info": "username", "type": "string(3,30)", "name": "Username" },
"firstname": { "info": "first name", "type": "string(1,30)" }, "firstname": { "info": "first name", "type": "string(1,30)", "name": "Firstname" },
"lastname": { "info": "last name", "type": "string(1,30)" }, "lastname": { "info": "last name", "type": "string(1,30)", "name": "Lastname" },
"articles": { "info": "user articles", "type": "any" } "articles": { "info": "user articles", "type": "any", "name": "Articles" }
} }
}, },
@ -33,15 +33,15 @@
"scope": [["admin"]], "scope": [["admin"]],
"info": "creates a new user", "info": "creates a new user",
"in": { "in": {
"username": { "info": "username", "type": "string(3,30)" }, "username": { "info": "username", "type": "string(3,30)", "name": "Username" },
"firstname": { "info": "first name", "type": "string(1,30)" }, "firstname": { "info": "first name", "type": "string(1,30)", "name": "Firstname" },
"lastname": { "info": "last name", "type": "string(1,30)" } "lastname": { "info": "last name", "type": "string(1,30)", "name": "Lastname" }
}, },
"out": { "out": {
"id": { "info": "new user's id", "type": "uint" }, "id": { "info": "user id", "type": "uint", "name": "ID" },
"username": { "info": "new user's username", "type": "string(3,30)" }, "username": { "info": "username", "type": "string(3,30)", "name": "Username" },
"firstname": { "info": "new user's first name", "type": "string(1,30)" }, "firstname": { "info": "first name", "type": "string(1,30)", "name": "Firstname" },
"lastname": { "info": "new user's last name", "type": "string(1,30)" } "lastname": { "info": "last name", "type": "string(1,30)", "name": "Lastname" }
} }
}, },
{ {
@ -50,16 +50,16 @@
"scope": [["admin"], ["self"]], "scope": [["admin"], ["self"]],
"info": "updates an existing user", "info": "updates an existing user",
"in": { "in": {
"{id}": { "info": "the target user id", "type": "uint", "name": "user_id" }, "{id}": { "info": "the target user id", "type": "uint", "name": "ID" },
"username": { "info": "updated username", "type": "?string(3,30)" }, "username": { "info": "updated username", "type": "?string(3,30)", "name": "Username" },
"firstname": { "info": "updated first name", "type": "?string(1,30)" }, "firstname": { "info": "updated first name", "type": "?string(1,30)", "name": "Firstname" },
"lastname": { "info": "updated last name", "type": "?string(1,30)" } "lastname": { "info": "updated last name", "type": "?string(1,30)", "name": "Lastname" }
}, },
"out": { "out": {
"id": { "info": "new user's id", "type": "uint" }, "id": { "info": "user id", "type": "uint", "name": "ID" },
"username": { "info": "new user's username", "type": "string(3,30)" }, "username": { "info": "username", "type": "string(3,30)", "name": "Username" },
"firstname": { "info": "new user's first name", "type": "string(1,30)" }, "firstname": { "info": "first name", "type": "string(1,30)", "name": "Firstname" },
"lastname": { "info": "new user's last name", "type": "string(1,30)" } "lastname": { "info": "last name", "type": "string(1,30)", "name": "Lastname" }
} }
}, },
@ -69,7 +69,7 @@
"scope": [["admin"], ["self"]], "scope": [["admin"], ["self"]],
"info": "deletes an existing user", "info": "deletes an existing user",
"in": { "in": {
"{id}": { "info": "the target user id", "name": "user_id", "type": "uint" } "{id}": { "info": "the target user id", "name": "ID", "type": "uint" }
}, },
"out": {} "out": {}
}, },
@ -83,10 +83,10 @@
"scope": [[]], "scope": [[]],
"info": "returns the list of existing articles a user wrote", "info": "returns the list of existing articles a user wrote",
"in": { "in": {
"{id}": { "info": "author user id", "name": "author_id", "type": "uint" } "{id}": { "info": "author user id", "name": "ID", "type": "uint" }
}, },
"out": { "out": {
"articles": { "info": "articles list", "type": "any" } "articles": { "info": "articles list", "type": "any", "name": "Articles" }
} }
}, },
@ -97,7 +97,7 @@
"info": "returns the list of existing articles", "info": "returns the list of existing articles",
"in": {}, "in": {},
"out": { "out": {
"articles": { "info": "articles list", "type": "any" } "articles": { "info": "articles list", "type": "any", "name": "Articles" }
} }
}, { }, {
"method": "GET", "method": "GET",
@ -105,14 +105,14 @@
"scope": [[]], "scope": [[]],
"info": "returns an existing article", "info": "returns an existing article",
"in": { "in": {
"{id}": { "info": "the target article id", "name": "article_id", "type": "uint" } "{id}": { "info": "the target article id", "name": "ID", "type": "uint" }
}, },
"out": { "out": {
"id": { "info": "the article id", "type": "uint" }, "id": { "info": "the article id", "type": "uint", "name": "ID" },
"title": { "info": "the article title", "type": "string(5,255)" }, "title": { "info": "the article title", "type": "string(5,255)", "name": "Title" },
"body": { "info": "the article body", "type": "string" }, "body": { "info": "the article body", "type": "string", "name": "Body" },
"author": { "info": "the author user id", "type": "uint" }, "author": { "info": "the author user id", "type": "uint", "name": "Author" },
"score": { "info": "absolute vote score", "type": "uint" } "score": { "info": "absolute vote score", "type": "uint", "name": "Score" }
} }
}, { }, {
"method": "POST", "method": "POST",
@ -120,15 +120,15 @@
"scope": [["author"]], "scope": [["author"]],
"info": "post a new article", "info": "post a new article",
"in": { "in": {
"title": { "info": "the article title", "type": "string(5,255)" }, "title": { "info": "the article title", "type": "string(5,255)", "name": "Title" },
"body": { "info": "the article body", "type": "string" } "body": { "info": "the article body", "type": "string", "name": "Body" }
}, },
"out": { "out": {
"id": { "info": "the article id", "type": "uint" }, "id": { "info": "the article id", "type": "uint", "name": "ID" },
"title": { "info": "the article title", "type": "string(5,255)" }, "title": { "info": "the article title", "type": "string(5,255)", "name": "Title" },
"body": { "info": "the article body", "type": "string" }, "body": { "info": "the article body", "type": "string", "name": "Body" },
"author": { "info": "the author user id", "type": "uint" }, "author": { "info": "the author user id", "type": "uint", "name": "Author" },
"score": { "info": "absolute vote score", "type": "uint" } "score": { "info": "absolute vote score", "type": "uint", "name": "Score" }
} }
}, { }, {
"method": "DELETE", "method": "DELETE",
@ -136,7 +136,7 @@
"scope": [["admin"], ["author"]], "scope": [["admin"], ["author"]],
"info": "deletes an article", "info": "deletes an article",
"in": { "in": {
"{id}": { "info": "the target article id", "name": "article_id", "type": "uint" } "{id}": { "info": "the target article id", "name": "ID", "type": "uint" }
}, },
"out": { } "out": { }
} }

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

@ -6,5 +6,5 @@ type User struct {
Username string `json:"username"` Username string `json:"username"`
Firstname string `json:"firstname"` Firstname string `json:"firstname"`
Lastname string `json:"lastname"` Lastname string `json:"lastname"`
Articles []Article `gorm:"foreignKey:Author"` Articles []Article `json:"articles" gorm:"foreignKey:Author"`
} }

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
} }