38 lines
872 B
Go
38 lines
872 B
Go
package api
|
|
|
|
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()
|
|
}
|