-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles.go
75 lines (67 loc) · 1.6 KB
/
roles.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 controllers
/*
import (
"net/http"
"github.com/denisbakhtin/projectmanager/models"
"github.com/gin-gonic/gin"
)
//rolesGet handles get all roles request
func rolesGet(c *gin.Context) {
var roles []models.Role
models.DB.Find(&roles)
c.JSON(http.StatusOK, roles)
}
//roleGet handles get role request
func roleGet(c *gin.Context) {
id := c.Param("id")
role := models.Role{}
models.DB.First(&role, id)
if role.ID == 0 {
c.JSON(http.StatusNotFound, "Role not found")
return
}
c.JSON(http.StatusOK, role)
}
//rolesPost handles create role request
func rolesPost(c *gin.Context) {
role := models.Role{}
if err := c.ShouldBindJSON(&role); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
if err := models.DB.Create(&role).Error; err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{})
}
//rolesPut handles update role request
func rolesPut(c *gin.Context) {
//id := c.Param("id")
role := models.Role{}
if err := c.ShouldBindJSON(&role); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
if err := models.DB.Save(&role).Error; err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{})
}
//rolesDelete handles delete role request
func rolesDelete(c *gin.Context) {
id := c.Param("id")
role := models.Role{}
models.DB.First(&role, id)
if role.ID == 0 {
c.JSON(http.StatusNotFound, "Role not found")
return
}
if err := models.DB.Delete(&role).Error; err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{})
}
*/