Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lgpd exclusion api #28

Merged
merged 14 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions docs/tech-challenge.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,64 @@
}
}
}
},
"/customers/lgpd-removal": {
"post": {
"tags": [
"Cliente"
],
"summary": "Cria solicitação de remoção dados LGPD",
"description": "Endpoint responsável que cria uma solicitação para remoção dados LGPD.",
"operationId": "create-lgpd-removal-request-customer",
"requestBody": {
"description": "Post the necessary fields for the API to create a new removal request.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Customer_lgpd_removal_request"
},
"examples": {
"Criar uma solicitação": {
"value": {
"name": "Fulano de tal",
"address": "rua xpto 123, bairro xpto",
"phone_number": "54984565122",
"payment_history": true
}
}
}
}
}
},
"responses": {
"201": {
"description": "Solicitação Criada",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Customer_lgpd_removal_request"
},
"examples": {
"Criar uma solicitação": {
"value": {
"name": "Fulano de tal",
"address": "rua xpto 123, bairro xpto",
"phone_number": "54984565122",
"payment_history": true
}
}
}
}
}
},
"400": {
"description": "Erro de BadRequest"
},
"500": {
"description": "Erro de ServerError"
}
}
}
}
},
"tags": [
Expand Down Expand Up @@ -290,6 +348,50 @@
"cpf",
"email"
]
},
"Customer_lgpd_removal_request": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "Fulano de tal"
},
"address": {
"type": "string",
"example": "rua xpto 123, bairro xpto"
},
"phone_number": {
"type": "string",
"example": "54984565122"
},
"payment_history": {
"type": "boolean",
"example": true
}
},
"required": [
"name",
"address",
"phone_number",
"payment_history"
]
},
"ErrorSchema": {
"title": "ErrorSchema",
"ErrorSchema": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"name": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
}
}
}
Expand Down
41 changes: 40 additions & 1 deletion src/adapters/controllers/customer/customer_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"net/http"

"github.com/CAVAh/api-tech-challenge/src/core/domain/dtos"
Expand All @@ -12,7 +13,14 @@ import (
func ListCustomers(c *gin.Context, usecase *usecases.ListCustomerUsecase) {
var inputDto dtos.ListCustomerDto

c.BindQuery(&inputDto)
err := c.BindQuery(&inputDto)

if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}

if err := validator.Validate(inputDto); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
Expand Down Expand Up @@ -61,3 +69,34 @@ func CreateCustomer(c *gin.Context, usecase *usecases.CreateCustomerUsecase) {

c.JSON(http.StatusCreated, result)
}

func CreateLgpdRemovalRequestCustomer(c *gin.Context, usecase *usecases.CreateLgpdRemovalRequestUsecase) {
var inputDto dtos.LGPDRemovalRequestDto

if err := c.ShouldBindJSON(&inputDto); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
fmt.Printf("error binding json: %v\n", err)
return
}

if err := validator.Validate(inputDto); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
fmt.Printf("error with validation json: %v\n", err)
return
}

result, err := usecase.Execute(inputDto)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}

c.JSON(http.StatusCreated, result)
}
7 changes: 7 additions & 0 deletions src/adapters/gateways/lgpd_removal_request_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gateways

import "github.com/CAVAh/api-tech-challenge/src/core/domain/entities"

type LgpdRemovalRequestRepository interface {
Create(removalRequest *entities.LgpdRemovalRequest) (*entities.LgpdRemovalRequest, error)
}
8 changes: 8 additions & 0 deletions src/core/domain/dtos/lgpd_removal_request_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dtos

type LGPDRemovalRequestDto struct {
Name string `json:"name" validate:"nonzero,min=10,max=255"`
Address string `json:"address" validate:"nonzero,min=10,max=255"`
PhoneNumber string `json:"phone_number" validate:"len=11"`
PaymentHistory bool `json:"payment_history" validate:"nonnil"`
}
10 changes: 10 additions & 0 deletions src/core/domain/entities/lgpd_removal_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package entities

