feat: added GET /lists, POST /list/items/{id} and GET /list/{id}
This commit is contained in:
+81
-15
@@ -3,6 +3,7 @@ package domain
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
@@ -34,12 +35,15 @@ type ListItem struct {
|
||||
|
||||
type ListRepository interface {
|
||||
CreateList(ctx context.Context, name string, userIDs []string) (*List, error)
|
||||
GetListsByUserID(ctx context.Context, userID string) ([]domain.List, error)
|
||||
AddUserToList(ctx context.Context, listID string, userID string) error
|
||||
RemoveUserFromList(ctx context.Context, listID string, userID string) error
|
||||
IsUserInList(ctx context.Context, listID string, userID string) (bool, error)
|
||||
IsUserInListByItemID(ctx context.Context, listItemID string, userID string) (bool, error)
|
||||
CreateListItem(ctx context.Context, listID string, title string) (*ListItem, error)
|
||||
UpdateListItem(ctx context.Context, listItemID string, title string, isCompleted bool) error
|
||||
SetListItemCompleted(ctx context.Context, listItemID string, isCompleted bool) error
|
||||
GetListItems(ctx context.Context, listID string) ([]ListItem, error)
|
||||
}
|
||||
|
||||
type ListService struct {
|
||||
@@ -62,6 +66,10 @@ func (s *ListService) Create(ctx context.Context, authUserID string, name string
|
||||
return s.repo.CreateList(ctx, name, userIDs)
|
||||
}
|
||||
|
||||
func (s *ListService) GetLists(ctx context.Context, authUserID string) ([]List, error) {
|
||||
return s.repo.GetListsByUserID(ctx, authUserID)
|
||||
}
|
||||
|
||||
func (s *ListService) AddUserToList(ctx context.Context, authUserID string, listID string, userID string) error {
|
||||
if listID == "" {
|
||||
return ErrListIDEmpty
|
||||
@@ -89,13 +97,9 @@ func (s *ListService) RemoveUserFromList(ctx context.Context, authUserID string,
|
||||
return ErrUserIDEmpty
|
||||
}
|
||||
|
||||
inList, err := s.repo.IsUserInList(ctx, listID, authUserID)
|
||||
if err != nil {
|
||||
if err := s.userAllowedToAccessList(ctx, authUserID, listID); err != nil {
|
||||
return err
|
||||
}
|
||||
if !inList {
|
||||
return ErrUserNotInList
|
||||
}
|
||||
|
||||
return s.repo.RemoveUserFromList(ctx, listID, userID)
|
||||
}
|
||||
@@ -108,13 +112,9 @@ func (s *ListService) CreateListItem(ctx context.Context, authUserID string, lis
|
||||
return nil, ErrListItemTitleEmpty
|
||||
}
|
||||
|
||||
inList, err := s.repo.IsUserInList(ctx, listID, authUserID)
|
||||
if err != nil {
|
||||
if err := s.userAllowedToAccessList(ctx, authUserID, listID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !inList {
|
||||
return nil, ErrUserNotInList
|
||||
}
|
||||
|
||||
return s.repo.CreateListItem(ctx, listID, title)
|
||||
}
|
||||
@@ -127,13 +127,79 @@ func (s *ListService) UpdateListItem(ctx context.Context, authUserID string, lis
|
||||
return ErrListItemTitleEmpty
|
||||
}
|
||||
|
||||
inList, err := s.repo.IsUserInListByItemID(ctx, listItemID, authUserID)
|
||||
if err != nil {
|
||||
if err := s.userAllowedToAccessList(ctx, authUserID, listItemID); err != nil {
|
||||
return err
|
||||
}
|
||||
if !inList {
|
||||
return ErrUserNotInList
|
||||
}
|
||||
|
||||
return s.repo.UpdateListItem(ctx, listItemID, title, isCompleted)
|
||||
}
|
||||
|
||||
func (s *ListService) SetListItemCompleted(ctx context.Context, authUserID string, listItemID string, isCompleted bool) error {
|
||||
if listItemID == "" {
|
||||
return ErrListItemIDEmpty
|
||||
}
|
||||
|
||||
if err := s.userAllowedToAccessListItem(ctx, authUserID, listItemID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.repo.SetListItemCompleted(ctx, listItemID, isCompleted)
|
||||
}
|
||||
|
||||
func (s *ListService) GetListItems(ctx context.Context, authUserID string, listID string) ([]ListItem, error) {
|
||||
if listID == "" {
|
||||
return nil, ErrListIDEmpty
|
||||
}
|
||||
|
||||
if err := s.userAllowedToAccessList(ctx, authUserID, listID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.repo.GetListItems(ctx, listID)
|
||||
}
|
||||
|
||||
func (s *ListService) userAllowedToAccessList(ctx context.Context, authUserID string, listID string) error {
|
||||
inList, err := s.repo.IsUserInList(ctx, listID, authUserID)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx,
|
||||
"failed to check if user is in list",
|
||||
slog.String("user_id", authUserID),
|
||||
slog.String("list_id", listID),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if !inList {
|
||||
slog.WarnContext(ctx,
|
||||
"user tried to access list without permission",
|
||||
slog.String("user_id", authUserID),
|
||||
slog.String("list_id", listID),
|
||||
)
|
||||
return ErrUserNotInList
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ListService) userAllowedToAccessListItem(ctx context.Context, authUserID string, listItemID string) error {
|
||||
inList, err := s.repo.IsUserInListByItemID(ctx, listItemID, authUserID)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx,
|
||||
"failed to check if user is in list",
|
||||
slog.String("user_id", authUserID),
|
||||
slog.String("list_item_id", listItemID),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if !inList {
|
||||
slog.WarnContext(ctx,
|
||||
"user tried to access list item without permission",
|
||||
slog.String("user_id", authUserID),
|
||||
slog.String("list_item_id", listItemID),
|
||||
)
|
||||
return ErrUserNotInList
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user