【Go Actual | E-commerce Platform】(9) Display Products

1. Display products

1.1 Routing interface registration

  • GET request to get the product list
v1.GET("products", api.ListProducts)

1.2 Writing interface functions

1.2.1 service layer

  • Create a struct that displays the item
type ListProductsService struct {
    
    
	PageNum     					  int 	  `form:"pageNum"`
	PageSize    					  int 	  `form:"pageSize"`
	CategoryID 		uint `form:"category_id" json:"category_id"`
}
  • Create a list method under this structure
func (service *ListProductsService) List() serializer.Response {
    
    
	...
}

1.2.2 api layers

  • Create a display list service object
listProductsService := service.ListProductsService{
    
    }
  • bind data
c.ShouldBind(&listProductsService)
  • Call the display method under the display product list object
res := listProductsService.List()
  • back to context
c.JSON(200, res)
  • API layer complete code
func ListProducts(c *gin.Context) {
    
    
	listProductsService := service.ListProductsService{
    
    }
	if err := c.ShouldBind(&listProductsService); err == nil {
    
    
		res := listProductsService.List()
		c.JSON(200, res)
	} else {
    
    
		c.JSON(200, ErrorResponse(err))
		logging.Info(err)
	}
}

1.3 Writing service functions

  • Create an item list model object
var products []model.Product
  • set pagination
if service.PageSize == 0 {
    
    
	service.PageSize = 15
}
  • If the category is 0, then return all products
if service.CategoryID == 0 {
    
    
		if err := model.DB.Model(model.Product{
    
    }).
			Count(&total).Error; err != nil {
    
    
			logging.Info(err)
			code = e.ErrorDatabase
			return serializer.Response{
    
    
				Status: code,
				Msg:    e.GetMsg(code),
				Error:  err.Error(),
			}
		}
		if err := model.DB.Offset((service.PageNum - 1) * service.PageSize).
			Limit(service.PageSize).Find(&products).
			Error; err != nil {
    
    
			logging.Info(err)
			code = e.ErrorDatabase
			return serializer.Response{
    
    
				Status: code,
				Msg:    e.GetMsg(code),
				Error:  err.Error(),
			}
		}
  • If it is not 0, it will return the product of the corresponding category
if err := model.DB.Model(model.Product{
    
    }).Preload("Category").
			Where("category_id = ?", service.CategoryID).
			Count(&total).Error; err != nil {
    
    
			logging.Info(err)
			code = e.ErrorDatabase
			return serializer.Response{
    
    
				Status: code,
				Msg:    e.GetMsg(code),
				Error:  err.Error(),
			}
		}
  • Serialization returns all products, pay attention to return the total number of all products
for _, item := range products {
    
    
		products := serializer.BuildProduct(item)
		productList = append(productList, products)
	}
return serializer.BuildListResponse(serializer.BuildProducts(products), uint(total))

1.4 Authentication Service

  • send request

insert image description here

  • get response

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45304503/article/details/121551890