type LgpdRemovalRequest struct {
ID uint `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
PhoneNumber string `json:"phone_number"`
PaymentHistory bool `json:"payment_history"`
CreatedAt string `json:"createdAt"`
}
22 changes: 22 additions & 0 deletions src/core/domain/usecases/customer/create_lgpd_removal_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package usecases

import (
"github.com/CAVAh/api-tech-challenge/src/adapters/gateways"
"github.com/CAVAh/api-tech-challenge/src/core/domain/dtos"
"github.com/CAVAh/api-tech-challenge/src/core/domain/entities"
)

type CreateLgpdRemovalRequestUsecase struct {
LgpdRemovalRequestRepository gateways.LgpdRemovalRequestRepository
}

func (r *CreateLgpdRemovalRequestUsecase) Execute(inputDto dtos.LGPDRemovalRequestDto) (*entities.LgpdRemovalRequest, error) {
removalRequest := entities.LgpdRemovalRequest{
Name: inputDto.Name,
Address: inputDto.Address,
PhoneNumber: inputDto.PhoneNumber,
PaymentHistory: inputDto.PaymentHistory,
}

return r.LgpdRemovalRequestRepository.Create(&removalRequest)
}
4 changes: 2 additions & 2 deletions src/infra/db/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (rdb *RealDatabase) First(dest interface{}, conds ...interface{}) error {
}

func ConnectDB() {
conectionString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=5432 sslmode=require TimeZone=America/Fortaleza",
conectionString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=5432 sslmode=disable TimeZone=America/Fortaleza",
os.Getenv("POSTGRES_HOST"), os.Getenv("POSTGRES_USER"), os.Getenv("POSTGRES_PASSWORD"), os.Getenv("POSTGRES_DB"))

db, err := gorm.Open(postgres.Open(conectionString))
Expand All @@ -50,7 +50,7 @@ func ConnectDB() {
db: db,
}

err = db.AutoMigrate(&models.Customer{})
err = db.AutoMigrate(&models.Customer{}, &models.LgpdRemovalRequest{})
if err != nil {
log.Panic("Erro ao fazer auto migrate")
}
Expand Down
26 changes: 26 additions & 0 deletions src/infra/db/models/lgpd_request_removal_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package models

import (
"github.com/CAVAh/api-tech-challenge/src/core/domain/entities"
"github.com/CAVAh/api-tech-challenge/src/utils"
"gorm.io/gorm"
)

type LgpdRemovalRequest struct {
gorm.Model
Name string
Address string
PhoneNumber string
PaymentHistory bool
}

func (c LgpdRemovalRequest) ToDomain() entities.LgpdRemovalRequest {
return entities.LgpdRemovalRequest{
ID: c.ID,
Name: c.Name,
Address: c.Address,
PhoneNumber: c.PhoneNumber,
PaymentHistory: c.PaymentHistory,
CreatedAt: c.CreatedAt.Format(utils.CompleteEnglishDateFormat),
}
}
35 changes: 35 additions & 0 deletions src/infra/db/repositories/lgpd_removal_request_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package repositories

import (
"errors"
"github.com/CAVAh/api-tech-challenge/src/core/domain/entities"
"github.com/CAVAh/api-tech-challenge/src/infra/db/database"
"github.com/CAVAh/api-tech-challenge/src/infra/db/models"
"strings"
)

type LgpdRemovalRequestRepository struct {
DB database.Database
}

func (r LgpdRemovalRequestRepository) Create(removalRequest *entities.LgpdRemovalRequest) (*entities.LgpdRemovalRequest, error) {

removalRequestModel := models.LgpdRemovalRequest{
Name: removalRequest.Name,
Address: removalRequest.Address,
PhoneNumber: removalRequest.PhoneNumber,
PaymentHistory: removalRequest.PaymentHistory,
}

if err := r.DB.Create(&removalRequestModel); err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, errors.New("solicitação de exclusão já existe no sistema")
} else {
return nil, errors.New("ocorreu um erro desconhecido ao criar a solicitação")
}
}

result := removalRequestModel.ToDomain()

return &result, nil
}
10 changes: 10 additions & 0 deletions src/infra/web/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ func HandleRequests() {
customerRepository := &repositories.CustomerRepository{
DB: database.DB,
}

lgpdRemovalRequestRepository := &repositories.LgpdRemovalRequestRepository{
DB: database.DB,
}

listUsecase := &usecases.ListCustomerUsecase{CustomerRepository: customerRepository}
createUsecase := &usecases.CreateCustomerUsecase{CustomerRepository: customerRepository}
createLgpdRemovalRequest := &usecases.CreateLgpdRemovalRequestUsecase{LgpdRemovalRequestRepository: lgpdRemovalRequestRepository}

router.GET("/customers", func(c *gin.Context) {
controllers.ListCustomers(c, listUsecase)
Expand All @@ -26,6 +32,10 @@ func HandleRequests() {
controllers.CreateCustomer(c, createUsecase)
})

router.POST("/customers/lgpd-removal", func(c *gin.Context) {
controllers.CreateLgpdRemovalRequestCustomer(c, createLgpdRemovalRequest)
})

err := router.Run()

if err != nil {
Expand Down
Loading