134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
|
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{})
|
||
|
}
|
||
|
|
||
|
server.HandleFunc(http.MethodGet, "/users", s.getAllUsers)
|
||
|
server.HandleFunc(http.MethodGet, "/user/{id}", s.getUserByID)
|
||
|
server.HandleFunc(http.MethodPost, "/user", s.createUser)
|
||
|
server.HandleFunc(http.MethodPut, "/user/{id}", s.updateUser)
|
||
|
server.HandleFunc(http.MethodDelete, "/user/{id}", s.deleteUser)
|
||
|
}
|
||
|
|
||
|
func (s Service) getAllUsers(req api.Request, res *api.Response) {
|
||
|
users := make([]model.User, 0)
|
||
|
s.DB.Find(&users)
|
||
|
res.SetData("users", users)
|
||
|
res.SetError(api.ErrorSuccess())
|
||
|
}
|
||
|
func (s Service) getUserByID(req api.Request, res *api.Response) {
|
||
|
id, err := req.Param.GetUint("user_id")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var user model.User
|
||
|
if s.DB.First(&user, id).RecordNotFound() {
|
||
|
res.SetError(api.ErrorNoMatchFound())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
res.SetError(api.ErrorSuccess())
|
||
|
}
|
||
|
|
||
|
func (s Service) createUser(req api.Request, res *api.Response) {
|
||
|
var user model.User
|
||
|
var err error
|
||
|
user.Username, err = req.Param.GetString("username")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "username")
|
||
|
return
|
||
|
}
|
||
|
user.Firstname, err = req.Param.GetString("firstname")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "firstname")
|
||
|
return
|
||
|
}
|
||
|
user.Lastname, err = req.Param.GetString("lastname")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "lastname")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
s.DB.Create(&user)
|
||
|
if s.DB.Last(&user).RecordNotFound() {
|
||
|
res.SetError(api.ErrorNoMatchFound())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
res.SetError(api.ErrorSuccess())
|
||
|
}
|
||
|
|
||
|
func (s Service) updateUser(req api.Request, res *api.Response) {
|
||
|
id, err := req.Param.GetUint("user_id")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var user model.User
|
||
|
if s.DB.First(&user, id).RecordNotFound() {
|
||
|
res.SetError(api.ErrorNoMatchFound())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 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 {
|
||
|
res.SetError(api.ErrorFailure())
|
||
|
return
|
||
|
}
|
||
|
res.SetError(api.ErrorSuccess())
|
||
|
}
|
||
|
|
||
|
func (s Service) deleteUser(req api.Request, res *api.Response) {
|
||
|
id, err := req.Param.GetUint("user_id")
|
||
|
if err != nil {
|
||
|
res.SetError(api.ErrorInvalidParam(), "user_id")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
user := model.User{}
|
||
|
user.ID = id
|
||
|
s.DB.Delete(&user)
|
||
|
res.SetError(api.ErrorSuccess())
|
||
|
}
|