|
// domain/account/service/account_service_impl.go |
|
|
|
package account |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
model "go-store/domain/account/model" |
|
account_repository "go-store/domain/account/repository" |
|
listing_model "go-store/domain/listing/model" |
|
listing_repository "go-store/domain/listing/repository" |
|
"go-store/dto" |
|
"go-store/helper" |
|
"go-store/prisma/db" |
|
"log" |
|
"strings" |
|
) |
|
|
|
type AccountServiceImpl struct { |
|
AccountRepository account_repository.AccountRepository |
|
ListingRepository listing_repository.ListingRepository |
|
} |
|
|
|
func NewAccountServiceImpl( |
|
accountRepository account_repository.AccountRepository, |
|
listingRepository listing_repository.ListingRepository, |
|
) AccountService { |
|
return &AccountServiceImpl{ |
|
AccountRepository: accountRepository, |
|
ListingRepository: listingRepository, |
|
} |
|
} |
|
|
|
func (s *AccountServiceImpl) Create(ctx context.Context, req dto.AccountCreate) (*dto.Account, error) { |
|
account, err := s.AccountRepository.Create(ctx, *model.NewAccount(&db.AccountModel{ |
|
InnerAccount: db.InnerAccount{ |
|
Email: req.Email, |
|
Name: req.Name, |
|
ClerkID: req.ClerkID, |
|
}, |
|
})) |
|
if err != nil { |
|
if strings.Contains(err.Error(), "email") { |
|
return nil, fmt.Errorf("an account with the email '%s' already exists", req.Email) |
|
} |
|
|
|
if strings.Contains(err.Error(), "clerk_id") { |
|
return nil, fmt.Errorf("an account with the clerk_id '%s' already exists", req.ClerkID) |
|
} |
|
|
|
log.Printf("Error creating account: %v", err) |
|
return nil, fmt.Errorf("an unexpected error occurred while creating the account") |
|
} |
|
|
|
model := model.NewAccount(account) |
|
dto := model.Dto() |
|
return dto, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) FindBy( |
|
ctx context.Context, |
|
value string, |
|
expand []string, |
|
paginationParamsCollection *dto.PaginationParamsCollection, |
|
) (*dto.Account, error) { |
|
account, err := s.AccountRepository.FindBy(ctx, value) |
|
if err != nil { |
|
if strings.Contains(err.Error(), "ErrNotFound") { |
|
return nil, fmt.Errorf("account with '%s' does not exist", value) |
|
} |
|
|
|
log.Printf("Error finding account by: %v", err) |
|
return nil, fmt.Errorf("an unexpected error occurred while retrieving the account") |
|
} |
|
|
|
acc := model.NewAccount(account) |
|
response := acc.Dto() |
|
|
|
if helper.Contains(expand, "listing") { |
|
listing, err := s.getListingsForAccount(ctx, account, paginationParamsCollection) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
if len(listing.Data) > 0 { |
|
response.Listings = listing |
|
} |
|
} |
|
|
|
return response, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) getListingsForAccount( |
|
ctx context.Context, |
|
account *db.AccountModel, |
|
paginationParamsCollection *dto.PaginationParamsCollection, |
|
) (*dto.ListingCollection, error) { |
|
listings, total, err := s.ListingRepository.FindAllByAccountID(ctx, account.ID, paginationParamsCollection) |
|
if err != nil { |
|
log.Printf("Error retrieving listings for account '%s': %v", account.ExternalID, err) |
|
return nil, fmt.Errorf("an unexpected error occurred while retrieving listings for the account") |
|
} |
|
|
|
var listingCollection dto.ListingCollection |
|
for _, listing := range listings { |
|
list := listing_model.NewListing(listing) |
|
dto := list.Dto() |
|
listingCollection.Data = append(listingCollection.Data, dto) |
|
} |
|
|
|
page := paginationParamsCollection.Listing.Page |
|
limit := paginationParamsCollection.Listing.Limit |
|
|
|
listingCollection.Meta = &dto.Meta{ |
|
Pagination: dto.Pagination{ |
|
Page: page, |
|
Limit: limit, |
|
TotalItems: total, |
|
TotalPages: (total + limit - 1) / limit, |
|
}, |
|
} |
|
|
|
return &listingCollection, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) Update(ctx context.Context, req dto.AccountUpdate) (*dto.Account, error) { |
|
account, err := s.AccountRepository.Update(ctx, *model.NewAccount(&db.AccountModel{ |
|
InnerAccount: db.InnerAccount{ |
|
ExternalID: req.ID, |
|
Name: req.Name, |
|
}, |
|
})) |
|
|
|
if err != nil { |
|
if strings.Contains(err.Error(), "ErrNotFound") { |
|
return nil, fmt.Errorf("cannot update: account with ID '%s' does not exist", req.ID) |
|
} |
|
|
|
log.Printf("Error updating account: %v", err) |
|
return nil, fmt.Errorf("an unexpected error occurred while updating the account") |
|
} |
|
|
|
accountModel := model.NewAccount(account) |
|
accountModelDto := accountModel.Dto() |
|
return accountModelDto, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) Delete(ctx context.Context, id string) (*dto.DeleteResponse, error) { |
|
err := s.AccountRepository.Delete(ctx, id) |
|
if err != nil { |
|
if strings.Contains(err.Error(), "ErrNotFound") { |
|
return nil, fmt.Errorf("cannot delete: account with ID '%s' does not exist", id) |
|
} |
|
|
|
log.Printf("Error deleting account: %v", err) |
|
return nil, fmt.Errorf("an unexpected error occurred while deleting the account") |
|
} |
|
|
|
return &dto.DeleteResponse{ |
|
Status: "ok", |
|
Message: "Deleted successfully", |
|
}, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) FindAll(ctx context.Context) (*dto.AccountCollection, error) { |
|
accountModels, err := s.AccountRepository.FindAll(ctx) |
|
if err != nil { |
|
log.Printf("Error retrieving accounts: %v", err) |
|
return nil, fmt.Errorf("an unexpected error occurred while retrieving accounts") |
|
} |
|
|
|
var accounts dto.AccountCollection |
|
for _, account := range accountModels { |
|
response := model.NewAccount(account) |
|
accounts.Data = append(accounts.Data, response.Dto()) |
|
} |
|
|
|
return &accounts, nil |
|
} |
|
|
|
func (s *AccountServiceImpl) Exists(ctx context.Context, accountID string) bool { |
|
exists := s.AccountRepository.Exists(ctx, accountID) |
|
if !exists { |
|
log.Printf("Account with ID '%s' does not exist", accountID) |
|
} |
|
return exists |
|
} |
Api Pesponse for GET
{{endpoint}}/api/v1/account/user_2ol3e8LvCLTZAlc6a3OcSORMnP1?expand=listing