send all files
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package productsmodule
|
||||
|
||||
import (
|
||||
productdto "api/application/products/dto"
|
||||
"api/models/api"
|
||||
)
|
||||
|
||||
// Controller defines the methods to be exposed in Controller layer
|
||||
type Controller interface {
|
||||
CreateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error)
|
||||
UpdateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error)
|
||||
VerifyCode(data *productdto.ValidateCodeDto) (*api.Response, int, error)
|
||||
GetProducts() (*api.Response, int, error)
|
||||
DeleteProduct(data *productdto.DeleteProductDto) (*api.Response, int, error)
|
||||
}
|
||||
|
||||
type controller struct {
|
||||
ProductsService
|
||||
}
|
||||
|
||||
func newController(service ProductsService) Controller {
|
||||
return &controller{
|
||||
ProductsService: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) CreateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error) {
|
||||
return c.ProductsService.CreateProduct(userId, data)
|
||||
}
|
||||
|
||||
func (c *controller) UpdateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error) {
|
||||
return c.ProductsService.UpdateProduct(userId, data)
|
||||
}
|
||||
|
||||
func (c *controller) VerifyCode(data *productdto.ValidateCodeDto) (*api.Response, int, error) {
|
||||
return c.ProductsService.VerifyCode(data)
|
||||
}
|
||||
|
||||
func (c *controller) GetProducts() (*api.Response, int, error) {
|
||||
return c.ProductsService.GetProducts()
|
||||
}
|
||||
|
||||
func (c *controller) DeleteProduct(data *productdto.DeleteProductDto) (*api.Response, int, error) {
|
||||
return c.ProductsService.DeleteProduct(data)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package productdto
|
||||
|
||||
import (
|
||||
"api/libs/logger"
|
||||
"errors"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type DeleteProductDto struct {
|
||||
Codigo string `json:"codigo" validate:"required"`
|
||||
}
|
||||
|
||||
func (d *DeleteProductDto) Validate() error {
|
||||
|
||||
validate := validator.New()
|
||||
|
||||
err := validate.Struct(d)
|
||||
if err != nil {
|
||||
if _, ok := err.(*validator.InvalidValidationError); ok {
|
||||
logger.Development.Info(err.Error())
|
||||
}
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
err = errors.New(e.Field() + " " + e.Tag())
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package productdto
|
||||
|
||||
import (
|
||||
"api/libs/logger"
|
||||
"errors"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type ProductDto struct {
|
||||
Nome string `json:"nome" validate:"required"`
|
||||
Codigo string `json:"codigo" validate:"required"`
|
||||
EstoqueTotal int64 `json:"estoqueTotal" validate:"required"`
|
||||
EstoqueCorte int64 `json:"estoqueCorte" validate:"required"`
|
||||
PrecoDe float32 `json:"precoDe" validate:"required"`
|
||||
PrecoPor float32 `json:"precoPor" validate:"required"`
|
||||
}
|
||||
|
||||
func (d *ProductDto) Validate() error {
|
||||
|
||||
validate := validator.New()
|
||||
|
||||
err := validate.Struct(d)
|
||||
if err != nil {
|
||||
if _, ok := err.(*validator.InvalidValidationError); ok {
|
||||
logger.Development.Info(err.Error())
|
||||
}
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
err = errors.New(e.Field() + " " + e.Tag())
|
||||
}
|
||||
}
|
||||
|
||||
if d.PrecoPor > d.PrecoDe {
|
||||
err = errors.New(`o "preço de" não pode ser inferior ao "preço por"`)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package productdto
|
||||
|
||||
import (
|
||||
"api/libs/logger"
|
||||
"errors"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type ValidateCodeDto struct {
|
||||
Codigo string `json:"codigo" validate:"required"`
|
||||
}
|
||||
|
||||
func (d *ValidateCodeDto) Validate() error {
|
||||
|
||||
validate := validator.New()
|
||||
|
||||
err := validate.Struct(d)
|
||||
if err != nil {
|
||||
if _, ok := err.(*validator.InvalidValidationError); ok {
|
||||
logger.Development.Info(err.Error())
|
||||
}
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
err = errors.New(e.Field() + " " + e.Tag())
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package productsmodule
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
// apply routes in app fiber, with controllers and services defined
|
||||
func ProductstModuleDecorator(app *fiber.App) {
|
||||
s := newService()
|
||||
c := newController(s)
|
||||
newRoutes(c, app)
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package productsmodule
|
||||
|
||||
import (
|
||||
productdto "api/application/products/dto"
|
||||
"api/libs/jwt"
|
||||
"api/libs/logger"
|
||||
"api/models/api"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func newRoutes(c Controller, app *fiber.App) {
|
||||
|
||||
app.Post("/product", jwt.JwtProtected(), createProduct(c))
|
||||
app.Put("/product", jwt.JwtProtected(), updateProduct(c))
|
||||
app.Post("/product/validate", jwt.JwtProtected(), validateCode(c))
|
||||
app.Get("/products", jwt.JwtProtected(), getProducts(c))
|
||||
app.Delete("/product", jwt.JwtProtected(), deleteProduct(c))
|
||||
|
||||
}
|
||||
|
||||
func getProducts(controller Controller) func(*fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
|
||||
response, statusCode, err := controller.GetProducts()
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusInternalServerError)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(statusCode).JSON(response)
|
||||
}
|
||||
}
|
||||
|
||||
func createProduct(controller Controller) func(*fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
userId := jwt.DecodeJwtSingleKey(c, "id").(string)
|
||||
product := &productdto.ProductDto{}
|
||||
|
||||
err := c.BodyParser(product)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = product.Validate()
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
response, statusCode, err := controller.CreateProduct(userId, product)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusInternalServerError)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(statusCode).JSON(response)
|
||||
}
|
||||
}
|
||||
|
||||
func updateProduct(controller Controller) func(*fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
userId := jwt.DecodeJwtSingleKey(c, "id").(string)
|
||||
product := &productdto.ProductDto{}
|
||||
|
||||
err := c.BodyParser(product)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = product.Validate()
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
response, statusCode, err := controller.UpdateProduct(userId, product)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusInternalServerError)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(statusCode).JSON(response)
|
||||
}
|
||||
}
|
||||
|
||||
func validateCode(controller Controller) func(*fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
|
||||
codeValidate := &productdto.ValidateCodeDto{}
|
||||
|
||||
err := c.BodyParser(codeValidate)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = codeValidate.Validate()
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
response, statusCode, err := controller.VerifyCode(codeValidate)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusInternalServerError)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(statusCode).JSON(response)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteProduct(controller Controller) func(*fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
|
||||
delete := &productdto.DeleteProductDto{}
|
||||
|
||||
err := c.BodyParser(delete)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = delete.Validate()
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusBadRequest)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
response, statusCode, err := controller.DeleteProduct(delete)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
c.Status(fiber.StatusInternalServerError)
|
||||
return c.JSON(api.Response{
|
||||
Error: true,
|
||||
ErrorMessage: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(statusCode).JSON(response)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package productsmodule
|
||||
|
||||
import (
|
||||
productdto "api/application/products/dto"
|
||||
"api/libs/logger"
|
||||
"api/libs/postgres"
|
||||
|
||||
"api/models/api"
|
||||
"api/models/products"
|
||||
"context"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type ProductsService interface {
|
||||
CreateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error)
|
||||
UpdateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error)
|
||||
VerifyCode(data *productdto.ValidateCodeDto) (*api.Response, int, error)
|
||||
GetProducts() (*api.Response, int, error)
|
||||
DeleteProduct(data *productdto.DeleteProductDto) (*api.Response, int, error)
|
||||
}
|
||||
|
||||
type productsService struct {
|
||||
productsRepository *products.Queries
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func newService() ProductsService {
|
||||
return &productsService{
|
||||
productsRepository: products.New(postgres.Pool),
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *productsService) CreateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error) {
|
||||
|
||||
estoqueDisponivel := data.EstoqueTotal - data.EstoqueCorte
|
||||
|
||||
newProduct, err := p.productsRepository.CreateProduct(p.ctx, userId, products.CreateProductParams{
|
||||
Nome: data.Nome,
|
||||
Codigo: data.Codigo,
|
||||
EstoqueTotal: data.EstoqueTotal,
|
||||
EstoqueCorte: data.EstoqueCorte,
|
||||
EstoqueDisponivel: estoqueDisponivel,
|
||||
PrecoDe: data.PrecoDe,
|
||||
PrecoPor: data.PrecoPor,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
return &api.Response{Error: true, ErrorMessage: err.Error(), Payload: "error insert product"}, fiber.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return &api.Response{Error: false, Payload: newProduct}, fiber.StatusOK, nil
|
||||
}
|
||||
|
||||
func (p *productsService) VerifyCode(data *productdto.ValidateCodeDto) (*api.Response, int, error) {
|
||||
|
||||
_, err := p.productsRepository.GetProduct(p.ctx, data.Codigo)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return &api.Response{Error: false, Payload: false}, fiber.StatusOK, nil
|
||||
}
|
||||
logger.Production.Info(err.Error())
|
||||
return &api.Response{Error: true, ErrorMessage: err.Error(), Payload: "error verify code product"}, fiber.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return &api.Response{Error: false, Payload: true}, fiber.StatusOK, nil
|
||||
}
|
||||
|
||||
func (p *productsService) UpdateProduct(userId string, data *productdto.ProductDto) (*api.Response, int, error) {
|
||||
|
||||
estoqueDisponivel := data.EstoqueTotal - data.EstoqueCorte
|
||||
|
||||
err := p.productsRepository.UpdateProduct(p.ctx, userId, products.UpdateProductParams{
|
||||
Nome: data.Nome,
|
||||
Codigo: data.Codigo,
|
||||
EstoqueTotal: data.EstoqueTotal,
|
||||
EstoqueCorte: data.EstoqueCorte,
|
||||
EstoqueDisponivel: estoqueDisponivel,
|
||||
PrecoDe: data.PrecoDe,
|
||||
PrecoPor: data.PrecoPor,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
return &api.Response{Error: true, ErrorMessage: err.Error(), Payload: "error update product"}, fiber.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return &api.Response{Error: false, Payload: nil}, fiber.StatusOK, nil
|
||||
}
|
||||
|
||||
func (p *productsService) GetProducts() (*api.Response, int, error) {
|
||||
|
||||
products, err := p.productsRepository.ListProducts(p.ctx)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
return &api.Response{Error: true, ErrorMessage: err.Error(), Payload: "error get products"}, fiber.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return &api.Response{Error: false, Payload: products}, fiber.StatusOK, nil
|
||||
}
|
||||
|
||||
func (p *productsService) DeleteProduct(data *productdto.DeleteProductDto) (*api.Response, int, error) {
|
||||
|
||||
err := p.productsRepository.DeleteProduct(p.ctx, data.Codigo)
|
||||
if err != nil {
|
||||
logger.Production.Info(err.Error())
|
||||
return &api.Response{Error: true, ErrorMessage: err.Error(), Payload: "error delete product"}, fiber.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return &api.Response{Error: false, Payload: nil}, fiber.StatusOK, nil
|
||||
}
|
||||
Reference in New Issue
Block a user