-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
109 lines (91 loc) · 2.6 KB
/
accounts.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package controller
import (
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
"github.com/pei0804/goa-simple-sample/app"
"github.com/pei0804/goa-simple-sample/models"
)
// AccountsController implements the accounts resource.
type AccountsController struct {
*goa.Controller
db *gorm.DB
}
// NewAccountsController creates a accounts controller.
func NewAccountsController(service *goa.Service, db *gorm.DB) *AccountsController {
return &AccountsController{
Controller: service.NewController("AccountsController"),
db: db,
}
}
// Add runs the add action.
func (c *AccountsController) Add(ctx *app.AddAccountsContext) error {
// AccountsController_Add: start_implement
// Put your logic here
a := &models.Account{}
a.Name = ctx.Payload.Name
a.Email = ctx.Payload.Email
adb := models.NewAccountDB(c.db)
err := adb.Add(ctx.Context, a)
if err != nil {
ctx.BadRequest(err)
}
// AccountsController_Add: end_implement
res := &app.Account{}
res = a.AccountToAccount()
return ctx.OK(res)
}
// Delete runs the delete action.
func (c *AccountsController) Delete(ctx *app.DeleteAccountsContext) error {
// AccountsController_Delete: start_implement
// Put your logic here
adb := models.NewAccountDB(c.db)
err := adb.Delete(ctx.Context, ctx.ID)
if err != nil {
ctx.BadRequest(err)
}
// AccountsController_Delete: end_implement
return nil
}
// List runs the list action.
func (c *AccountsController) List(ctx *app.ListAccountsContext) error {
// AccountsController_List: start_implement
// Put your logic here
adb := models.NewAccountDB(c.db)
a := adb.ListAccount(ctx.Context)
// AccountsController_List: end_implement
res := app.AccountCollection{}
res = a
return ctx.OK(res)
}
// Show runs the show action.
func (c *AccountsController) Show(ctx *app.ShowAccountsContext) error {
// AccountsController_Show: start_implement
// Put your logic here
adb := models.NewAccountDB(c.db)
a, err := adb.OneAccount(ctx, ctx.ID)
if err != nil {
return ctx.NotFound()
}
// AccountsController_Show: end_implement
res := &app.Account{}
res = a
return ctx.OK(res)
}
// Update runs the update action.
func (c *AccountsController) Update(ctx *app.UpdateAccountsContext) error {
// AccountsController_Update: start_implement
// Put your logic here
a := &models.Account{}
a.ID = ctx.ID
a.Name = ctx.Payload.Name
a.Email = ctx.Payload.Email
adb := models.NewAccountDB(c.db)
err := adb.Update(ctx.Context, a)
if err == gorm.ErrRecordNotFound {
return ctx.NotFound()
} else if err != nil {
return ctx.BadRequest(goa.ErrBadRequest(err))
}
// AccountsController_Update: end_implement
return nil
}