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{})

效果


相关推荐
百锦再13 分钟前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
Anson Jiang13 分钟前
浏览器标签页管理:使用chrome.tabs API实现新建、切换、抓取内容——Chrome插件开发从入门到精通系列教程06
开发语言·前端·javascript·chrome·ecmascript·chrome devtools·chrome插件
会开花的二叉树22 分钟前
继承与组合:C++面向对象的核心
java·开发语言·c++
长河2 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
Cyan_RA92 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
再见晴天*_*5 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08277 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge9 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘9 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务9 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