pastebin/services/controller/v1/pastebin_controller.go

184 lines
4.9 KiB
Go
Raw Normal View History

2024-03-18 17:03:14 +00:00
package controllerv1
2023-09-30 13:47:59 +00:00
import (
2024-03-18 17:03:14 +00:00
"time"
2023-09-30 13:47:59 +00:00
"github.com/sirupsen/logrus"
2023-10-15 13:30:40 +00:00
"gitter.top/coco/coco"
2024-03-18 17:03:14 +00:00
2023-09-30 13:47:59 +00:00
"pastebin/common"
2024-03-18 17:03:14 +00:00
genv1 "pastebin/gen/v1"
2023-09-30 13:47:59 +00:00
"pastebin/internal/repositories"
)
type PastebinService struct{}
2024-03-18 17:03:14 +00:00
// IDE: PastebinService implemented genv1.AutoGenPastebinServiceImpl interface
var _ genv1.AutoGenPastebinServiceImpl = (*PastebinService)(nil)
2023-09-30 13:47:59 +00:00
// List 列表
2024-03-18 17:03:14 +00:00
func (receiver *PastebinService) List(ctx *coco.Context, req *genv1.PastebinServiceListReq) (resp *genv1.PastebinServiceListResp, err error) {
resp = new(genv1.PastebinServiceListResp)
2023-09-30 13:47:59 +00:00
if req.PageSize == 0 {
req.PageSize = 25
}
var limit = req.PageSize + 1
var offset int64
if req.CurrentPage == 0 {
req.CurrentPage = 1
}
offset = (req.CurrentPage - 1) * req.PageSize
db := repositories.GetPastebin()
2024-03-18 17:03:14 +00:00
var records []genv1.ModelPastebin
2023-09-30 13:47:59 +00:00
if err := db.Find().SetWhere("expired_at > ?", time.Now().Unix()).SetLimit(int(limit)).
SetOffset(int(offset)).SetOrder("id desc").Find(&records); err != nil {
logrus.Errorf("query records failed: %v", err)
return nil, err
}
var hasMore bool
if len(records) >= int(limit) {
hasMore = true
records = records[:req.PageSize]
}
for _, record := range records {
2024-03-18 17:03:14 +00:00
item := &genv1.PastebinServiceRecord{
2023-09-30 13:47:59 +00:00
Id: record.Id,
Key: record.ShortId,
Title: record.Title,
Lang: record.Lang,
Author: record.Author,
CreatedAt: common.Unix2Datetime(record.CreatedAt),
ExpiredAt: common.Unix2Datetime(record.ExpiredAt),
NeedPassword: record.NeedPassword,
}
resp.Items = append(resp.Items, item)
}
resp.HasMore = hasMore
return resp, nil
}
// Add 新建
2024-03-18 17:03:14 +00:00
func (receiver *PastebinService) Add(ctx *coco.Context, req *genv1.PastebinServiceAddReq) (resp *genv1.PastebinServiceAddResp, err error) {
resp = new(genv1.PastebinServiceAddResp)
2023-09-30 13:47:59 +00:00
var expiredAt = time.Now()
switch req.Record.ExpireLevel {
2024-03-18 17:03:14 +00:00
case genv1.ExpireLevel_Forever:
2023-09-30 13:47:59 +00:00
expiredAt = expiredAt.Add(time.Hour * 87600)
2024-03-18 17:03:14 +00:00
case genv1.ExpireLevel_Day:
2023-09-30 13:47:59 +00:00
expiredAt = expiredAt.Add(time.Hour * 24)
2024-03-18 17:03:14 +00:00
case genv1.ExpireLevel_Week:
2023-09-30 13:47:59 +00:00
expiredAt = expiredAt.Add(time.Hour * 168)
2024-03-18 17:03:14 +00:00
case genv1.ExpireLevel_Month:
2023-09-30 13:47:59 +00:00
expiredAt = expiredAt.Add(time.Hour * 720)
2024-03-18 17:03:14 +00:00
case genv1.ExpireLevel_Year:
2023-09-30 13:47:59 +00:00
expiredAt = expiredAt.Add(time.Hour * 8760)
}
2024-03-18 17:03:14 +00:00
var record = genv1.ModelPastebin{
2023-09-30 13:47:59 +00:00
CreatedAt: time.Now().Unix(),
ExpiredAt: expiredAt.Unix(),
ShortId: common.UniqID(time.Now().UnixMilli()),
Title: req.Record.Title,
Content: req.Record.Content,
Lang: req.Record.Lang,
Password: req.Record.Password,
NeedPassword: req.Record.NeedPassword,
Editable: req.Record.Editable,
Author: req.Record.Author,
}
db := repositories.GetPastebin()
if err := db.Insert().Insert(&record); err != nil {
logrus.Errorf("insert record failed: %v", err)
return nil, err
}
return resp, nil
}
// Update 更新
2024-03-18 17:03:14 +00:00
func (receiver *PastebinService) Update(ctx *coco.Context, req *genv1.PastebinServiceUpdateReq) (resp *genv1.PastebinServiceUpdateResp, err error) {
resp = new(genv1.PastebinServiceUpdateResp)
2023-09-30 13:47:59 +00:00
// TODO impl...
return resp, nil
}
// Get 获取一条记录
2024-03-18 17:03:14 +00:00
func (receiver *PastebinService) Get(ctx *coco.Context, req *genv1.PastebinServiceGetReq) (resp *genv1.PastebinServiceGetResp, err error) {
resp = new(genv1.PastebinServiceGetResp)
2023-09-30 13:47:59 +00:00
db := repositories.GetPastebin()
2024-03-18 17:03:14 +00:00
var record genv1.ModelPastebin
2023-09-30 13:47:59 +00:00
if err := db.Find().SetWhere("short_id = ?", req.Key).FindOne(&record); err != nil {
logrus.Errorf("query records failed: %v", err)
return nil, err
}
if record.NeedPassword && req.Password == "" {
2023-10-15 13:30:40 +00:00
return nil, coco.CreateErrorWithMsg(4001, "pastebin need password")
2023-09-30 13:47:59 +00:00
}
if record.NeedPassword && req.Password != record.Password {
2023-10-15 13:30:40 +00:00
return nil, coco.CreateErrorWithMsg(4002, "incorrect password")
2023-09-30 13:47:59 +00:00
}
2024-03-18 17:03:14 +00:00
item := &genv1.PastebinServiceRecord{
2023-09-30 13:47:59 +00:00
Id: record.Id,
Key: record.ShortId,
Title: record.Title,
Content: record.Content,
CreatedAt: common.Unix2Datetime(record.CreatedAt),
ExpiredAt: common.Unix2Datetime(record.ExpiredAt),
NeedPassword: record.NeedPassword,
Lang: record.Lang,
Author: record.Author,
}
resp.Record = item
return resp, nil
}
2023-10-15 13:30:40 +00:00
// Raw 获取原文内容
2024-03-18 17:03:14 +00:00
func (receiver *PastebinService) Raw(ctx *coco.Context, req *genv1.PastebinServiceRawReq) (resp *genv1.PastebinServiceRawResp, err error) {
resp = new(genv1.PastebinServiceRawResp)
2023-10-15 13:30:40 +00:00
db := repositories.GetPastebin()
2024-03-18 17:03:14 +00:00
var record genv1.ModelPastebin
2023-10-15 13:30:40 +00:00
if err := db.Find().SetWhere("short_id = ?", req.Key).FindOne(&record); err != nil {
logrus.Errorf("query records failed: %v", err)
return nil, err
}
if record.NeedPassword && req.Password == "" {
return nil, coco.CreateErrorWithMsg(4001, "pastebin need password")
}
if record.NeedPassword && req.Password != record.Password {
return nil, coco.CreateErrorWithMsg(4002, "incorrect password")
}
_, _ = ctx.Writer.WriteString(record.Content)
ctx.Header("Content-Type", "text/plain")
return resp, nil
}