From 1df830f97f9869c7e059fb1719d75dbf5955d49b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 29 Mar 2020 17:30:36 +0200 Subject: [PATCH] use models for api handler output instead of creating a copied struct --- main.go | 1 + service/article/article.go | 19 +++---------------- service/model/article.go | 1 + service/user/user.go | 38 ++++++-------------------------------- 4 files changed, 11 insertions(+), 48 deletions(-) diff --git a/main.go b/main.go index 1ca0b4d..d50d9ce 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/service/article/article.go b/service/article/article.go index f500f9a..fd462a9 100644 --- a/service/article/article.go +++ b/service/article/article.go @@ -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 } diff --git a/service/model/article.go b/service/model/article.go index c16d840..621758d 100644 --- a/service/model/article.go +++ b/service/model/article.go @@ -6,4 +6,5 @@ type Article struct { Author uint `json:"author"` Title string `json:"title"` Body string `json:"body"` + Score int } diff --git a/service/user/user.go b/service/user/user.go index c677547..9203483 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -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 {