rag/internal/handlers/controllers/library/library.go
2024-07-21 01:20:52 +08:00

75 lines
1.8 KiB
Go

package library
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"leafdev.top/leaf/rag/internal/logic"
"leafdev.top/leaf/rag/internal/providers/helper"
"leafdev.top/leaf/rag/models"
"net/http"
)
type Controller struct {
}
func NewLibraryController() *Controller {
return &Controller{}
}
var libraryLogic = logic.NewLibraryLogic()
// Library 获取当前用户的资料库 godoc
// @Summary 获取当前用户的资料库
// @Success 200 {array} models.ListLibraryResponse
// @Router /library [get]
//func (c *Controller) Library(ctx *gin.Context) {
// libraries, _ := libraryLogic.ListLibrary(ctx)
//
// libraryResponse := models.ListLibraryResponse{
// Libraries: libraries,
// }
//
// //helper.Response(ctx, http.StatusOK, libraryResponse)
// ctx.JSON(http.StatusOK, libraryResponse)
//}
// CreateLibrary 创建一个资料库 godoc
// @Summary 创建一个资料库
// @Security ApiKeyAuth
// @Success 200 {object} models.CreateLibraryResponse
// @Router /library [post]
func (c *Controller) CreateLibrary(ctx *gin.Context) {
var createLibraryReq models.CreateLibraryRequest
err := ctx.ShouldBindBodyWithJSON(&createLibraryReq)
if err != nil {
helper.ResponseError(ctx, http.StatusBadRequest, err)
return
}
library, err := libraryLogic.CreateLibrary(ctx, createLibraryReq.Name)
if err != nil {
helper.ResponseError(ctx, http.StatusInternalServerError, err)
return
}
//helper.Response(ctx, http.StatusCreated, models.CreateLibraryResponse{
// Library: library,
//})
ctx.JSON(http.StatusOK, models.CreateLibraryResponse{
Library: library,
})
}
func MustBindBodyWithJSON(ctx *gin.Context, obj interface{}) error {
err := ctx.ShouldBindBodyWith(obj, binding.JSON)
if err != nil {
helper.ResponseError(ctx, http.StatusBadRequest, err)
return err
}
return err
}