diff --git a/go.mod b/go.mod index 2823ac0..bba9eaa 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect - github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.8.0 // indirect diff --git a/internal/adapters/db/cart.go b/internal/adapters/db/cart.go deleted file mode 100644 index 7b4b6a4..0000000 --- a/internal/adapters/db/cart.go +++ /dev/null @@ -1,62 +0,0 @@ -package db - -import ( - "errors" - "github.com/pangolin-do-golang/tech-challenge/internal/errutil" - - "github.com/google/uuid" - "github.com/pangolin-do-golang/tech-challenge/internal/core/cart" - "gorm.io/gorm" -) - -type PostgresCartRepository struct { - db *gorm.DB -} - -type CartPostgres struct { - BaseModel - ClientID uuid.UUID `gorm:"client_id"` - Customer CustomerPostgres `gorm:"foreignKey:ClientID"` - Products []CartProductsPostgres `gorm:"foreignKey:CartID"` -} - -func (op CartPostgres) TableName() string { - return "cart" -} - -func NewPostgresCartRepository(db *gorm.DB) cart.ICartRepository { - return &PostgresCartRepository{db: db} -} - -func (p *PostgresCartRepository) Create(clientID uuid.UUID) (*cart.Cart, error) { - record := &CartPostgres{ - ClientID: clientID, - } - - err := p.db.Create(record).Error - if err != nil { - return nil, err - } - - return &cart.Cart{ - ID: record.ID, - ClientID: record.ClientID, - }, nil -} - -func (p *PostgresCartRepository) Get(clientID uuid.UUID) (*cart.Cart, error) { - var row CartPostgres - err := p.db.First(&row, "client_id = ?", clientID).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errutil.ErrRecordNotFound - } - - return nil, err - } - - return &cart.Cart{ - ID: row.ID, - ClientID: row.ClientID, - }, nil -} diff --git a/internal/adapters/db/cart_products.go b/internal/adapters/db/cart_products.go deleted file mode 100644 index ceb2fbc..0000000 --- a/internal/adapters/db/cart_products.go +++ /dev/null @@ -1,79 +0,0 @@ -package db - -import ( - "context" - - "github.com/google/uuid" - "github.com/pangolin-do-golang/tech-challenge/internal/core/cart" - "gorm.io/gorm" -) - -type PostgresCartProductsRepository struct { - db *gorm.DB -} - -type CartProductsPostgres struct { - BaseModel - CartID uuid.UUID `gorm:"type:uuid"` - ProductID uuid.UUID `gorm:"type:uuid"` - Quantity int `gorm:"quantity"` - Comments string `gorm:"comments"` - Cart CartPostgres `gorm:"foreignKey:CartID"` - Product ProductPostgres `gorm:"foreignKey:ProductID"` -} - -func (op *CartProductsPostgres) TableName() string { - return "cart_products" -} - -func NewPostgresCartProductsRepository(db *gorm.DB) cart.ICartProductRepository { - return &PostgresCartProductsRepository{db: db} -} - -func (p *PostgresCartProductsRepository) Create(_ context.Context, cartID uuid.UUID, product *cart.Product) error { - cartProduct := CartProductsPostgres{ - CartID: cartID, - ProductID: product.ProductID, - Quantity: product.Quantity, - Comments: product.Comments, - } - - result := p.db.Create(&cartProduct) - if result.Error != nil { - return result.Error - } - - return nil -} - -func (p *PostgresCartProductsRepository) GetByCartID(_ context.Context, cartID uuid.UUID) ([]*cart.Product, error) { - var cartProducts []CartProductsPostgres - err := p.db.Where("cart_id = ?", cartID).Find(&cartProducts).Error - if err != nil { - return nil, err - } - - var products []*cart.Product - for _, cp := range cartProducts { - products = append(products, &cart.Product{ - ProductID: cp.ProductID, - Quantity: cp.Quantity, - Comments: cp.Comments, - }) - } - - return products, nil -} - -func (p *PostgresCartProductsRepository) DeleteByProductID(_ context.Context, cartID, productID uuid.UUID) error { - return p.db.Delete(&CartProductsPostgres{}, "cart_id = ? AND product_id = ?", cartID, productID).Error -} - -func (p *PostgresCartProductsRepository) UpdateProductByProductID(_ context.Context, cartID, productID uuid.UUID, product *cart.Product) error { - return p.db.Model(&CartProductsPostgres{}). - Where("cart_id = ? AND product_id = ?", cartID, productID). - Updates(map[string]interface{}{ - "quantity": product.Quantity, - "comments": product.Comments, - }).Error -} diff --git a/internal/adapters/db/customer.go b/internal/adapters/db/customer.go index 87aad00..fe2129b 100644 --- a/internal/adapters/db/customer.go +++ b/internal/adapters/db/customer.go @@ -14,11 +14,10 @@ type PostgresCustomerRepository struct { type CustomerPostgres struct { BaseModel - Name string `gorm:"name"` - Cpf string `gorm:"uniqueIndex" json:"cpf"` - Email string `gorm:"email"` - Age int `gorm:"age"` - Orders []OrderPostgres `gorm:"foreignKey:ClientID"` + Name string `gorm:"name"` + Cpf string `gorm:"uniqueIndex" json:"cpf"` + Email string `gorm:"email"` + Age int `gorm:"age"` } func (cp CustomerPostgres) TableName() string { diff --git a/internal/adapters/db/order.go b/internal/adapters/db/order.go deleted file mode 100644 index 900cefc..0000000 --- a/internal/adapters/db/order.go +++ /dev/null @@ -1,151 +0,0 @@ -package db - -import ( - "errors" - "fmt" - "strings" - - "github.com/google/uuid" - "github.com/pangolin-do-golang/tech-challenge/internal/core/order" - "github.com/pangolin-do-golang/tech-challenge/internal/errutil" - "gorm.io/gorm" -) - -type PostgresOrderRepository struct { - db *gorm.DB -} - -type OrderPostgres struct { - BaseModel - ClientID uuid.UUID `gorm:"client_id,type:uuid"` - TotalAmount float64 `gorm:"total_amount"` - Status string `gorm:"status"` - Customer CustomerPostgres `gorm:"foreignKey:ClientID"` - Products []OrderProductPostgres `gorm:"foreignKey:OrderID"` -} - -func (op OrderPostgres) TableName() string { - return "order" -} - -func NewPostgresOrderRepository(db *gorm.DB) order.IOrderRepository { - return &PostgresOrderRepository{db: db} -} - -func (r *PostgresOrderRepository) Update(order *order.Order) error { - result := r.db.Model(&OrderPostgres{}). - Where("id", order.ID). - Update("status", order.Status). - Update("total_amount", order.TotalAmount) - if result.Error != nil { - return result.Error - } - - return nil -} - -func (r *PostgresOrderRepository) Create(order *order.Order) (*order.Order, error) { - dbOrder := OrderPostgres{ - BaseModel: BaseModel{ID: uuid.New()}, - ClientID: order.ClientID, - TotalAmount: order.TotalAmount, - Status: order.Status, - } - - result := r.db.Create(&dbOrder) - if result.Error != nil { - return nil, result.Error - } - - order.ID = dbOrder.ID - order.CreatedAt = dbOrder.CreatedAt - return order, nil -} - -func (r *PostgresOrderRepository) Get(id uuid.UUID) (*order.Order, error) { - var record OrderPostgres - - if err := r.db.First(&record, "id = ?", id).Error; err != nil { - return nil, errutil.ErrRecordNotFound - } - - return &order.Order{ - ID: record.ID, - ClientID: record.ClientID, - CreatedAt: record.CreatedAt, - TotalAmount: record.TotalAmount, - Status: record.Status, - }, nil -} - -func (r *PostgresOrderRepository) GetAll() ([]order.Order, error) { - var records []OrderPostgres - - err := r.db.Raw(buildGetAllQuery()).Scan(&records).Error - - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errutil.ErrRecordNotFound - } - - if err != nil { - return nil, err - } - - parsedOrders := make([]order.Order, len(records)) - for i, record := range records { - parsedOrders[i] = order.Order{ - ID: record.ID, - ClientID: record.ClientID, - CreatedAt: record.CreatedAt, - TotalAmount: record.TotalAmount, - Status: record.Status, - } - } - - return parsedOrders, nil -} - -func buildGetAllQuery() string { - ignoredStatus := []string{ - order.StatusFinished, - order.StatusDeclined, - } - - statusSortedPriority := []string{ - order.StatusReady, - order.StatusPreparing, - order.StatusPaid, - order.StatusPending, - order.StatusCreated, - } - - return fmt.Sprintf(` - SELECT * FROM "order" - WHERE status NOT IN (%s) - ORDER BY - CASE %s - END, created_at - `, - buildIgnoredStatusCondition(ignoredStatus), - buildSortedStatusCase(statusSortedPriority), - ) -} - -func buildIgnoredStatusCondition(ignoredStatus []string) string { - var ignoredParts []string - for _, status := range ignoredStatus { - ignoredParts = append(ignoredParts, fmt.Sprintf("'%s'", status)) - } - - return strings.Join(ignoredParts, ", ") -} - -func buildSortedStatusCase(statusSortedPriority []string) string { - var caseParts []string - for i, status := range statusSortedPriority { - caseParts = append(caseParts, fmt.Sprintf("WHEN status = '%s' THEN %d", status, i+1)) - } - caseParts = append(caseParts, fmt.Sprintf("ELSE %d", len(statusSortedPriority))) - - return strings.Join(caseParts, " ") -} diff --git a/internal/adapters/db/order_products.go b/internal/adapters/db/order_products.go deleted file mode 100644 index e6b1eb0..0000000 --- a/internal/adapters/db/order_products.go +++ /dev/null @@ -1,66 +0,0 @@ -package db - -import ( - "context" - "github.com/pangolin-do-golang/tech-challenge/internal/core/order" - - "github.com/google/uuid" - "gorm.io/gorm" -) - -type PostgresOrderProductsRepository struct { - db *gorm.DB -} - -type OrderProductPostgres struct { - BaseModel - OrderID uuid.UUID `gorm:"type:uuid"` - ProductID uuid.UUID `gorm:"type:uuid"` - Quantity int `gorm:"quantity"` - Comments string `gorm:"comments"` - Order OrderPostgres `gorm:"foreignKey:OrderID"` - Product ProductPostgres `gorm:"foreignKey:ProductID"` -} - -func (op *OrderProductPostgres) TableName() string { - return "order_products" -} - -func NewPostgresOrderProductsRepository(db *gorm.DB) order.IOrderProductRepository { - return &PostgresOrderProductsRepository{db: db} -} - -func (p *PostgresOrderProductsRepository) Create(_ context.Context, orderID uuid.UUID, product *order.Product) error { - orderProduct := OrderProductPostgres{ - OrderID: orderID, - ProductID: product.ProductID, - Quantity: product.Quantity, - Comments: product.Comments, - } - - result := p.db.Create(&orderProduct) - if result.Error != nil { - return result.Error - } - - return nil -} - -func (p *PostgresOrderProductsRepository) GetByOrderID(_ context.Context, cartID uuid.UUID) ([]*order.Product, error) { - var cartProducts []OrderProductPostgres - err := p.db.Where("order_id = ?", cartID).Find(&cartProducts).Error - if err != nil { - return nil, err - } - - var products []*order.Product - for _, cp := range cartProducts { - products = append(products, &order.Product{ - ProductID: cp.ProductID, - Quantity: cp.Quantity, - Comments: cp.Comments, - }) - } - - return products, nil -} diff --git a/internal/adapters/db/product.go b/internal/adapters/db/product.go deleted file mode 100644 index ee0decd..0000000 --- a/internal/adapters/db/product.go +++ /dev/null @@ -1,107 +0,0 @@ -package db - -import ( - "fmt" - "time" - - "github.com/google/uuid" - "github.com/pangolin-do-golang/tech-challenge/internal/core/product" - "gorm.io/gorm" -) - -type PostgresProductRepository struct { - db *gorm.DB -} - -type ProductPostgres struct { - BaseModel - Name string `gorm:"name"` - Description string `gorm:"description"` - Category string `gorm:"category"` - Price float64 `gorm:"price"` -} - -func (pp ProductPostgres) TableName() string { - return "product" -} - -func NewPostgresProductRepository(db *gorm.DB) product.Repository { - return &PostgresProductRepository{db: db} -} - -func (repo *PostgresProductRepository) GetByID(id uuid.UUID) (*product.Product, error) { - var dbRecord ProductPostgres - - if err := repo.db.Where("id = ? and deleted_at is null", id).First(&dbRecord).Error; err != nil { - return nil, err - } - - if dbRecord.ID == uuid.Nil { - return nil, nil - } - - return &product.Product{ - Id: dbRecord.ID, - Name: dbRecord.Name, - Description: dbRecord.Description, - Category: dbRecord.Category, - Price: dbRecord.Price, - }, nil -} - -func (repo *PostgresProductRepository) Search(search string, category string) (*[]product.Product, error) { - var dbRecords []ProductPostgres - - db := repo.db - - if len(search) > 0 { - parsedSearch := fmt.Sprintf("%%%s%%", search) - db = db.Where("name ILIKE ?", parsedSearch) - } - - if len(category) > 0 { - db = db.Where("category = ?", category) - } - - if err := db.Where("deleted_at is null").Find(&dbRecords).Error; err != nil { - return nil, err - } - - if len(dbRecords) == 0 { - emptyResult := make([]product.Product, 0) - return &emptyResult, nil - } - - var results []product.Product - - for _, record := range dbRecords { - results = append(results, product.Product{ - Id: record.ID, - Name: record.Name, - Description: record.Description, - Category: record.Category, - Price: record.Price, - }) - } - - return &results, nil -} - -func (repo *PostgresProductRepository) Delete(id uuid.UUID) error { - var dbRecord *ProductPostgres - - if err := repo.db.Where("id = ? and deleted_at is null", id).First(&dbRecord).Error; err != nil { - return err - } - - if dbRecord == nil { - return nil - } - - if err := repo.db.Model(&dbRecord).Where("id = ? and deleted_at is null", id).Update("deleted_at", time.Now()).Error; err != nil { - fmt.Println(err) - return err - } - - return nil -} diff --git a/internal/errutil/errors.go b/internal/errutil/errors.go index 94605bb..8964bf1 100644 --- a/internal/errutil/errors.go +++ b/internal/errutil/errors.go @@ -14,14 +14,6 @@ func (e *Error) Error() string { return e.Message } -func NewSystemError(err error, mes string) *Error { - return &Error{ - Message: mes, - Type: "SYSTEM", - originalError: err, - } -} - func NewBusinessError(err error, mes string) *Error { return &Error{ Message: mes,