generated from PenaSide/GolangTemplate
feat(repository/account): add/remove item from cart
This commit is contained in:
parent
69a5c5cc12
commit
9a6961984c
@ -37,7 +37,7 @@ func NewAccountRepository(deps *AccountRepositoryDeps) *AccountRepository {
|
||||
}
|
||||
|
||||
if deps.MongoDB == nil {
|
||||
log.Panicln("repository is nil on <NewAccountRepository>")
|
||||
log.Panicln("mongodb is nil on <NewAccountRepository>")
|
||||
}
|
||||
|
||||
return &AccountRepository{
|
||||
@ -135,16 +135,16 @@ func (receiver *AccountRepository) Insert(ctx context.Context, account *models.A
|
||||
func (receiver *AccountRepository) Remove(ctx context.Context, id string) (*models.Account, errors.Error) {
|
||||
account := models.Account{}
|
||||
|
||||
update := bson.M{"$set": bson.M{
|
||||
fields.Account.Deleted: true,
|
||||
fields.Account.DeletedAt: time.Now(),
|
||||
}}
|
||||
|
||||
filter := bson.M{
|
||||
fields.Account.UserID: id,
|
||||
fields.Account.Deleted: false,
|
||||
}
|
||||
|
||||
update := bson.M{"$set": bson.M{
|
||||
fields.Account.Deleted: true,
|
||||
fields.Account.DeletedAt: time.Now(),
|
||||
}}
|
||||
|
||||
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update).Decode(&account); err != nil {
|
||||
receiver.logger.Error("failed to set 'deleted=true' on <Delete> of <AccountRepository>",
|
||||
zap.String("id", id),
|
||||
@ -210,3 +210,81 @@ func (receiver *AccountRepository) CountAll(ctx context.Context) (int64, errors.
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (receiver *AccountRepository) AddItemToCart(ctx context.Context, userID, itemID string) (*models.Account, errors.Error) {
|
||||
account := models.Account{}
|
||||
|
||||
filter := bson.M{
|
||||
fields.Account.UserID: userID,
|
||||
fields.Account.Deleted: false,
|
||||
}
|
||||
|
||||
update := bson.M{
|
||||
"$addToSet": bson.M{
|
||||
fields.Account.Cart: itemID,
|
||||
},
|
||||
"$set": bson.M{
|
||||
fields.Account.UpdatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update).Decode(&account); err != nil {
|
||||
receiver.logger.Error("failed to add item on <AddItemToCart> of <AccountRepository>",
|
||||
zap.String("userID", userID),
|
||||
zap.String("itemID", itemID),
|
||||
zap.Error(err),
|
||||
)
|
||||
|
||||
removeErr := errors.New(
|
||||
fmt.Errorf("failed to add item <%s> account with <%s> on <AddItemToCart> of <AccountRepository>: %w", itemID, userID, err),
|
||||
errors.ErrInternalError,
|
||||
)
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
removeErr.SetType(errors.ErrNotFound)
|
||||
}
|
||||
|
||||
return nil, removeErr
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (receiver *AccountRepository) RemoveItemFromCart(ctx context.Context, userID, itemID string) (*models.Account, errors.Error) {
|
||||
account := models.Account{}
|
||||
|
||||
filter := bson.M{
|
||||
fields.Account.UserID: userID,
|
||||
fields.Account.Deleted: false,
|
||||
}
|
||||
|
||||
update := bson.M{
|
||||
"$pull": bson.M{
|
||||
fields.Account.Cart: itemID,
|
||||
},
|
||||
"$set": bson.M{
|
||||
fields.Account.UpdatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update).Decode(&account); err != nil {
|
||||
receiver.logger.Error("failed to add item on <AddItemToCart> of <AccountRepository>",
|
||||
zap.String("userID", userID),
|
||||
zap.String("itemID", itemID),
|
||||
zap.Error(err),
|
||||
)
|
||||
|
||||
removeErr := errors.New(
|
||||
fmt.Errorf("failed to add item <%s> account with <%s> on <AddItemToCart> of <AccountRepository>: %w", itemID, userID, err),
|
||||
errors.ErrInternalError,
|
||||
)
|
||||
|
||||
if err == mongo.ErrNoDocuments {
|
||||
removeErr.SetType(errors.ErrNotFound)
|
||||
}
|
||||
|
||||
return nil, removeErr
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ func NewCurrencyRepository(deps *CurrencyRepositoryDeps) *CurrencyRepository {
|
||||
}
|
||||
|
||||
if deps.MongoDB == nil {
|
||||
log.Panicln("repository is nil on <NewCurrencyRepository>")
|
||||
log.Panicln("mongodb is nil on <NewCurrencyRepository>")
|
||||
}
|
||||
|
||||
return &CurrencyRepository{
|
||||
|
Loading…
Reference in New Issue
Block a user