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
|
|
|
|
func (s Service) Wire(server *aicra.Server) {
|
|
|
|
if !s.DB.HasTable(&model.User{}) {
|
|
|
|
s.DB.CreateTable(&model.User{})
|
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
server.Handle(http.MethodGet, "/users", s.getAllUsers)
|
|
|
|
server.Handle(http.MethodGet, "/user/{id}", s.getUserByID)
|
|
|
|
server.Handle(http.MethodPost, "/user", s.createUser)
|
|
|
|
server.Handle(http.MethodPut, "/user/{id}", s.updateUser)
|
|
|
|
server.Handle(http.MethodDelete, "/user/{id}", s.deleteUser)
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) getAllUsers(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
users := make([]model.User, 0)
|
|
|
|
s.DB.Find(&users)
|
|
|
|
res.SetData("users", users)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) getUserByID(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("user_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var user model.User
|
|
|
|
if s.DB.First(&user, id).RecordNotFound() {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.SetData("id", user.ID)
|
|
|
|
res.SetData("username", user.Username)
|
|
|
|
res.SetData("firstname", user.Firstname)
|
|
|
|
res.SetData("lastname", user.Lastname)
|
|
|
|
res.SetData("articles", user.Articles)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) createUser(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
var user model.User
|
|
|
|
var err error
|
|
|
|
user.Username, err = req.Param.GetString("username")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
user.Firstname, err = req.Param.GetString("firstname")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
user.Lastname, err = req.Param.GetString("lastname")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.DB.Create(&user)
|
|
|
|
if s.DB.Last(&user).RecordNotFound() {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.SetData("id", user.ID)
|
|
|
|
res.SetData("username", user.Username)
|
|
|
|
res.SetData("firstname", user.Firstname)
|
|
|
|
res.SetData("lastname", user.Lastname)
|
|
|
|
res.SetData("articles", user.Articles)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) updateUser(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("user_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var user model.User
|
|
|
|
if s.DB.First(&user, id).RecordNotFound() {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorNoMatchFound
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// override with updated values
|
|
|
|
if updated, err := req.Param.GetString("username"); err == nil {
|
|
|
|
user.Username = updated
|
|
|
|
}
|
|
|
|
if updated, err := req.Param.GetString("firstname"); err == nil {
|
|
|
|
user.Firstname = updated
|
|
|
|
}
|
|
|
|
if updated, err := req.Param.GetString("lastname"); err == nil {
|
|
|
|
user.Lastname = updated
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
if s.DB.Save(&user).RowsAffected < 1 {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorFailure
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:02:42 +00:00
|
|
|
func (s Service) deleteUser(req api.Request, res *api.Response) api.Error {
|
2020-03-22 12:49:08 +00:00
|
|
|
id, err := req.Param.GetUint("user_id")
|
|
|
|
if err != nil {
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorInvalidParam
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := model.User{}
|
|
|
|
user.ID = id
|
|
|
|
s.DB.Delete(&user)
|
2020-03-28 14:02:42 +00:00
|
|
|
return api.ErrorSuccess
|
2020-03-22 12:49:08 +00:00
|
|
|
}
|