feat: added delete endpoints for lists and list items
This commit is contained in:
@@ -47,7 +47,7 @@ func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
list, err := h.ListService.Create(ctx, authContext.UserID, payload.Name, payload.UserIDs)
|
||||
list, err := h.ListService.CreateList(ctx, authContext.UserID, payload.Name, payload.UserIDs)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to create list", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to create list")
|
||||
@@ -58,6 +58,46 @@ func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
||||
response.JSON(ctx, w, http.StatusCreated, list)
|
||||
}
|
||||
|
||||
// DeleteList handles the deletion of a list
|
||||
//
|
||||
// @Summary Delete list route
|
||||
// @Description Deletes a list, cascading to user associations and items
|
||||
// @Tags List
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "List ID"
|
||||
// @Success 204
|
||||
// @Error 400 {object} response.ErrorResponse "failed to decode request url"
|
||||
// @Error 500 {object} response.ErrorResponse "failed to delete list"
|
||||
// @Router /lists/{id} [delete]
|
||||
func (h *ListHandler) DeleteList(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
listID := r.PathValue("id")
|
||||
if listID == "" {
|
||||
slog.ErrorContext(ctx, "failed to read list id from path")
|
||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request url")
|
||||
return
|
||||
}
|
||||
|
||||
authContext, err := domain.GetAuthContext(ctx)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to get auth context", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to delete list")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.ListService.DeleteList(ctx, authContext.UserID, listID)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to delete list", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to delete list")
|
||||
return
|
||||
}
|
||||
|
||||
slog.InfoContext(ctx, "deleted list successfully", slog.Any("list_id", listID))
|
||||
response.Status(ctx, w, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// GetLists handles fetching lists for the current user
|
||||
//
|
||||
// @Summary Returns all lists of the user
|
||||
@@ -226,6 +266,46 @@ func (h *ListHandler) CreateListItem(w http.ResponseWriter, r *http.Request) {
|
||||
response.JSON(ctx, w, http.StatusCreated, item)
|
||||
}
|
||||
|
||||
// DeleteListItem handles the deletion of a list item
|
||||
//
|
||||
// @Summary Delete list item route
|
||||
// @Description Deletes an item
|
||||
// @Tags List
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "List Item ID"
|
||||
// @Success 204
|
||||
// @Error 400 {object} response.ErrorResponse "failed to decode request url"
|
||||
// @Error 500 {object} response.ErrorResponse "failed to delete list item"
|
||||
// @Router /lists/item/{id} [delete]
|
||||
func (h *ListHandler) DeleteListItem(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
listItemID := r.PathValue("id")
|
||||
if listItemID == "" {
|
||||
slog.ErrorContext(ctx, "failed to read list item id from path")
|
||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request url")
|
||||
return
|
||||
}
|
||||
|
||||
authContext, err := domain.GetAuthContext(ctx)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to get auth context", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to delete list item")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.ListService.DeleteListItem(ctx, authContext.UserID, listItemID)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "failed to delete list item", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to delete list item")
|
||||
return
|
||||
}
|
||||
|
||||
slog.InfoContext(ctx, "deleted list item successfully", slog.Any("list_item_id", listItemID))
|
||||
response.Status(ctx, w, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// UpdateListItem handles updating of a list item
|
||||
//
|
||||
// @Summary Update list item
|
||||
@@ -274,6 +354,7 @@ func (h *ListHandler) UpdateListItem(w http.ResponseWriter, r *http.Request) {
|
||||
// @Tags List
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "List Item ID"
|
||||
// @Param payload body request.SetListItemCompletedPayload true "Update list item is completed payload"
|
||||
// @Success 204 {object} nil
|
||||
// @Error 400 {object} response.ErrorResponse "failed to decode request url"
|
||||
|
||||
@@ -42,10 +42,12 @@ func NewMux(cfg Config) http.Handler {
|
||||
apiMux.Handle("POST /users", protected(userHandler.CreateUser))
|
||||
|
||||
apiMux.Handle("POST /lists", protected(listHandler.CreateList))
|
||||
apiMux.Handle("DELETE /lists/{id}", protected(listHandler.DeleteList))
|
||||
apiMux.Handle("GET /lists", protected(listHandler.GetLists))
|
||||
apiMux.Handle("POST /lists/user", protected(listHandler.AddUserToList))
|
||||
apiMux.Handle("DELETE /lists/user", protected(listHandler.RemoveUserFromList))
|
||||
apiMux.Handle("POST /lists/item", protected(listHandler.CreateListItem))
|
||||
apiMux.Handle("DELETE /lists/item/{id}", protected(listHandler.DeleteListItem))
|
||||
apiMux.Handle("PUT /lists/item", protected(listHandler.UpdateListItem))
|
||||
apiMux.Handle("POST /lists/items/{id}", protected(listHandler.SetListItemCompleted))
|
||||
apiMux.Handle("GET /lists/{id}", protected(listHandler.GetListItems))
|
||||
|
||||
Reference in New Issue
Block a user