168 lines
4.5 KiB
Go
168 lines
4.5 KiB
Go
package unit
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"wish-list-api/api/presenter"
|
|
"wish-list-api/pkg/entities"
|
|
wishlist "wish-list-api/pkg/wish-list"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
type MockWishListRepository struct {
|
|
wishLists map[string]*entities.WishList
|
|
wishListItems map[string]*entities.WishListItem
|
|
}
|
|
|
|
func NewMockWishListRepository() wishlist.Repository {
|
|
return &MockWishListRepository{
|
|
wishLists: make(map[string]*entities.WishList),
|
|
wishListItems: make(map[string]*entities.WishListItem),
|
|
}
|
|
}
|
|
|
|
func (r *MockWishListRepository) CreateWishList(wishList *entities.WishList) (*entities.WishList, error) {
|
|
if wishList.ID == "" {
|
|
wishList.ID = "mock-id-" + time.Now().String()
|
|
}
|
|
wishList.CreatedAt = time.Now()
|
|
wishList.UpdatedAt = time.Now()
|
|
r.wishLists[wishList.ID] = wishList
|
|
return wishList, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) ReadWishList(ID string) (*entities.WishList, error) {
|
|
if wishList, ok := r.wishLists[ID]; ok {
|
|
return wishList, nil
|
|
}
|
|
return nil, errors.New("wishlist not found")
|
|
}
|
|
|
|
func (r *MockWishListRepository) ReadAllWishLists(userID string) (*[]presenter.WishList, error) {
|
|
var result []presenter.WishList
|
|
for _, wl := range r.wishLists {
|
|
if wl.UserID == userID {
|
|
objID, _ := primitive.ObjectIDFromHex(wl.ID)
|
|
result = append(result, presenter.WishList{
|
|
ID: objID,
|
|
Title: wl.Title,
|
|
UserID: wl.UserID,
|
|
Description: wl.Description,
|
|
IsPublic: wl.IsPublic,
|
|
PhotoURL: wl.PhotoURL,
|
|
CreatedAt: wl.CreatedAt,
|
|
UpdatedAt: wl.UpdatedAt,
|
|
})
|
|
}
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) ReadPublicWishLists() (*[]presenter.WishList, error) {
|
|
var result []presenter.WishList
|
|
for _, wl := range r.wishLists {
|
|
if wl.IsPublic {
|
|
objID, _ := primitive.ObjectIDFromHex(wl.ID)
|
|
result = append(result, presenter.WishList{
|
|
ID: objID,
|
|
Title: wl.Title,
|
|
UserID: wl.UserID,
|
|
Description: wl.Description,
|
|
IsPublic: wl.IsPublic,
|
|
PhotoURL: wl.PhotoURL,
|
|
CreatedAt: wl.CreatedAt,
|
|
UpdatedAt: wl.UpdatedAt,
|
|
})
|
|
}
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) UpdateWishList(wishList *entities.WishList) (*entities.WishList, error) {
|
|
if _, ok := r.wishLists[wishList.ID]; !ok {
|
|
return nil, errors.New("wishlist not found")
|
|
}
|
|
wishList.UpdatedAt = time.Now()
|
|
r.wishLists[wishList.ID] = wishList
|
|
return wishList, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) DeleteWishList(ID string) error {
|
|
if _, ok := r.wishLists[ID]; !ok {
|
|
return errors.New("wishlist not found")
|
|
}
|
|
delete(r.wishLists, ID)
|
|
|
|
for id, item := range r.wishListItems {
|
|
if item.WishListID == ID {
|
|
delete(r.wishListItems, id)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) CreateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) {
|
|
if _, ok := r.wishLists[item.WishListID]; !ok {
|
|
return nil, errors.New("wishlist not found")
|
|
}
|
|
|
|
if item.ID == "" {
|
|
item.ID = "mock-item-id-" + time.Now().String()
|
|
}
|
|
item.CreatedAt = time.Now()
|
|
item.UpdatedAt = time.Now()
|
|
r.wishListItems[item.ID] = item
|
|
return item, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) ReadWishListItem(ID string) (*entities.WishListItem, error) {
|
|
if item, ok := r.wishListItems[ID]; ok {
|
|
return item, nil
|
|
}
|
|
return nil, errors.New("wishlist item not found")
|
|
}
|
|
|
|
func (r *MockWishListRepository) ReadAllWishListItems(wishListID string) (*[]presenter.WishListItem, error) {
|
|
if _, ok := r.wishLists[wishListID]; !ok {
|
|
return nil, errors.New("wishlist not found")
|
|
}
|
|
|
|
var result []presenter.WishListItem
|
|
for _, item := range r.wishListItems {
|
|
if item.WishListID == wishListID {
|
|
objID, _ := primitive.ObjectIDFromHex(item.ID)
|
|
result = append(result, presenter.WishListItem{
|
|
ID: objID,
|
|
Title: item.Title,
|
|
URL: item.URL,
|
|
Cost: item.Cost,
|
|
WishListID: item.WishListID,
|
|
Description: item.Description,
|
|
PhotoURL: item.PhotoURL,
|
|
CreatedAt: item.CreatedAt,
|
|
UpdatedAt: item.UpdatedAt,
|
|
})
|
|
}
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) UpdateWishListItem(item *entities.WishListItem) (*entities.WishListItem, error) {
|
|
if _, ok := r.wishListItems[item.ID]; !ok {
|
|
return nil, errors.New("wishlist item not found")
|
|
}
|
|
item.UpdatedAt = time.Now()
|
|
r.wishListItems[item.ID] = item
|
|
return item, nil
|
|
}
|
|
|
|
func (r *MockWishListRepository) DeleteWishListItem(ID string) error {
|
|
if _, ok := r.wishListItems[ID]; !ok {
|
|
return errors.New("wishlist item not found")
|
|
}
|
|
delete(r.wishListItems, ID)
|
|
return nil
|
|
}
|