go练习 day01

DTO: note_dto.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
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
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
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
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
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
func initBasePlatformRoutes() {
	InitNoteRoutes()
}

db:AutoMigrate
db.AutoMigrate(&model.Note{})
效果

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值