优秀的编程知识分享平台

网站首页 > 技术文章 正文

11.Go语言编写个人博客 标签(go搭建博客)

nanyue 2024-09-09 04:59:10 技术文章 9 ℃

一篇文章可以有多个标签,一个标签也可以对应多篇文章。这种关系称为“多对多”关系。在数据库设计中,通常通过创建一个中间关联表或联结表(junction table)来实现多对多关系。

这个中间表至少包含两个字段:一个是文章ID,另一个是标签ID。每条记录代表了一篇文章和一个标签之间的关联。这样,一篇文章可以通过这个中间表关联到多个标签,每个标签也可以关联到多篇文章。在 ORM(对象关系映射)中,这种关系也可以通过特定的关系定义来处理,在 GORM 中可以使用 Many2Many 关系。

本节中我只把标签的创建部分完成。它修改自分类的创建,这里就不再对代码进行解释。如果有不懂的,可以查看上一节的内容“Go语言编写个人博客 文章分类”:10.Go语言编写个人博客 文章分类-今日头条 (toutiao.com)

代码如下:

api/tag.go

package api

import (
	"errors"
	"net/http"
	"xblog/models"

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

// CreateTag 创建新标签
func CreateTag(c *gin.Context) {
	// 从上下文中获取当前用户
	currentUser, _ := c.Get("currentUser")
	user, ok := currentUser.(*models.JwtClaims)
	if !ok {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户认证信息"})
		return
	}

	// 获取用户ID
	userID := user.UserID

	// 查询用户信息并获取用户角色,使用指针调用 GetRole 方法
	var userObj models.User
	result := models.DB.Where("id = ?", userID).First(&userObj)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
		return
	} else if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error})
		return
	}

	role, err := userObj.GetRole()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// 检查用户角色,只有为 1 时才有权限
	if role != 1 {
		c.JSON(http.StatusForbidden, gin.H{"error": "无权创建标签"})
		return
	}

	// 初始化 Tag 结构体
	var tag models.Tag

	// 绑定请求体到 Tag 结构体
	if err := c.ShouldBindJSON(&tag); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
		return
	}

	// 保存标签到数据库
	if result := models.DB.Create(&tag); result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
		return
	}

	// 返回创建的标签
	c.JSON(http.StatusOK, gin.H{"tag": tag})
}

models/tag.go

package models

import (
	"gorm.io/gorm"
)

type Tag struct {
	gorm.Model
	Name     string    `gorm:"type:varchar(63);not null" json:"name"`
	Articles []Article `gorm:"many2many:article_tags;"`
}

models/article.go

package models

import (
	"gorm.io/gorm"
)

type Article struct {
	gorm.Model
	Title      string   `gorm:"type:varchar(255);not null" json:"title" form:"title"`
	Content    string   `gorm:"type:text;not null" json:"content" form:"content"`
	UserID     uint     `gorm:"not null" json:"user_id"`
	User       User     `gorm:"foreignKey:UserID"`
	CoverImage string   `gorm:"type:varchar(255)" json:"cover_image"`
	Images     []Image  `gorm:"foreignKey:ArticleID"`
	CategoryID uint     `gorm:"not null" json:"category_id" form:"category_id"`
	Category   Category `gorm:"foreignKey:CategoryID"`
	Tags       []Tag    `gorm:"many2many:article_tags;"`
}

type Image struct {
	gorm.Model
	URL       string `gorm:"type:varchar(255);not null" json:"url"`
	ArticleID uint   `gorm:"not null" json:"article_id"`
}

测试命令:curl.sh

登陆并获取token

curl -X POST http://10.0.0.185:8000/api/v1/user/login \
-H "Content-Type: application/json" \
-d '{"username":"user1","password":"passwd123"}'; echo

携带Token创建标签

curl -X POST http://10.0.0.185:8000/api/v1/tag/create \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyLCJ1c2VybmFtZSI6InVzZXIxIiwiZXhwIjoxNzE1MDU5OTI0LCJpc3MiOiJ4YmxvZyJ9.0uBWCdlIvJFkNzNFeIJ6WKGlGEmWiQDIQ9hJ-ZBrj9Q" \
-d '{"name":"IPv4"}'; echo

大家可以根据以上代码自行测试。

Tags:

最近发表
标签列表