fix: minor improvements, wrong argument order, ambigious columns in sql, log messages, etc
This commit is contained in:
@@ -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))
|
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
|
// 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))
|
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
|
// 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
|
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 {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to update list item", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to update list item", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to update list item")
|
response.Error(ctx, w, http.StatusInternalServerError, "failed to update list item")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(ctx, w, http.StatusNoContent, nil)
|
response.Status(ctx, w, http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetListItemCompleted handles updating "is_completed" of a list item
|
// SetListItemCompleted handles updating "is_completed" of a list item
|
||||||
@@ -286,14 +286,14 @@ func (h *ListHandler) SetListItemCompleted(w http.ResponseWriter, r *http.Reques
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.ListService.SetListItemCompleted(ctx, listItemID, authContext.UserID, payload.IsCompleted)
|
err = h.ListService.SetListItemCompleted(ctx, authContext.UserID, listItemID, payload.IsCompleted)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to set list item completed", slog.Any("error", err))
|
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")
|
response.Error(ctx, w, http.StatusInternalServerError, "failed to set list item completed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(ctx, w, http.StatusNoContent, nil)
|
response.Status(ctx, w, http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetListItems handles return all list items of a list
|
// 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)
|
items, err := h.ListService.GetListItems(ctx, authContext.UserID, listID)
|
||||||
if err != nil {
|
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")
|
response.Error(ctx, w, http.StatusInternalServerError, "failed to read list items")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ import (
|
|||||||
"net/http"
|
"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) {
|
func JSON(ctx context.Context, w http.ResponseWriter, status int, data any) {
|
||||||
payload, err := json.Marshal(data)
|
payload, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ func (s *ListService) UpdateListItem(ctx context.Context, authUserID string, lis
|
|||||||
return ErrListItemTitleEmpty
|
return ErrListItemTitleEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.userAllowedToAccessList(ctx, authUserID, listItemID); err != nil {
|
if err := s.userAllowedToAccessListItem(ctx, authUserID, listItemID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"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) {
|
func (r *ListRepo) GetLists(ctx context.Context, userID string) ([]domain.List, error) {
|
||||||
rows, err := r.db.QueryContext(ctx,
|
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,
|
userID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
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()
|
defer rows.Close()
|
||||||
|
|
||||||
@@ -177,6 +181,9 @@ func (r *ListRepo) GetListItems(ctx context.Context, listID string) ([]domain.Li
|
|||||||
listID,
|
listID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("failed to get list items: %w", err)
|
return nil, fmt.Errorf("failed to get list items: %w", err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user