【Go实战 | 电商平台】(11) 更新商品

简介: 文章目录1. 更新商品1.1 路由接口注册1.2 接口函数编写1.2.1 service层1.2.2 api层1.3 服务函数编写1.4 验证服务

文章目录

1. 更新商品

1.1 路由接口注册

1.2 接口函数编写

1.2.1 service层

1.2.2 api层

1.3 服务函数编写

1.4 验证服务

1. 更新商品

1.1 路由接口注册

authed.PUT("product/:id", api.UpdateProduct)


1.2 接口函数编写

1.2.1 service层

创建更新商品的服务

type UpdateProductService struct {
  ID            uint   `form:"id" json:"id"`
  Name          string `form:"name" json:"name"`
  CategoryID    int    `form:"category_id" json:"category_id"`
  Title         string `form:"title" json:"title" binding:"required,min=2,max=100"`
  Info          string `form:"info" json:"info" binding:"max=1000"`
  ImgPath       string `form:"img_path" json:"img_path"`
  Price         string `form:"price" json:"price"`
  DiscountPrice string `form:"discount_price" json:"discount_price"`
  OnSale bool `form:"on_sale" json:"on_sale"`
  Num string `form:"num" json:"num"`
}


创建更新的商品服务的方法

func (service *UpdateProductService) Update(id string) serializer.Response {
  ...
}


1.2.2 api层

创建一个更新商品服务的对象

updateProductService := service.UpdateProductService{}


上下文绑定更新商品服务对象

c.ShouldBind(&updateProductService)


调用这个更新服务对象的方法,注意这个id是路由上面的那个id

res := updateProductService.Update(c.Param("id"))


上下文返回

c.JSON(200, res)


完整代码

func UpdateProduct(c *gin.Context) {
  updateProductService := service.UpdateProductService{}
  if err := c.ShouldBind(&updateProductService); err == nil {
  res := updateProductService.Update(c.Param("id"))
  c.JSON(200, res)
  } else {
  c.JSON(200, ErrorResponse(err))
  logging.Info(err)
  }
}


1.3 服务函数编写

找到商品分类

var category model.Category
  model.DB.Model(&model.Category{}).First(&category,service.CategoryID)


找到该商品

var product model.Product
  model.DB.Model(&model.Product{}).First(&product,id)

修改商品信息

product.Name=service.Name
  product.Category=category
  product.CategoryID=uint(service.CategoryID)
  product.Title=service.Title
  product.Info=service.Info
  product.ImgPath=service.ImgPath
  product.Price=service.Price
  product.DiscountPrice=service.DiscountPrice
  product.OnSale=service.OnSale


保存数据库中

err := model.DB.Save(&product).Error


返回信息

return serializer.Response{
  Status: code,
  Msg:    e.GetMsg(code),
  }

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语言创建字典