go练习 day01

DTO: note_dto.go

go 复制代码
package dto

import "king/model"

type NoteAddDTO struct {
	ID        uint
	Title     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.Title
	iNote.Content = m.Content
	iNote.UserID = m.UserID
}

Model:note.go

go 复制代码
package model

import "gorm.io/gorm"

type Note struct {
	gorm.Model
	Title    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

go 复制代码
package dao

import (
	"king/model"
	"king/service/dto"
)

var noteDao *NoteDao

type 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.Note
	iNoteAddDTO.ConverToModel(&iNote)

	err := m.Orm.Create(&iNote).Error
	if err == nil {
		iNoteAddDTO.ID = iNote.ID
	}

	return err
}

Service:note_service.go

go 复制代码
package service

import (
	"king/dao"
	"king/service/dto"
)

var noteService *NoteService

type NoteService struct {
	BaseService
	Dao *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

go 复制代码
package api

import (
	"king/service"
	"king/service/dto"
	"net/http"

	"github.com/gin-gonic/gin"
)

const (
	ERR_CODE_ADD_NOTE       = 10021
)

type NoteApi struct {
	BaseApi
	Service *service.NoteService
}

func NewNoteApi() NoteApi{
	return NoteApi{
		BaseApi: NewBaseApi(),
		Service: service.NewNoteService(),
	}
}

func (m NoteApi)AddNote(c *gin.Context) {
	var iNoteAddDTO dto.NoteAddDTO
	if 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

go 复制代码
package router

import (
	"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

go 复制代码
func initBasePlatformRoutes() {
	InitNoteRoutes()
}

db:AutoMigrate

go 复制代码
db.AutoMigrate(&model.Note{})

效果


相关推荐
aramae6 分钟前
详细分析平衡树--红黑树(万字长文/图文详解)
开发语言·数据结构·c++·笔记·算法
一百天成为python专家9 分钟前
python爬虫入门(小白五分钟从入门到精通)
开发语言·爬虫·python·opencv·yolo·计算机视觉·正则表达式
Mr YiRan17 分钟前
多线程性能优化基础
android·java·开发语言·性能优化
熊猫钓鱼>_>23 分钟前
Java String 性能优化与内存管理:现代开发实战指南
java·开发语言·性能优化
练习时长一年28 分钟前
Spring容器的refresh()方法
java·开发语言
h79971036 分钟前
go资深之路笔记(九)kafka浅析
笔记·golang·kafka
Yeats_Liao40 分钟前
Go Web 编程快速入门 02 - 认识 net/http 与 Handler 接口
前端·http·golang
Arlene42 分钟前
JVM Java虚拟机
java·开发语言·jvm
千码君201644 分钟前
Go语言:关于导包的两个重要说明
开发语言·后端·golang·package·导包
88号技师1 小时前
2025年8月SCI-汉尼拔·巴卡优化算法Hannibal Barca optimizer-附Matlab免费代码
开发语言·人工智能·算法·数学建模·matlab·优化算法