-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount.go
75 lines (65 loc) · 1.96 KB
/
account.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
72
73
74
75
package main
import (
"github.com/goadesign/goa"
"github.com/goadesign/gorma-cellar/app"
"github.com/goadesign/gorma-cellar/models"
"github.com/jinzhu/gorm"
)
// ErrDatabaseError is the error returned when a db query fails.
var ErrDatabaseError = goa.NewErrorClass("db_error", 500)
// AccountController implements the account resource.
type AccountController struct {
*goa.Controller
}
// NewAccountController creates a account controller.
func NewAccountController(service *goa.Service) *AccountController {
return &AccountController{Controller: service.NewController("account")}
}
// Create runs the create action.
func (c *AccountController) Create(ctx *app.CreateAccountContext) error {
a := models.Account{}
a.Name = ctx.Payload.Name
err := adb.Add(ctx.Context, &a)
if err != nil {
return ErrDatabaseError(err)
}
ctx.ResponseData.Header().Set("Location", app.AccountHref(a.ID))
return ctx.Created()
}
// Delete runs the delete action.
func (c *AccountController) Delete(ctx *app.DeleteAccountContext) error {
err := adb.Delete(ctx.Context, ctx.AccountID)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.NoContent()
}
// Show runs the show action.
func (c *AccountController) Show(ctx *app.ShowAccountContext) error {
account, err := adb.OneAccount(ctx.Context, ctx.AccountID)
if err == gorm.ErrRecordNotFound {
return ctx.NotFound()
} else if err != nil {
return ErrDatabaseError(err)
}
account.Href = app.AccountHref(account.ID)
return ctx.OK(account)
}
// List
func (c *AccountController) List(ctx *app.ListAccountContext) error {
accounts := adb.ListAccount(ctx.Context)
return ctx.OK(accounts)
}
// Update runs the update action.
func (c *AccountController) Update(ctx *app.UpdateAccountContext) error {
m, err := adb.Get(ctx.Context, ctx.AccountID)
if err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
m.Name = ctx.Payload.Name
err = adb.Update(ctx, m)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.NoContent()
}