feat: attachments manage function is ready

This commit is contained in:
RockYang
2024-01-15 18:48:01 +08:00
parent 9f57fb1421
commit 39fdfae541
30 changed files with 336 additions and 9 deletions

View File

@@ -3,9 +3,13 @@ package handler
import (
"chatplus/core"
"chatplus/service/oss"
"chatplus/store/model"
"chatplus/store/vo"
"chatplus/utils"
"chatplus/utils/resp"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"time"
)
type UploadHandler struct {
@@ -27,5 +31,40 @@ func (h *UploadHandler) Upload(c *gin.Context) {
return
}
userId := h.GetLoginUserId(c)
res := h.db.Create(&model.File{
UserId: userId,
Name: file.Name,
URL: file.URL,
Ext: file.Ext,
Size: file.Size,
CreatedAt: time.Time{},
})
if res.Error != nil || res.RowsAffected == 0 {
resp.ERROR(c, "error with update database: "+res.Error.Error())
return
}
resp.SUCCESS(c, file)
}
func (h *UploadHandler) List(c *gin.Context) {
userId := h.GetLoginUserId(c)
var items []model.File
var files = make([]vo.File, 0)
h.db.Debug().Where("user_id = ?", userId).Find(&items)
if len(items) > 0 {
for _, v := range items {
var file vo.File
err := utils.CopyObject(v, &file)
if err != nil {
logger.Error(err)
continue
}
file.CreatedAt = v.CreatedAt.Unix()
files = append(files, file)
}
}
resp.SUCCESS(c, files)
}