-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspeaker.go
71 lines (63 loc) · 2.06 KB
/
speaker.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"
)
// SpeakerController implements theuser resource.
type SpeakerController struct {
*goa.Controller
e *Env
storagelist map[string]Storage
storage models.SpeakerStorage
}
// NewSpeakerController creates a user controller.
func NewSpeakerController(service *goa.Service, storagelist map[string]Storage, e *Env) *SpeakerController {
return &SpeakerController{
Controller: service.NewController("SpeakerController"),
e: e,
storagelist: storagelist,
storage: storagelist["SPEAKERSTORAGE"].(models.SpeakerStorage),
}
}
// Create runs the create action.
func (c *SpeakerController) Create(ctx *app.CreateSpeakerContext) error {
a := models.SpeakerFromCreateSpeakerPayload(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.SpeakerHref(ctx.TenantID, ctx.EventID, ra.ID))
return ctx.Created()
}
// Delete runs the delete action.
func (c *SpeakerController) Delete(ctx *app.DeleteSpeakerContext) error {
err := c.storage.Delete(ctx.Context, ctx.SpeakerID)
if err != nil {
return ctx.NotFound()
}
return ctx.NoContent()
}
// List runs the list action.
func (c *SpeakerController) List(ctx *app.ListSpeakerContext) error {
res := c.storage.ListSpeaker(ctx.Context, ctx.TenantID)
return ctx.OK(res)
}
// Show runs the show action.
func (c *SpeakerController) Show(ctx *app.ShowSpeakerContext) error {
res, err := c.storage.OneSpeaker(ctx.Context, ctx.SpeakerID, ctx.TenantID)
if err != nil && err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
return ctx.OK(res)
}
// Update runs the update action.
func (c *SpeakerController) Update(ctx *app.UpdateSpeakerContext) error {
err := c.storage.UpdateFromUpdateSpeakerPayload(ctx.Context, ctx.Payload, ctx.SpeakerID)
if err != nil {
goa.LogError(ctx, "updating user", err.Error())
return ctx.Err()
}
return ctx.NoContent()
}