Advertisement
erkayes935

customer_controller.go

Dec 25th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.42 KB | Source Code | 0 0
  1. package controller
  2.  
  3. import (
  4.     "challenge-goapi/model"
  5.     "challenge-goapi/usecase"
  6.     "net/http"
  7.  
  8.     "github.com/gin-gonic/gin"
  9. )
  10.  
  11. type CustomerController struct {
  12.     useCase usecase.CustomerUseCase
  13.     rg      *gin.RouterGroup
  14. }
  15.  
  16. func (c *CustomerController) createNewCustomer(g *gin.Context) {
  17.     var payload model.Customer
  18.     if err := g.ShouldBindJSON(&payload); err != nil {
  19.         g.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
  20.         return
  21.     }
  22.  
  23.     customer, err := c.useCase.CreateNewCustomer(payload)
  24.     if err != nil {
  25.         g.JSON(http.StatusInternalServerError, gin.H{"err": "Failed to create new customer"})
  26.         return
  27.     }
  28.  
  29.     g.JSON(http.StatusCreated, customer)
  30. }
  31.  
  32. func (c *CustomerController) getAllCustomer(g *gin.Context) {
  33.     customers, err := c.useCase.FindAllCustomer()
  34.     if err != nil {
  35.         g.JSON(http.StatusInternalServerError, gin.H{"err": "Failed to retrieve data customers"})
  36.         return
  37.     }
  38.  
  39.     if len(customers) > 0 {
  40.         g.JSON(http.StatusOK, customers)
  41.         return
  42.     }
  43.     g.JSON(http.StatusOK, gin.H{"message": "List customers empty"})
  44. }
  45.  
  46. func (c *CustomerController) getCustomerById(g *gin.Context) {
  47.     id := g.Param("id")
  48.     customer, err := c.useCase.FindCustomerById(id)
  49.     if err != nil {
  50.         g.JSON(http.StatusInternalServerError, gin.H{"err": "Failed to get customer by ID"})
  51.         return
  52.     }
  53.  
  54.     g.JSON(http.StatusOK, customer)
  55. }
  56.  
  57. func (c *CustomerController) updateCustomer(g *gin.Context) {
  58.     var payload model.Customer
  59.     if err := g.ShouldBindJSON(&payload); err != nil {
  60.         g.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
  61.         return
  62.     }
  63.  
  64.     customer, err := c.useCase.UpdateCustomer(payload)
  65.     if err != nil {
  66.         g.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
  67.         return
  68.     }
  69.  
  70.     g.JSON(http.StatusOK, customer)
  71. }
  72.  
  73. func (c *CustomerController) deleteCustomer(g *gin.Context) {
  74.     id := g.Param("id")
  75.     err := c.useCase.DeleteCustomer(id)
  76.     if err != nil {
  77.         g.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
  78.         return
  79.     }
  80.  
  81.     g.JSON(http.StatusOK, nil)
  82. }
  83.  
  84. func (c *CustomerController) Route() {
  85.     c.rg.POST("/customers", c.createNewCustomer)
  86.     c.rg.GET("/customers", c.getAllCustomer)
  87.     c.rg.GET("/customers/:id", c.getCustomerById)
  88.     c.rg.PUT("/customers", c.updateCustomer)
  89.     c.rg.DELETE("/customers/:id", c.deleteCustomer)
  90. }
  91.  
  92. func NewCustomerController(useCase usecase.CustomerUseCase, rg *gin.RouterGroup) *CustomerController {
  93.     return &CustomerController{useCase: useCase, rg: rg}
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement