5
5
"OPP/backend/auth"
6
6
"OPP/backend/dao"
7
7
"net/http"
8
- "strconv"
9
8
10
9
"github.com/gin-gonic/gin"
11
10
)
@@ -20,12 +19,7 @@ func NewTotemHandler() *TotemHandlers {
20
19
}
21
20
}
22
21
23
- func (th * TotemHandlers ) GetTotemConfig (c * gin.Context , params api.GetTotemConfigParams ) {
24
- id , err := strconv .ParseInt (params .Id , 10 , 64 )
25
- if err != nil || id <= 0 {
26
- c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid totem ID" })
27
- return
28
- }
22
+ func (th * TotemHandlers ) GetTotemConfig (c * gin.Context , id string ) {
29
23
err , totemConfig := th .dao .GetTotemById (c .Request .Context (), id )
30
24
if err != nil {
31
25
if err == dao .ErrTotemNotFound {
@@ -51,9 +45,68 @@ func (th *TotemHandlers) RegisterTotem(c *gin.Context) {
51
45
return
52
46
}
53
47
48
+ // Check if zone exists
49
+ _ , err := dao .NewZoneDao ().GetZoneById (c .Request .Context (), totemRequest .ZoneId )
50
+ if err != nil {
51
+ if err == dao .ErrZoneNotFound {
52
+ c .JSON (http .StatusNotFound , gin.H {"error" : "zone not found" })
53
+ return
54
+ }
55
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to check if zone exists" })
56
+ return
57
+ }
58
+
54
59
if err := th .dao .AddTotem (c .Request .Context (), totemRequest ); err != nil {
60
+ if err == dao .ErrTotemAlreadyExists {
61
+ c .JSON (http .StatusConflict , gin.H {"error" : "totem already exists" })
62
+ return
63
+ }
55
64
c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to register totem" })
56
65
return
57
66
}
58
67
c .JSON (http .StatusOK , gin.H {"message" : "totem registered successfully" })
59
68
}
69
+
70
+ func (th * TotemHandlers ) GetAllTotems (c * gin.Context , params api.GetAllTotemsParams ) {
71
+ totems , err := th .dao .GetTotems (c .Request .Context (), * params .Limit , * params .Offset )
72
+ if err != nil {
73
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to get totems" })
74
+ return
75
+ }
76
+ c .JSON (http .StatusOK , totems )
77
+ }
78
+
79
+ func (th * TotemHandlers ) DeleteTotemById (c * gin.Context , id string ) {
80
+ username , role , err := auth .GetPermissions (c )
81
+ if err != nil {
82
+ return
83
+ }
84
+
85
+ // Get totem by ID
86
+ err , totem := th .dao .GetTotemById (c .Request .Context (), id )
87
+ if err != nil {
88
+ if err == dao .ErrTotemNotFound {
89
+ c .JSON (http .StatusNotFound , gin.H {"error" : "totem not found" })
90
+ return
91
+ }
92
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to get totem" })
93
+ return
94
+ }
95
+ isZoneAdmin , err := NewZoneHandler ().isZoneAdmin (c , totem .ZoneId , username )
96
+
97
+ if role != "superuser" && ! isZoneAdmin {
98
+ c .JSON (http .StatusForbidden , gin.H {"forbidden" : "you do not have permission to delete this totem" })
99
+ return
100
+ }
101
+
102
+ err = th .dao .DeleteTotemById (c .Request .Context (), id )
103
+ if err != nil {
104
+ if err == dao .ErrTotemNotFound {
105
+ c .JSON (http .StatusNotFound , gin.H {"error" : "totem not found" })
106
+ return
107
+ }
108
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to delete totem" })
109
+ return
110
+ }
111
+ c .JSON (http .StatusOK , gin.H {"message" : "totem deleted successfully" })
112
+ }
0 commit comments