Advertisement
erkayes935

customer_usecase.go

Dec 25th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.39 KB | Source Code | 0 0
  1. package usecase
  2.  
  3. import (
  4.     "challenge-goapi/model"
  5.     "challenge-goapi/repository"
  6.     "fmt"
  7. )
  8.  
  9. type CustomerUseCase interface {
  10.     CreateNewCustomer(customer model.Customer) (model.Customer, error)
  11.     FindAllCustomer() ([]model.Customer, error)
  12.     FindCustomerById(id string) (model.Customer, error)
  13.     UpdateCustomer(customer model.Customer) (model.Customer, error)
  14.     DeleteCustomer(id string) error
  15. }
  16.  
  17. type customerUseCase struct {
  18.     repo repository.CustomerRepository
  19. }
  20.  
  21. func (c *customerUseCase) CreateNewCustomer(customer model.Customer) (model.Customer, error) {
  22.     return c.repo.Create(customer)
  23. }
  24.  
  25. func (c *customerUseCase) FindAllCustomer() ([]model.Customer, error) {
  26.     return c.repo.List()
  27. }
  28.  
  29. func (c *customerUseCase) FindCustomerById(id string) (model.Customer, error) {
  30.     return c.repo.Get(id)
  31. }
  32.  
  33. func (c *customerUseCase) UpdateCustomer(customer model.Customer) (model.Customer, error) {
  34.     _, err := c.repo.Get(customer.Id)
  35.     if err != nil {
  36.         return model.Customer{}, fmt.Errorf("customer with ID %d not found", customer.Id)
  37.     }
  38.     return c.repo.Update(customer)
  39. }
  40.  
  41. func (c *customerUseCase) DeleteCustomer(id string) error {
  42.     _, err := c.repo.Get(id)
  43.     if err != nil {
  44.         return fmt.Errorf("customer with ID %d not found", id)
  45.     }
  46.     return c.repo.Delete(id)
  47. }
  48.  
  49. func NewCustomerUseCase(repo repository.CustomerRepository) CustomerUseCase {
  50.     return &customerUseCase{repo: repo}
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement