make service structs more readable
This commit is contained in:
parent
f31adca326
commit
56cd3ff26c
2
go.mod
2
go.mod
|
@ -5,6 +5,6 @@ go 1.14
|
|||
require (
|
||||
git.xdrm.io/go/aicra v0.3.0
|
||||
github.com/jinzhu/gorm v1.9.12
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
|
||||
)
|
||||
|
||||
replace git.xdrm.io/go/aicra => ../aicra
|
||||
|
|
|
@ -38,49 +38,41 @@ func (s Service) Wire(server *aicra.Server) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type iByID struct {
|
||||
ID uint
|
||||
}
|
||||
|
||||
type oArticle struct {
|
||||
type articleInfo struct {
|
||||
ID uint
|
||||
Author uint
|
||||
Title string
|
||||
Body string
|
||||
Score uint
|
||||
}
|
||||
type iCreate struct {
|
||||
Title string
|
||||
Body string
|
||||
}
|
||||
|
||||
type oArticleList struct {
|
||||
type articleList struct {
|
||||
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)
|
||||
s.DB.Where("author = ?", param.ID).Find(&articles)
|
||||
return &oArticleList{
|
||||
return &articleList{
|
||||
Articles: articles,
|
||||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
func (s Service) getAllArticles() (*oArticleList, api.Error) {
|
||||
func (s Service) getAllArticles() (*articleList, api.Error) {
|
||||
articles := make([]model.Article, 0)
|
||||
s.DB.Find(&articles)
|
||||
return &oArticleList{
|
||||
return &articleList{
|
||||
Articles: articles,
|
||||
}, 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
|
||||
if s.DB.First(&article, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
return &oArticle{
|
||||
return &articleInfo{
|
||||
ID: article.ID,
|
||||
Title: article.Title,
|
||||
Body: article.Body,
|
||||
|
@ -88,11 +80,16 @@ func (s Service) getArticleByID(param iByID) (*oArticle, api.Error) {
|
|||
}, 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
|
||||
}
|
||||
|
||||
func (s Service) deleteArticle(param iByID) api.Error {
|
||||
func (s Service) deleteArticle(param struct{ ID uint }) api.Error {
|
||||
article := model.Article{
|
||||
ID: param.ID,
|
||||
}
|
||||
|
|
|
@ -38,45 +38,31 @@ func (s Service) Wire(server *aicra.Server) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type oUserList struct {
|
||||
Users []model.User
|
||||
}
|
||||
|
||||
type oUser struct {
|
||||
type userInfo struct {
|
||||
ID uint
|
||||
Username string
|
||||
Firstname string
|
||||
Lastname string
|
||||
Articles interface{}
|
||||
}
|
||||
type iByID struct {
|
||||
ID uint
|
||||
|
||||
type userList struct {
|
||||
Users []model.User
|
||||
}
|
||||
|
||||
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{}
|
||||
func (s Service) getAllUsers() (*userList, api.Error) {
|
||||
users := userList{}
|
||||
s.DB.Find(&users.Users)
|
||||
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
|
||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
return &oUser{
|
||||
return &userInfo{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
|
@ -85,7 +71,13 @@ func (s Service) getUserByID(param iByID) (*oUser, api.Error) {
|
|||
}, 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{
|
||||
Username: param.Username,
|
||||
Firstname: param.Firstname,
|
||||
|
@ -97,7 +89,7 @@ func (s Service) createUser(param iCreate) (*oUser, api.Error) {
|
|||
return nil, api.ErrorNoMatchFound
|
||||
}
|
||||
|
||||
return &oUser{
|
||||
return &userInfo{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
|
@ -106,7 +98,14 @@ func (s Service) createUser(param iCreate) (*oUser, api.Error) {
|
|||
}, 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
|
||||
if s.DB.First(&user, param.ID).RecordNotFound() {
|
||||
return nil, api.ErrorNoMatchFound
|
||||
|
@ -128,7 +127,7 @@ func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
|
|||
return nil, api.ErrorFailure
|
||||
}
|
||||
|
||||
return &oUser{
|
||||
return &userInfo{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Firstname: user.Firstname,
|
||||
|
@ -137,7 +136,7 @@ func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
|
|||
}, api.ErrorSuccess
|
||||
}
|
||||
|
||||
func (s Service) deleteUser(param iByID) api.Error {
|
||||
func (s Service) deleteUser(param struct{ ID uint }) api.Error {
|
||||
user := model.User{
|
||||
ID: param.ID,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue