当前位置: 首页 > news >正文

go练习 day01

DTO: note_dto.go

package dtoimport "king/model"type NoteAddDTO struct {ID        uintTitle     string  `json:"title" form:"title" binding:"required" message:"标题不能为空"`Content   string  `json:"content" form:"content" binding:"required" message:"内容不能为空"`UserID    uint    `json:"userId" form:"userId" binding:"required" message:"用户ID不能为空"`
}func (m *NoteAddDTO)ConverToModel (iNote *model.Note) {iNote.Title = m.TitleiNote.Content = m.ContentiNote.UserID = m.UserID
}

Model:note.go

package modelimport "gorm.io/gorm"type Note struct {gorm.ModelTitle    string `json:"title" gorm:"size:64;not null"`Content  string `json:"content" gorm:"not null"`UserID   uint   `json:"user_id" gorm:"not null"`
}

Dao:note_dao.go

package daoimport ("king/model""king/service/dto"
)var noteDao *NoteDaotype NoteDao struct {BaseDao
}func NewNoteDao() *NoteDao {if noteDao == nil {noteDao = &NoteDao{NewBaseDao(),}}return noteDao
}func (m *NoteDao) AddNote(iNoteAddDTO *dto.NoteAddDTO) error{var iNote model.NoteiNoteAddDTO.ConverToModel(&iNote)err := m.Orm.Create(&iNote).Errorif err == nil {iNoteAddDTO.ID = iNote.ID}return err
}

Service:note_service.go

package serviceimport ("king/dao""king/service/dto"
)var noteService *NoteServicetype NoteService struct {BaseServiceDao *dao.NoteDao
}func NewNoteService() *NoteService{if noteService == nil {noteService = &NoteService{Dao: dao.NewNoteDao(),}}return noteService
}func (m *NoteService) AddNote(iNoteAddDTO *dto.NoteAddDTO) error{return m.Dao.AddNote(iNoteAddDTO)
}

API:note_api.go

package apiimport ("king/service""king/service/dto""net/http""github.com/gin-gonic/gin"
)const (ERR_CODE_ADD_NOTE       = 10021
)type NoteApi struct {BaseApiService *service.NoteService
}func NewNoteApi() NoteApi{return NoteApi{BaseApi: NewBaseApi(),Service: service.NewNoteService(),}
}func (m NoteApi)AddNote(c *gin.Context) {var iNoteAddDTO dto.NoteAddDTOif err := m.BuildRequest(BuildRequestOption{Ctx: c, DTO: &iNoteAddDTO}).GetError(); err != nil {return }err := m.Service.AddNote(&iNoteAddDTO)if err != nil {m.ServerFail(ResponseJson{Code: ERR_CODE_ADD_NOTE,Status: http.StatusBadRequest,Msg: "新增文章失败",})return}m.OK(ResponseJson{Code: SUCCESS,Msg: "新增文章成功",Data: iNoteAddDTO,})
}

Router:note.go

package routerimport ("king/api""github.com/gin-gonic/gin"
)func InitNoteRoutes() {RegistRoute(func(rgPublic *gin.RouterGroup, rgAuth *gin.RouterGroup) {noteApi := api.NewNoteApi()rgAuthNote := rgAuth.Group("note"){rgAuthNote.POST("", noteApi.AddNote)}})
}

Routers:router.go

func initBasePlatformRoutes() {InitNoteRoutes()
}

db:AutoMigrate

db.AutoMigrate(&model.Note{})

效果

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/112058.html

相关文章:

  • C# Blazor 学习笔记(0.1):如何开始Blazor和vs基本设置
  • 原码的乘法运算 补码乘法运算
  • 找不到d3dx9_43.dll丢失怎么解决(分享几种解决方法)
  • 篇四:建造者模式:逐步构造复杂对象
  • vs导出和导入动态库和静态库
  • 30 使用easyExcel依赖生成Excel
  • 排序进行曲-v2.0
  • 反弹shell的N种姿势
  • 创意视频剪辑教程:快速合并视频并标题,让你的作品更吸睛!
  • 解决Hadoop审计日志hdfs-audit.log过大的问题
  • 【Java】java和kotlin关于Json写文件
  • 【深度学习】采用自动编码器生成新图像
  • 华为云交付
  • dns瞅一瞅
  • springAOP的实例
  • 【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)
  • P1320 压缩技术(续集版)
  • k8s(七) 叩丁狼 service Ingress
  • Android Studio 关于BottomNavigationView 无法预览视图我的解决办法
  • 【STM32】小电流FOC驱控一体板(开源)
  • 代码分析:循环创建N个子进程——为什么最后一个属于父进程?
  • 【SpringBoot面试题整理-超级有效】
  • 岩土工程仪器多通道振弦传感器信号转换器应用于隧道安全监测
  • 西瓜书读书笔记整理(五)—— 第四章 决策树
  • STM32 4G学习
  • Golang 中实现实时聊天通讯
  • 前端面试的性能优化部分(5)每天10个小知识点
  • 【链表OJ 1】移除链表元素val
  • 复原 IP 地址——力扣93
  • OSPF综合实验