use models for api handler output instead of creating a copied struct
This commit is contained in:
parent
56cd3ff26c
commit
1df830f97f
1
main.go
1
main.go
|
@ -63,6 +63,7 @@ func buildDataTypes() []datatype.T {
|
||||||
dtypes = append(dtypes, builtin.AnyDataType{})
|
dtypes = append(dtypes, builtin.AnyDataType{})
|
||||||
dtypes = append(dtypes, builtin.BoolDataType{})
|
dtypes = append(dtypes, builtin.BoolDataType{})
|
||||||
dtypes = append(dtypes, builtin.UintDataType{})
|
dtypes = append(dtypes, builtin.UintDataType{})
|
||||||
|
dtypes = append(dtypes, builtin.IntDataType{})
|
||||||
dtypes = append(dtypes, builtin.StringDataType{})
|
dtypes = append(dtypes, builtin.StringDataType{})
|
||||||
|
|
||||||
return dtypes
|
return dtypes
|
||||||
|
|
|
@ -38,14 +38,6 @@ func (s Service) Wire(server *aicra.Server) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type articleInfo struct {
|
|
||||||
ID uint
|
|
||||||
Author uint
|
|
||||||
Title string
|
|
||||||
Body string
|
|
||||||
Score uint
|
|
||||||
}
|
|
||||||
|
|
||||||
type articleList struct {
|
type articleList struct {
|
||||||
Articles []model.Article
|
Articles []model.Article
|
||||||
}
|
}
|
||||||
|
@ -66,18 +58,13 @@ func (s Service) getAllArticles() (*articleList, api.Error) {
|
||||||
}, api.ErrorSuccess
|
}, 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
|
var article model.Article
|
||||||
if s.DB.First(&article, param.ID).RecordNotFound() {
|
if s.DB.First(&article, param.ID).RecordNotFound() {
|
||||||
return nil, api.ErrorNoMatchFound
|
return nil, api.ErrorNoMatchFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return &articleInfo{
|
return &article, api.ErrorSuccess
|
||||||
ID: article.ID,
|
|
||||||
Title: article.Title,
|
|
||||||
Body: article.Body,
|
|
||||||
Author: article.Author,
|
|
||||||
}, api.ErrorSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type createRequest struct {
|
type createRequest struct {
|
||||||
|
@ -85,7 +72,7 @@ type createRequest struct {
|
||||||
Body string
|
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
|
return nil, api.ErrorNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@ type Article struct {
|
||||||
Author uint `json:"author"`
|
Author uint `json:"author"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
|
Score int
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,14 +38,6 @@ func (s Service) Wire(server *aicra.Server) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type userInfo struct {
|
|
||||||
ID uint
|
|
||||||
Username string
|
|
||||||
Firstname string
|
|
||||||
Lastname string
|
|
||||||
Articles interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type userList struct {
|
type userList struct {
|
||||||
Users []model.User
|
Users []model.User
|
||||||
}
|
}
|
||||||
|
@ -56,19 +48,13 @@ func (s Service) getAllUsers() (*userList, api.Error) {
|
||||||
return &users, api.ErrorSuccess
|
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
|
var user model.User
|
||||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||||
return nil, api.ErrorNoMatchFound
|
return nil, api.ErrorNoMatchFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return &userInfo{
|
return &user, api.ErrorSuccess
|
||||||
ID: user.ID,
|
|
||||||
Username: user.Username,
|
|
||||||
Firstname: user.Firstname,
|
|
||||||
Lastname: user.Lastname,
|
|
||||||
Articles: user.Articles,
|
|
||||||
}, api.ErrorSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type createRequest struct {
|
type createRequest struct {
|
||||||
|
@ -77,7 +63,7 @@ type createRequest struct {
|
||||||
Lastname string
|
Lastname string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) createUser(param createRequest) (*userInfo, api.Error) {
|
func (s Service) createUser(param createRequest) (*model.User, api.Error) {
|
||||||
user := model.User{
|
user := model.User{
|
||||||
Username: param.Username,
|
Username: param.Username,
|
||||||
Firstname: param.Firstname,
|
Firstname: param.Firstname,
|
||||||
|
@ -89,13 +75,7 @@ func (s Service) createUser(param createRequest) (*userInfo, api.Error) {
|
||||||
return nil, api.ErrorNoMatchFound
|
return nil, api.ErrorNoMatchFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return &userInfo{
|
return &user, api.ErrorSuccess
|
||||||
ID: user.ID,
|
|
||||||
Username: user.Username,
|
|
||||||
Firstname: user.Firstname,
|
|
||||||
Lastname: user.Lastname,
|
|
||||||
Articles: user.Articles,
|
|
||||||
}, api.ErrorSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateRequest struct {
|
type updateRequest struct {
|
||||||
|
@ -105,7 +85,7 @@ type updateRequest struct {
|
||||||
Lastname *string
|
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
|
var user model.User
|
||||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||||
return nil, api.ErrorNoMatchFound
|
return nil, api.ErrorNoMatchFound
|
||||||
|
@ -127,13 +107,7 @@ func (s Service) updateUser(param updateRequest) (*userInfo, api.Error) {
|
||||||
return nil, api.ErrorFailure
|
return nil, api.ErrorFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
return &userInfo{
|
return &user, api.ErrorSuccess
|
||||||
ID: user.ID,
|
|
||||||
Username: user.Username,
|
|
||||||
Firstname: user.Firstname,
|
|
||||||
Lastname: user.Lastname,
|
|
||||||
Articles: user.Articles,
|
|
||||||
}, api.ErrorSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) deleteUser(param struct{ ID uint }) api.Error {
|
func (s Service) deleteUser(param struct{ ID uint }) api.Error {
|
||||||
|
|
Loading…
Reference in New Issue