/
signups.go
145 lines (128 loc) · 4.3 KB
/
signups.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright ADH Partnership
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package event
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/adh-partnership/api/pkg/database"
"github.com/adh-partnership/api/pkg/database/dto"
"github.com/adh-partnership/api/pkg/database/models"
"github.com/adh-partnership/api/pkg/gin/response"
)
// Create/Edit User Signup for Event
// @Summary Create/Edit User Signup for Event
// @Description Create/Edit User Signup for Event. This will only work for the logged in user.
// @Tags Events
// @Param id path string true "Event ID"
// @Param signup body dto.EventSignupRequest true "Signup"
// @Success 200 {object} models.Event
// @Failure 400 {object} response.R
// @Failure 403 {object} response.R
// @Failure 404 {object} response.R
// @Failure 500 {object} response.R
// @Router /v1/events/{id}/signup [post]
func postEventSignup(c *gin.Context) {
event, err := database.GetEvent(c.Param("id"))
if err != nil {
log.Errorf("Error getting event: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if event == nil {
response.RespondError(c, http.StatusNotFound, "Not Found")
return
}
data := &dto.EventSignupRequest{}
if err := c.ShouldBind(&data); err != nil {
response.RespondError(c, http.StatusBadRequest, "Invalid request")
return
}
user := c.MustGet("x-user").(*models.User)
signup := &models.EventSignup{}
if err := database.DB.Where("event_id = ? AND user_id = ?", event.ID, user.CID).First(signup).Error; err != nil {
if err == gorm.ErrRecordNotFound {
signup := &models.EventSignup{
EventID: event.ID,
User: user,
Choice1: data.Choice1,
Choice2: data.Choice2,
Choice3: data.Choice3,
Notes: data.Notes,
}
if err := database.DB.Create(signup).Error; err != nil {
log.Errorf("Error creating event signup: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
} else {
log.Errorf("Error getting event signup: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
} else {
signup.Choice1 = data.Choice1
signup.Choice2 = data.Choice2
signup.Choice3 = data.Choice3
signup.Notes = data.Notes
if err := database.DB.Save(&signup).Error; err != nil {
log.Errorf("Error updating event signup: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
}
event, _ = database.GetEvent(c.Param("id"))
response.Respond(c, http.StatusOK, dto.ConvEventToEventsResponse(event))
}
// Delete User Signup
// @Summary Delete User Signup
// @Description Delete User Signup. This will only work for the logged in user.
// @Tags Events
// @Param id path string true "Event ID"
// @Success 204
// @Failure 403 {object} response.R
// @Failure 404 {object} response.R
// @Failure 500 {object} response.R
// @Router /v1/events/{id}/signup [delete]
func deleteEventSignup(c *gin.Context) {
event, err := database.GetEvent(c.Param("id"))
if err != nil {
log.Errorf("Error getting event: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if event == nil {
response.RespondError(c, http.StatusNotFound, "Not Found")
return
}
user := c.MustGet("x-user").(*models.User)
found := false
for i := range event.Signups {
if *event.Signups[i].UserID == user.CID {
if err := database.DB.Delete(event.Signups[i]).Error; err != nil {
log.Errorf("Error deleting event signup: %s", err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
found = true
}
}
if !found {
response.RespondError(c, http.StatusNotFound, "Not Found")
return
}
response.RespondBlank(c, http.StatusNoContent)
}