【Go实战 | 电商平台】(9) 展示商品

简介: 1. 展示商品1.1 路由接口注册GET请求获取商品列表

1. 展示商品

1.1 路由接口注册

GET请求获取商品列表

v1.GET("products", api.ListProducts)


1.2 接口函数编写

1.2.1 service层

创建一个展示商品的结构体

type ListProductsService struct {
  PageNum            int    `form:"pageNum"`
  PageSize           int    `form:"pageSize"`
  CategoryID    uint `form:"category_id" json:"category_id"`
}


创建该结构体下的列表方法

func (service *ListProductsService) List() serializer.Response {
  ...
}


1.2.2 api层

创建一个展示列表服务对象

listProductsService := service.ListProductsService{}


绑定数据

c.ShouldBind(&listProductsService)


调用展示商品列表对象下的展示方法

res := listProductsService.List()


返回上下文

c.JSON(200, res)


api层完整代码

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 服务函数编写

创建一个商品列表模型对象

var products []model.Product


设置分页

if service.PageSize == 0 {
  service.PageSize = 15
}


如果分类传过来是0的话就返回所有的商品

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(),
    }
  }


不为0的话,就返回对应分类的商品

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(),
    }
  }


序列化返回所有的商品,注意要返回所有商品的总数

for _, item := range products {
  products := serializer.BuildProduct(item)
  productList = append(productList, products)
  }
return serializer.BuildListResponse(serializer.BuildProducts(products), uint(total))

1.4 验证服务

发送请求

image.png


获取响应

image.png


相关文章
|
22天前
|
Shell Go API
Go语言grequests库并发请求的实战案例
Go语言grequests库并发请求的实战案例
|
1月前
|
安全 大数据 Go
深入探索Go语言并发编程:Goroutines与Channels的实战应用
在当今高性能、高并发的应用需求下,Go语言以其独特的并发模型——Goroutines和Channels,成为了众多开发者眼中的璀璨明星。本文不仅阐述了Goroutines作为轻量级线程的优势,还深入剖析了Channels作为Goroutines间通信的桥梁,如何优雅地解决并发编程中的复杂问题。通过实战案例,我们将展示如何利用这些特性构建高效、可扩展的并发系统,同时探讨并发编程中常见的陷阱与最佳实践,为读者打开Go语言并发编程的广阔视野。
|
2月前
|
消息中间件 缓存 Kafka
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
|
2月前
|
消息中间件 SQL 关系型数据库
go-zero微服务实战系列(十、分布式事务如何实现)
go-zero微服务实战系列(十、分布式事务如何实现)
|
2月前
|
消息中间件 NoSQL Kafka
go-zero微服务实战系列(九、极致优化秒杀性能)
go-zero微服务实战系列(九、极致优化秒杀性能)
|
13天前
|
Go
Go 语言循环语句
在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句。
24 1
|
2天前
|
存储 Go 容器
深入探究Go语言中的数据结构
深入探究Go语言中的数据结构
10 3
|
12天前
|
Go 开发者
探索Go语言的并发之美
在Go语言的世界里,"并发"不仅仅是一个特性,它是一种哲学。本文将带你领略Go语言中goroutine和channel的魔力,揭示如何通过Go的并发机制来构建高效、可靠的系统。我们将通过一个简单的示例,展示如何利用Go的并发特性来解决实际问题,让你的程序像Go一样,轻盈而强大。
|
13天前
|
JSON Go API
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
|
13天前
|
Go
go语言创建字典
go语言创建字典