2024-11-23 12:11:44 +08:00
|
|
|
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() (<Book>, error)
|
|
|
|
// controller.GetBook()
|
|
|
|
|
|
|
|
// h.book.GetBook(ctx, isbn)
|
2024-12-01 09:52:50 +08:00
|
|
|
// exception.IsNotFound(err)
|
2024-11-23 12:11:44 +08:00
|
|
|
}
|