fix: minor improvements, wrong argument order, ambigious columns in sql, log messages, etc

This commit is contained in:
2026-08-21 16:21:33 +02:00
parent 441c47163c
commit e91e80e725
4 changed files with 22 additions and 10 deletions
+7 -7
View File
@@ -126,7 +126,7 @@ func (h *ListHandler) AddUserToList(w http.ResponseWriter, r *http.Request) {
}
slog.InfoContext(ctx, "added user to list successfully", slog.Any("list_id", payload.ListID), slog.Any("user_id", payload.UserID))
response.JSON(ctx, w, http.StatusNoContent, nil)
response.Status(ctx, w, http.StatusNoContent)
}
// RemoveUserFromList handles the removal of a user association to a list
@@ -166,7 +166,7 @@ func (h *ListHandler) RemoveUserFromList(w http.ResponseWriter, r *http.Request)
}
slog.InfoContext(ctx, "removed user from list successfully", slog.Any("list_id", payload.ListID), slog.Any("user_id", payload.UserID))
response.JSON(ctx, w, http.StatusNoContent, nil)
response.Status(ctx, w, http.StatusNoContent)
}
// CreateListItem handles creation of a new list item on a given list
@@ -238,14 +238,14 @@ func (h *ListHandler) UpdateListItem(w http.ResponseWriter, r *http.Request) {
return
}
err = h.ListService.UpdateListItem(ctx, payload.ListItemID, authContext.UserID, payload.Title, payload.IsCompleted)
err = h.ListService.UpdateListItem(ctx, authContext.UserID, payload.ListItemID, payload.Title, payload.IsCompleted)
if err != nil {
slog.ErrorContext(ctx, "failed to update list item", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to update list item")
return
}
response.JSON(ctx, w, http.StatusNoContent, nil)
response.Status(ctx, w, http.StatusNoContent)
}
// SetListItemCompleted handles updating "is_completed" of a list item
@@ -286,14 +286,14 @@ func (h *ListHandler) SetListItemCompleted(w http.ResponseWriter, r *http.Reques
return
}
err = h.ListService.SetListItemCompleted(ctx, listItemID, authContext.UserID, payload.IsCompleted)
err = h.ListService.SetListItemCompleted(ctx, authContext.UserID, listItemID, payload.IsCompleted)
if err != nil {
slog.ErrorContext(ctx, "failed to set list item completed", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to set list item completed")
return
}
response.JSON(ctx, w, http.StatusNoContent, nil)
response.Status(ctx, w, http.StatusNoContent)
}
// GetListItems handles return all list items of a list
@@ -327,7 +327,7 @@ func (h *ListHandler) GetListItems(w http.ResponseWriter, r *http.Request) {
items, err := h.ListService.GetListItems(ctx, authContext.UserID, listID)
if err != nil {
slog.ErrorContext(ctx, "failed to set list item completed", slog.Any("error", err))
slog.ErrorContext(ctx, "failed to read list items", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to read list items")
return
}
+5
View File
@@ -7,6 +7,11 @@ import (
"net/http"
)
// TODO: Move to own file
func Status(ctx context.Context, w http.ResponseWriter, status int) {
w.WriteHeader(status)
}
func JSON(ctx context.Context, w http.ResponseWriter, status int, data any) {
payload, err := json.Marshal(data)
if err != nil {
+1 -1
View File
@@ -127,7 +127,7 @@ func (s *ListService) UpdateListItem(ctx context.Context, authUserID string, lis
return ErrListItemTitleEmpty
}
if err := s.userAllowedToAccessList(ctx, authUserID, listItemID); err != nil {
if err := s.userAllowedToAccessListItem(ctx, authUserID, listItemID); err != nil {
return err
}
+9 -2
View File
@@ -3,6 +3,7 @@ package repository
import (
"context"
"database/sql"
"errors"
"fmt"
"log/slog"
@@ -60,11 +61,14 @@ func (r *ListRepo) CreateList(ctx context.Context, name string, userIDs []string
func (r *ListRepo) GetLists(ctx context.Context, userID string) ([]domain.List, error) {
rows, err := r.db.QueryContext(ctx,
"SELECT id, name, created_at, modified_at FROM lists JOIN list_users ON list.id=list_users.list_id WHERE list_users.user_id = $1",
"SELECT id, name, lists.created_at, modified_at FROM lists INNER JOIN list_users ON lists.id=list_users.list_id WHERE list_users.user_id = $1",
userID,
)
if err != nil {
return nil, fmt.Errorf("failed to get list items: %w", err)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("failed to get lists: %w", err)
}
defer rows.Close()
@@ -177,6 +181,9 @@ func (r *ListRepo) GetListItems(ctx context.Context, listID string) ([]domain.Li
listID,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("failed to get list items: %w", err)
}
defer rows.Close()