go17/book/api/comment.go

38 lines
872 B
Go
Raw Normal View History

2024-11-23 10:55:52 +08:00
package api
2024-11-23 12:11:44 +08:00
import (
"github.com/gin-gonic/gin"
"gitlab.com/go-course-project/go17/book/config"
"gorm.io/gorm"
)
// 构造函数, 用户初始化这个结构体
func NewCommentApiHandler() *CommentApiHandler {
return &CommentApiHandler{
db: config.Get().MySQL.DB(),
}
}
// 面向对象
// BookApiHandler 他来实现接口的功能
type CommentApiHandler struct {
db *gorm.DB
}
func (h *CommentApiHandler) 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()
}