make service structs more readable

This commit is contained in:
Adrien Marquès 2020-03-29 17:24:35 +02:00
parent f31adca326
commit 56cd3ff26c
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
3 changed files with 43 additions and 47 deletions

2
go.mod
View File

@ -5,6 +5,6 @@ go 1.14
require ( require (
git.xdrm.io/go/aicra v0.3.0 git.xdrm.io/go/aicra v0.3.0
github.com/jinzhu/gorm v1.9.12 github.com/jinzhu/gorm v1.9.12
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
) )
replace git.xdrm.io/go/aicra => ../aicra

View File

@ -38,49 +38,41 @@ func (s Service) Wire(server *aicra.Server) error {
return nil return nil
} }
type iByID struct { type articleInfo struct {
ID uint
}
type oArticle struct {
ID uint ID uint
Author uint Author uint
Title string Title string
Body string Body string
Score uint Score uint
} }
type iCreate struct {
Title string
Body string
}
type oArticleList struct { type articleList struct {
Articles []model.Article Articles []model.Article
} }
func (s Service) getArticlesByAuthor(param iByID) (*oArticleList, api.Error) { func (s Service) getArticlesByAuthor(param struct{ ID uint }) (*articleList, api.Error) {
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Where("author = ?", param.ID).Find(&articles) s.DB.Where("author = ?", param.ID).Find(&articles)
return &oArticleList{ return &articleList{
Articles: articles, Articles: articles,
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) getAllArticles() (*oArticleList, api.Error) { func (s Service) getAllArticles() (*articleList, api.Error) {
articles := make([]model.Article, 0) articles := make([]model.Article, 0)
s.DB.Find(&articles) s.DB.Find(&articles)
return &oArticleList{ return &articleList{
Articles: articles, Articles: articles,
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) getArticleByID(param iByID) (*oArticle, api.Error) { func (s Service) getArticleByID(param struct{ ID uint }) (*articleInfo, 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 &oArticle{ return &articleInfo{
ID: article.ID, ID: article.ID,
Title: article.Title, Title: article.Title,
Body: article.Body, Body: article.Body,
@ -88,11 +80,16 @@ func (s Service) getArticleByID(param iByID) (*oArticle, api.Error) {
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) postArticle(param iCreate) (*oArticle, api.Error) { type createRequest struct {
Title string
Body string
}
func (s Service) postArticle(param createRequest) (*articleInfo, api.Error) {
return nil, api.ErrorNotImplemented return nil, api.ErrorNotImplemented
} }
func (s Service) deleteArticle(param iByID) api.Error { func (s Service) deleteArticle(param struct{ ID uint }) api.Error {
article := model.Article{ article := model.Article{
ID: param.ID, ID: param.ID,
} }

View File

@ -38,45 +38,31 @@ func (s Service) Wire(server *aicra.Server) error {
return nil return nil
} }
type oUserList struct { type userInfo struct {
Users []model.User
}
type oUser struct {
ID uint ID uint
Username string Username string
Firstname string Firstname string
Lastname string Lastname string
Articles interface{} Articles interface{}
} }
type iByID struct {
ID uint type userList struct {
Users []model.User
} }
type iCreate struct { func (s Service) getAllUsers() (*userList, api.Error) {
Username string users := userList{}
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) s.DB.Find(&users.Users)
return &users, api.ErrorSuccess return &users, api.ErrorSuccess
} }
func (s Service) getUserByID(param iByID) (*oUser, api.Error) {
func (s Service) getUserByID(param struct{ ID uint }) (*userInfo, 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 &oUser{ return &userInfo{
ID: user.ID, ID: user.ID,
Username: user.Username, Username: user.Username,
Firstname: user.Firstname, Firstname: user.Firstname,
@ -85,7 +71,13 @@ func (s Service) getUserByID(param iByID) (*oUser, api.Error) {
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) createUser(param iCreate) (*oUser, api.Error) { type createRequest struct {
Username string
Firstname string
Lastname string
}
func (s Service) createUser(param createRequest) (*userInfo, api.Error) {
user := model.User{ user := model.User{
Username: param.Username, Username: param.Username,
Firstname: param.Firstname, Firstname: param.Firstname,
@ -97,7 +89,7 @@ func (s Service) createUser(param iCreate) (*oUser, api.Error) {
return nil, api.ErrorNoMatchFound return nil, api.ErrorNoMatchFound
} }
return &oUser{ return &userInfo{
ID: user.ID, ID: user.ID,
Username: user.Username, Username: user.Username,
Firstname: user.Firstname, Firstname: user.Firstname,
@ -106,7 +98,14 @@ func (s Service) createUser(param iCreate) (*oUser, api.Error) {
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) updateUser(param iUpdate) (*oUser, api.Error) { type updateRequest struct {
ID uint
Username *string
Firstname *string
Lastname *string
}
func (s Service) updateUser(param updateRequest) (*userInfo, 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
@ -128,7 +127,7 @@ func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
return nil, api.ErrorFailure return nil, api.ErrorFailure
} }
return &oUser{ return &userInfo{
ID: user.ID, ID: user.ID,
Username: user.Username, Username: user.Username,
Firstname: user.Firstname, Firstname: user.Firstname,
@ -137,7 +136,7 @@ func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
}, api.ErrorSuccess }, api.ErrorSuccess
} }
func (s Service) deleteUser(param iByID) api.Error { func (s Service) deleteUser(param struct{ ID uint }) api.Error {
user := model.User{ user := model.User{
ID: param.ID, ID: param.ID,
} }