2020-03-22 12:49:08 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.xdrm.io/go/aicra"
|
|
|
|
"git.xdrm.io/go/aicra/api"
|
|
|
|
"git.xdrm.io/go/tiny-url-ex/service/model"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service to manage users
|
|
|
|
type Service struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wire services to their paths
|
2020-03-29 15:03:53 +00:00
|
|
|
func (s Service) Wire(server *aicra.Server) error {
|
2020-03-22 12:49:08 +00:00
|
|
|
if !s.DB.HasTable(&model.User{}) {
|
|
|
|
s.DB.CreateTable(&model.User{})
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
if err := server.Handle(http.MethodGet, "/user/{id}", s.getUserByID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodGet, "/users", s.getAllUsers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodPost, "/user", s.createUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodPut, "/user/{id}", s.updateUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := server.Handle(http.MethodDelete, "/user/{id}", s.deleteUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
type oUserList struct {
|
|
|
|
Users []model.User
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
type oUser struct {
|
|
|
|
ID uint
|
|
|
|
Username string
|
|
|
|
Firstname string
|
|
|
|
Lastname string
|
|
|
|
Articles interface{}
|
|
|
|
}
|
|
|
|
type iByID struct {
|
|
|
|
ID uint
|
|
|
|
}
|
2020-03-22 12:49:08 +00:00
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
type iCreate struct {
|
|
|
|
Username string
|
|
|
|
Firstname string
|
|
|
|
Lastname string
|
|
|
|
}
|
|
|
|
type iUpdate struct {
|
|
|
|
ID uint
|
|
|
|
Username *string
|
|
|
|
Firstname *string
|
|
|
|
Lastname *string
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
func (s Service) getAllUsers() (*oUserList, api.Error) {
|
|
|
|
users := oUserList{}
|
|
|
|
s.DB.Find(&users.Users)
|
|
|
|
return &users, api.ErrorSuccess
|
|
|
|
}
|
|
|
|
func (s Service) getUserByID(param iByID) (*oUser, api.Error) {
|
2020-03-22 12:49:08 +00:00
|
|
|
var user model.User
|
2020-03-29 15:03:53 +00:00
|
|
|
if s.DB.First(&user, param.ID).RecordNotFound() {
|
|
|
|
return nil, api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-29 15:03:53 +00:00
|
|
|
|
|
|
|
return &oUser{
|
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
Firstname: user.Firstname,
|
|
|
|
Lastname: user.Lastname,
|
|
|
|
Articles: user.Articles,
|
|
|
|
}, api.ErrorSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) createUser(param iCreate) (*oUser, api.Error) {
|
|
|
|
user := model.User{
|
|
|
|
Username: param.Username,
|
|
|
|
Firstname: param.Firstname,
|
|
|
|
Lastname: param.Lastname,
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.DB.Create(&user)
|
|
|
|
if s.DB.Last(&user).RecordNotFound() {
|
2020-03-29 15:03:53 +00:00
|
|
|
return nil, api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
return &oUser{
|
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
Firstname: user.Firstname,
|
|
|
|
Lastname: user.Lastname,
|
|
|
|
Articles: user.Articles,
|
|
|
|
}, api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
func (s Service) updateUser(param iUpdate) (*oUser, api.Error) {
|
2020-03-22 12:49:08 +00:00
|
|
|
var user model.User
|
2020-03-29 15:03:53 +00:00
|
|
|
if s.DB.First(&user, param.ID).RecordNotFound() {
|
|
|
|
return nil, api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// override with updated values
|
2020-03-29 15:03:53 +00:00
|
|
|
if param.Username != nil {
|
|
|
|
user.Username = *param.Username
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-29 15:03:53 +00:00
|
|
|
if param.Firstname != nil {
|
|
|
|
user.Firstname = *param.Firstname
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-29 15:03:53 +00:00
|
|
|
if param.Lastname != nil {
|
|
|
|
user.Lastname = *param.Lastname
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
if s.DB.Save(&user).RowsAffected < 1 {
|
2020-03-29 15:03:53 +00:00
|
|
|
return nil, api.ErrorFailure
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-29 15:03:53 +00:00
|
|
|
|
|
|
|
return &oUser{
|
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
Firstname: user.Firstname,
|
|
|
|
Lastname: user.Lastname,
|
|
|
|
Articles: user.Articles,
|
|
|
|
}, api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:03:53 +00:00
|
|
|
func (s Service) deleteUser(param iByID) api.Error {
|
|
|
|
user := model.User{
|
|
|
|
ID: param.ID,
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
s.DB.Delete(&user)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|