-
Notifications
You must be signed in to change notification settings - Fork 0
/
c-projects.go
122 lines (109 loc) · 2.64 KB
/
c-projects.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
110
111
112
113
114
115
116
117
118
119
120
121
122
package donate
import (
"net/http"
"time"
"github.com/chonglou/arche/plugins/nut"
"github.com/chonglou/arche/web/mux"
)
func (p *Plugin) indexProjects(c *mux.Context) {
var items []Project
if err := p.DB.Model(&items).
Column("id", "title").
Order("updated_at DESC").
Select(); err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, items)
}
type fmProject struct {
Title string `json:"title" binding:"required"`
Body string `json:"body" binding:"required"`
Type string `json:"type" binding:"required"`
Methods string `json:"methods" binding:"required"`
}
func (p *Plugin) createProject(c *mux.Context) {
user, err := p.Layout.CurrentUser(c)
if err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
var fm fmProject
if err := c.BindJSON(&fm); err != nil {
c.Abort(http.StatusBadRequest, err)
return
}
it := Project{
Title: fm.Title,
Body: fm.Body,
Type: fm.Type,
Methods: fm.Methods,
UserID: user.ID,
UpdatedAt: time.Now(),
}
if err := p.DB.Insert(&it); err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, it)
}
func (p *Plugin) showProject(c *mux.Context) {
var it Project
if err := p.DB.Model(&it).Where("id = ?", c.Param("id")).
Select(); err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, it)
}
func (p *Plugin) updateProject(c *mux.Context) {
var fm fmProject
if err := c.BindJSON(&fm); err != nil {
c.Abort(http.StatusBadRequest, err)
return
}
it, err := p.canEditProject(c)
if err != nil {
c.Abort(http.StatusForbidden, err)
return
}
it.Title = fm.Title
it.Body = fm.Body
it.Type = fm.Type
it.Methods = fm.Methods
it.UpdatedAt = time.Now()
if _, err := p.DB.Model(it).
Column("title", "body", "type", "methods", "updated_at").
Update(); err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, mux.H{})
}
func (p *Plugin) destroyProject(c *mux.Context) {
it, err := p.canEditProject(c)
if err != nil {
c.Abort(http.StatusInternalServerError, err)
return
}
if err := p.DB.Delete(it); err != nil {
c.Abort(http.StatusInternalServerError, err)
}
c.JSON(http.StatusOK, mux.H{})
}
func (p *Plugin) canEditProject(c *mux.Context) (*Project, error) {
var it Project
if err := p.DB.Model(&it).Where("id = ?", c.Param("id")).
Select(); err != nil {
return nil, err
}
l := c.Locale()
user, err := p.Layout.CurrentUser(c)
if err != nil {
return nil, err
}
if it.UserID != user.ID && p.Dao.Is(p.DB, user.ID, nut.RoleAdmin) {
return nil, p.I18n.E(l, "errors.not-allow")
}
return &it, nil
}