use models for api handler output instead of creating a copied struct

This commit is contained in:
Adrien Marquès 2020-03-29 17:30:36 +02:00
parent 56cd3ff26c
commit 1df830f97f
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
4 changed files with 11 additions and 48 deletions

View File

@ -63,6 +63,7 @@ func buildDataTypes() []datatype.T {
dtypes = append(dtypes, builtin.AnyDataType{})
dtypes = append(dtypes, builtin.BoolDataType{})
dtypes = append(dtypes, builtin.UintDataType{})
dtypes = append(dtypes, builtin.IntDataType{})
dtypes = append(dtypes, builtin.StringDataType{})
return dtypes

View File

@ -38,14 +38,6 @@ func (s Service) Wire(server *aicra.Server) error {
return nil
}
type articleInfo struct {
ID uint
Author uint
Title string
Body string
Score uint
}
type articleList struct {
Articles []model.Article
}
@ -66,18 +58,13 @@ func (s Service) getAllArticles() (*articleList, api.Error) {
}, api.ErrorSuccess
}
func (s Service) getArticleByID(param struct{ ID uint }) (*articleInfo, api.Error) {
func (s Service) getArticleByID(param struct{ ID uint }) (*model.Article, api.Error) {
var article model.Article
if s.DB.First(&article, param.ID).RecordNotFound() {
return nil, api.ErrorNoMatchFound
}
return &articleInfo{
ID: article.ID,
Title: article.Title,
Body: article.Body,
Author: article.Author,
}, api.ErrorSuccess
return &article, api.ErrorSuccess
}
type createRequest struct {
@ -85,7 +72,7 @@ type createRequest struct {
Body string
}
func (s Service) postArticle(param createRequest) (*articleInfo, api.Error) {
func (s Service) postArticle(param createRequest) (*model.Article, api.Error) {
return nil, api.ErrorNotImplemented
}

View File

@ -6,4 +6,5 @@ type Article struct {
Author uint `json:"author"`
Title string `json:"title"`
Body string `json:"body"`
Score int
}

View File

@ -38,14 +38,6 @@ func (s Service) Wire(server *aicra.Server) error {
return nil
}
type userInfo struct {
ID uint
Username string
Firstname string
Lastname string
Articles interface{}
}
type userList struct {
Users []model.User
}
@ -56,19 +48,13 @@ func (s Service) getAllUsers() (*userList, api.Error) {
return &users, api.ErrorSuccess
}
func (s Service) getUserByID(param struct{ ID uint }) (*userInfo, api.Error) {
func (s Service) getUserByID(param struct{ ID uint }) (*model.User, api.Error) {
var user model.User
if s.DB.First(&user, param.ID).RecordNotFound() {
return nil, api.ErrorNoMatchFound
}
return &userInfo{
ID: user.ID,
Username: user.Username,
Firstname: user.Firstname,
Lastname: user.Lastname,
Articles: user.Articles,
}, api.ErrorSuccess
return &user, api.ErrorSuccess
}
type createRequest struct {
@ -77,7 +63,7 @@ type createRequest struct {
Lastname string
}
func (s Service) createUser(param createRequest) (*userInfo, api.Error) {
func (s Service) createUser(param createRequest) (*model.User, api.Error) {
user := model.User{
Username: param.Username,
Firstname: param.Firstname,
@ -89,13 +75,7 @@ func (s Service) createUser(param createRequest) (*userInfo, api.Error) {
return nil, api.ErrorNoMatchFound
}
return &userInfo{
ID: user.ID,
Username: user.Username,
Firstname: user.Firstname,
Lastname: user.Lastname,
Articles: user.Articles,
}, api.ErrorSuccess
return &user, api.ErrorSuccess
}
type updateRequest struct {
@ -105,7 +85,7 @@ type updateRequest struct {
Lastname *string
}
func (s Service) updateUser(param updateRequest) (*userInfo, api.Error) {
func (s Service) updateUser(param updateRequest) (*model.User, api.Error) {
var user model.User
if s.DB.First(&user, param.ID).RecordNotFound() {
return nil, api.ErrorNoMatchFound
@ -127,13 +107,7 @@ func (s Service) updateUser(param updateRequest) (*userInfo, api.Error) {
return nil, api.ErrorFailure
}
return &userInfo{
ID: user.ID,
Username: user.Username,
Firstname: user.Firstname,
Lastname: user.Lastname,
Articles: user.Articles,
}, api.ErrorSuccess
return &user, api.ErrorSuccess
}
func (s Service) deleteUser(param struct{ ID uint }) api.Error {