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

效果


相关推荐
清水白石0082 分钟前
《精通 Python 设计模式:从入门理解到实战落地》
开发语言·python
五阿哥永琪3 分钟前
Nacos注册/配置中心
java·开发语言
无敌最俊朗@5 分钟前
Qt多线程阻塞:为何信号失效?
java·开发语言
ii_best7 分钟前
「安卓开发辅助工具按键精灵」xml全分辨率插件jsd插件脚本教程
android·xml·开发语言·编辑器·安卓
李慕婉学姐8 分钟前
【开题答辩过程】以《基于python的气象灾害数据分析与可视化系统》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
开发语言·python·数据分析
一起养小猫16 分钟前
《Java数据结构与算法》第四篇(三)二叉树遍历详解_CSDN文章
java·开发语言·数据结构
消失的旧时光-194320 分钟前
从 Android 回调到 C 接口:函数指针 + void* self 的一次彻底理解
android·c语言·开发语言
我命由我1234522 分钟前
Python Flask 开发问题:ImportError: cannot import name ‘escape‘ from ‘flask‘
服务器·开发语言·后端·python·flask·学习方法·python3.11
elangyipi12329 分钟前
JavaScript 高级错误处理与 Chrome 调试艺术
开发语言·javascript·chrome