/
admin_jpnic_admin.go
104 lines (88 loc) · 2.86 KB
/
admin_jpnic_admin.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
package v0
import (
"github.com/gin-gonic/gin"
"github.com/homenoc/dsbd-backend/pkg/api/core"
auth "github.com/homenoc/dsbd-backend/pkg/api/core/auth/v0"
"github.com/homenoc/dsbd-backend/pkg/api/core/common"
"github.com/homenoc/dsbd-backend/pkg/api/core/group/service/jpnicAdmin"
dbJPNICAdmin "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/jpnicAdmin/v0"
dbService "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/v0"
"gorm.io/gorm"
"log"
"net/http"
"strconv"
)
func AddJPNICAdminByAdmin(c *gin.Context) {
var input core.JPNICAdmin
resultAdmin := auth.AdminAuthorization(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()})
return
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}
err = c.BindJSON(&input)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}
if err = dbService.JoinJPNICByAdmin(uint(id), input); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}
noticeAddJPNICByAdmin(id, input)
c.JSON(http.StatusOK, common.Result{})
}
func DeleteJPNICAdminByAdmin(c *gin.Context) {
resultAdmin := auth.AdminAuthorization(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()})
return
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}
if err = dbService.DeleteJPNICByAdmin(uint(id)); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}
noticeDelete("JPNIC管理者連絡窓口", uint(id))
c.JSON(http.StatusOK, common.Result{})
}
func UpdateJPNICAdminByAdmin(c *gin.Context) {
var input core.JPNICAdmin
resultAdmin := auth.AdminAuthorization(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()})
return
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}
err = c.BindJSON(&input)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}
before := dbJPNICAdmin.Get(jpnicAdmin.ID, &core.JPNICAdmin{Model: gorm.Model{ID: uint(id)}})
if before.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: before.Err.Error()})
return
}
input.ID = uint(id)
if err = dbService.UpdateJPNICByAdmin(input); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}
noticeUpdateJPNICByAdmin(before.Admins[0], input)
c.JSON(http.StatusOK, common.Result{})
}