Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
feat(issue-34): adds cart overview endpoint and fixes db references
Browse files Browse the repository at this point in the history
  • Loading branch information
jefersonhuan committed Jul 28, 2024
1 parent 910d2d2 commit d5dbe16
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 53 deletions.
Binary file added assets/diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 48 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ const docTemplate = `{
}
}
},
"/cart/overview": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Cart"
],
"parameters": [
{
"description": "GetCartPayload",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/controller.GetCartPayload"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"500": {
"description": "Internal Server Error"
}
}
}
},
"/cart/remove-product": {
"post": {
"description": "Removes a Product from Customer's Cart",
Expand Down Expand Up @@ -124,7 +156,7 @@ const docTemplate = `{
},
"/customer": {
"get": {
"description": "Get all customer's list",
"description": "Overview all customer's list",
"consumes": [
"application/json"
],
Expand All @@ -134,7 +166,7 @@ const docTemplate = `{
"tags": [
"Customer"
],
"summary": "Get customer list",
"summary": "Overview customer list",
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -189,7 +221,7 @@ const docTemplate = `{
},
"/customer/{cpf}": {
"get": {
"description": "Get a customer by cpf",
"description": "Overview a customer by cpf",
"consumes": [
"application/json"
],
Expand All @@ -199,7 +231,7 @@ const docTemplate = `{
"tags": [
"Customer"
],
"summary": "Get customer by cpf",
"summary": "Overview customer by cpf",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -658,6 +690,18 @@ const docTemplate = `{
}
}
},
"controller.GetCartPayload": {
"type": "object",
"required": [
"client_id"
],
"properties": {
"client_id": {
"type": "string",
"format": "uuid"
}
}
},
"controller.HTTPError": {
"type": "object",
"properties": {
Expand Down
52 changes: 48 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@
}
}
},
"/cart/overview": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Cart"
],
"parameters": [
{
"description": "GetCartPayload",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/controller.GetCartPayload"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"500": {
"description": "Internal Server Error"
}
}
}
},
"/cart/remove-product": {
"post": {
"description": "Removes a Product from Customer's Cart",
Expand Down Expand Up @@ -118,7 +150,7 @@
},
"/customer": {
"get": {
"description": "Get all customer's list",
"description": "Overview all customer's list",
"consumes": [
"application/json"
],
Expand All @@ -128,7 +160,7 @@
"tags": [
"Customer"
],
"summary": "Get customer list",
"summary": "Overview customer list",
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -183,7 +215,7 @@
},
"/customer/{cpf}": {
"get": {
"description": "Get a customer by cpf",
"description": "Overview a customer by cpf",
"consumes": [
"application/json"
],
Expand All @@ -193,7 +225,7 @@
"tags": [
"Customer"
],
"summary": "Get customer by cpf",
"summary": "Overview customer by cpf",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -652,6 +684,18 @@
}
}
},
"controller.GetCartPayload": {
"type": "object",
"required": [
"client_id"
],
"properties": {
"client_id": {
"type": "string",
"format": "uuid"
}
}
},
"controller.HTTPError": {
"type": "object",
"properties": {
Expand Down
36 changes: 32 additions & 4 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ definitions:
- product_id
- quantity
type: object
controller.GetCartPayload:
properties:
client_id:
format: uuid
type: string
required:
- client_id
type: object
controller.HTTPError:
properties:
error:
Expand Down Expand Up @@ -185,6 +193,26 @@ paths:
type: object
tags:
- Cart
/cart/overview:
post:
consumes:
- application/json
parameters:
- description: GetCartPayload
in: body
name: payload
required: true
schema:
$ref: '#/definitions/controller.GetCartPayload'
produces:
- application/json
responses:
"200":
description: OK
"500":
description: Internal Server Error
tags:
- Cart
/cart/remove-product:
post:
consumes:
Expand All @@ -210,7 +238,7 @@ paths:
get:
consumes:
- application/json
description: Get all customer's list
description: Overview all customer's list
produces:
- application/json
responses:
Expand All @@ -220,7 +248,7 @@ paths:
items:
$ref: '#/definitions/customer.Customer'
type: array
summary: Get customer list
summary: Overview customer list
tags:
- Customer
post:
Expand Down Expand Up @@ -253,7 +281,7 @@ paths:
get:
consumes:
- application/json
description: Get a customer by cpf
description: Overview a customer by cpf
parameters:
- description: customer cpf
in: path
Expand All @@ -269,7 +297,7 @@ paths:
$ref: '#/definitions/customer.Customer'
"404":
description: Customer not found
summary: Get customer by cpf
summary: Overview customer by cpf
tags:
- Customer
/customer/{id}:
Expand Down
10 changes: 6 additions & 4 deletions internal/adapters/db/cart_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ type PostgresCartProductsRepository struct {

type CartProductsPostgres struct {
BaseModel
CartID uuid.UUID `gorm:"type:uuid" json:"cart_id"`
ProductID uuid.UUID `gorm:"type:uuid" json:"product_id"`
Quantity int `gorm:"quantity"`
Comments string `gorm:"comments"`
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 {
Expand Down
9 changes: 5 additions & 4 deletions internal/adapters/db/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ 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"`
Name string `gorm:"name"`
Cpf string `gorm:"uniqueIndex" json:"cpf"`
Email string `gorm:"email"`
Age int `gorm:"age"`
Orders []OrderPostgres `gorm:"foreignKey:ClientID"`
}

func (cp CustomerPostgres) TableName() string {
Expand Down
12 changes: 5 additions & 7 deletions internal/adapters/db/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type OrderPostgres struct {
ClientID uuid.UUID `gorm:"client_id,type:uuid"`
TotalAmount float64 `gorm:"total_amount"`
Status string `gorm:"status"`
Products []OrderProductPostgres `gorm:"foreignKey:OrderID"`
Customer CustomerPostgres `gorm:"foreignKey:ClientID"`
Products []OrderProductPostgres `gorm:"foreignKey:OrderID"`
}

func (op OrderPostgres) TableName() string {
Expand All @@ -29,12 +29,10 @@ func NewPostgresOrderRepository(db *gorm.DB) order.IOrderRepository {
}

func (r *PostgresOrderRepository) Update(order *order.Order) error {
dbOrder := OrderPostgres{
BaseModel: BaseModel{ID: order.ID},
Status: order.Status,
}

result := r.db.Save(&dbOrder)
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
}
Expand Down
11 changes: 6 additions & 5 deletions internal/adapters/db/order_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ type PostgresOrderProductsRepository struct {

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"`
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 {
Expand Down
2 changes: 2 additions & 0 deletions internal/adapters/rest/controller/abstract_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/gin-gonic/gin"
"github.com/pangolin-do-golang/tech-challenge/internal/errutil"
"log"
)

type AbstractController struct{}
Expand All @@ -13,6 +14,7 @@ type HTTPError struct {
}

func (ctrl *AbstractController) Error(c *gin.Context, err error) {
log.Println(err)
var e *errutil.Error
if errors.As(err, &e) {
switch e.Type {
Expand Down
Loading

0 comments on commit d5dbe16

Please sign in to comment.