-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuser.go
71 lines (63 loc) · 1.96 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/goadesign/goa"
"github.com/gopheracademy/congo/app"
"github.com/gopheracademy/congo/models"
"github.com/jinzhu/gorm"
)
// UserController implements theuser resource.
type UserController struct {
*goa.Controller
e *Env
storagelist map[string]Storage
storage models.UserStorage
}
// NewUserController creates a user controller.
func NewUserController(service *goa.Service, storagelist map[string]Storage, e *Env) *UserController {
return &UserController{
Controller: service.NewController("UserController"),
e: e,
storagelist: storagelist,
storage: storagelist["USERSTORAGE"].(models.UserStorage),
}
}
// Create runs the create action.
func (c *UserController) Create(ctx *app.CreateUserContext) error {
a := models.UserFromCreateUserPayload(ctx.Payload)
ra, err := c.storage.Add(ctx.Context, a)
if err != nil {
return ctx.Service.Send(ctx, 500, err.Error())
}
ctx.ResponseData.Header().Set("Location", app.UserHref(ctx.TenantID, ra.ID))
return ctx.Created()
}
// Delete runs the delete action.
func (c *UserController) Delete(ctx *app.DeleteUserContext) error {
err := c.storage.Delete(ctx.Context, ctx.UserID)
if err != nil {
return ctx.NotFound()
}
return ctx.NoContent()
}
// List runs the list action.
func (c *UserController) List(ctx *app.ListUserContext) error {
res := c.storage.ListUser(ctx.Context, ctx.TenantID)
return ctx.OK(res)
}
// Show runs the show action.
func (c *UserController) Show(ctx *app.ShowUserContext) error {
res, err := c.storage.OneUser(ctx.Context, ctx.UserID, ctx.TenantID)
if err != nil && err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
return ctx.OK(res)
}
// Update runs the update action.
func (c *UserController) Update(ctx *app.UpdateUserContext) error {
err := c.storage.UpdateFromUpdateUserPayload(ctx.Context, ctx.Payload, ctx.UserID)
if err != nil {
goa.LogError(ctx, "updating user", err.Error())
return ctx.Err()
}
return ctx.NoContent()
}