forked from fabric8-services/fabric8-wit
/
userspace.go
85 lines (72 loc) · 2.22 KB
/
userspace.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
package main
import (
"fmt"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/gormsupport"
"github.com/almighty/almighty-core/models"
"github.com/almighty/almighty-core/workitem"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
// Data describes a single resource in usersapce
type Data struct {
gormsupport.Lifecycle
ID uuid.UUID `sql:"type:uuid default uuid_generate_v4()" gorm:"primary_key"` // This is the ID PK field
Path string
Data workitem.Fields `sql:"type:jsonb"`
}
// TableName implements gorm.tabler
func (w Data) TableName() string {
return "userspace_data"
}
// UserspaceController implements the userspace resource.
type UserspaceController struct {
*goa.Controller
db *gorm.DB
}
// NewUserspaceController creates a userspace controller.
func NewUserspaceController(service *goa.Service, db *gorm.DB) *UserspaceController {
db.AutoMigrate(&Data{}).AddUniqueIndex("idx_userspace_path", "path")
return &UserspaceController{Controller: service.NewController("UserspaceController"), db: db}
}
// Create runs the create action.
func (c *UserspaceController) Create(ctx *app.CreateUserspaceContext) error {
return models.Transactional(c.db, func(db *gorm.DB) error {
path := ctx.RequestURI
data := Data{}
err := c.db.Where("path = ?", path).First(&data).Error
fmt.Println(err)
if err != nil {
data = Data{
ID: uuid.NewV4(),
Path: ctx.RequestURI,
Data: workitem.Fields(ctx.Payload),
}
err := c.db.Create(&data).Error
if err != nil {
goa.LogError(ctx, "error adding data", "error", err.Error())
return ctx.InternalServerError()
}
} else {
err := c.db.Model(&data).Update("data", workitem.Fields(ctx.Payload)).Error
if err != nil {
goa.LogError(ctx, "error updating data", "error", err.Error())
return ctx.InternalServerError()
}
}
return ctx.NoContent()
})
}
// Show shows the record
func (c *UserspaceController) Show(ctx *app.ShowUserspaceContext) error {
return models.Transactional(c.db, func(db *gorm.DB) error {
path := ctx.RequestURI
data := Data{}
err := c.db.Where("path = ?", path).First(&data).Error
if err != nil {
return ctx.NotFound()
}
return ctx.OK(data.Data)
})
}