package controller import ( "github.com/gin-gonic/gin" "gitlab.com/go-course-project/go17/book/config" "gorm.io/gorm" ) // 构造函数, 用户初始化这个结构体 func NewCommentController() *CommentController { return &CommentController{ db: config.Get().MySQL.DB(), book: NewBookController(), } } type CommentController struct { db *gorm.DB book *BookController } func (h *CommentController) AddComment(ctx *gin.Context) { // book id, user a, comment // book id // 判断book id 是不是合法, 到底有没有这本book // 1. NewBookHandler().GetBook(ctx) // 2. 把这个逻辑再写一套 // if err := h.db.Where("isbn = ?", id).Take(&ins).Error; err != nil { // response.Failed(ctx, err) // return // } // 你需要什么? // 你需要一个 功能(业务处理逻辑): GetBook() (, error) // controller.GetBook() // h.book.GetBook(ctx, isbn) // exception.IsNotFound(err) }