Skip to content

Commit 6722949

Browse files
committed
Allow setting event type ID of an availability rule in the API request
1 parent 0aeb1ef commit 6722949

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

‎internal/handler/availability.go‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
123123
r.Body = http.MaxBytesReader(w, r.Body, 32<<10)
124124

125125
var req struct {
126-
DayOfWeek *int `json:"day_of_week"`
127-
StartTime *string `json:"start_time"`
128-
EndTime *string `json:"end_time"`
126+
EventTypeId *string `json:"event_type_id"`
127+
DayOfWeek *int `json:"day_of_week"`
128+
StartTime *string `json:"start_time"`
129+
EndTime *string `json:"end_time"`
129130
}
130131
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
131132
h.writeError(w, http.StatusBadRequest, "invalid JSON")
@@ -137,11 +138,10 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
137138
}
138139

139140
var current availRuleJSON
140-
var etID sql.NullString
141141
err := h.db.QueryRowContext(r.Context(),
142142
`SELECT id, event_type_id, day_of_week, start_time, end_time
143143
FROM availability_rules WHERE id = ? AND user_id = ?`, id, user.ID).
144-
Scan(&current.ID, &etID, &current.DayOfWeek, &current.StartTime, &current.EndTime)
144+
Scan(&current.ID, &current.EventTypeID, &current.DayOfWeek, &current.StartTime, &current.EndTime)
145145
if err == sql.ErrNoRows {
146146
h.writeError(w, http.StatusNotFound, "rule not found")
147147
return
@@ -151,8 +151,16 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
151151
h.writeError(w, http.StatusInternalServerError, "internal error")
152152
return
153153
}
154-
if etID.Valid {
155-
current.EventTypeID = &etID.String
154+
155+
// As there's no good way to distinguish between "set etId to null" and "don't set etId" we're using a string of
156+
// `null` to represent the former.
157+
if req.EventTypeId != nil {
158+
etID := req.EventTypeId
159+
if *etID == "null" {
160+
current.EventTypeID = nil
161+
} else {
162+
current.EventTypeID = req.EventTypeId
163+
}
156164
}
157165

158166
if req.DayOfWeek != nil {
@@ -174,8 +182,8 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
174182
}
175183

176184
_, err = h.db.ExecContext(r.Context(),
177-
`UPDATE availability_rules SET day_of_week=?, start_time=?, end_time=? WHERE id=? AND user_id=?`,
178-
current.DayOfWeek, current.StartTime, current.EndTime, id, user.ID)
185+
`UPDATE availability_rules SET event_type_id=?, day_of_week=?, start_time=?, end_time=? WHERE id=? AND user_id=?`,
186+
current.EventTypeID, current.DayOfWeek, current.StartTime, current.EndTime, id, user.ID)
179187
if err != nil {
180188
if db.IsUniqueViolation(err) {
181189
h.writeError(w, http.StatusConflict, "a rule for this day and time already exists")

0 commit comments

Comments
 (0)