package controllerv1 import ( "time" "github.com/sirupsen/logrus" "gitter.top/coco/coco" "pastebin/common" genv1 "pastebin/gen/v1" "pastebin/internal/repositories" ) type PastebinService struct{} // IDE: PastebinService implemented genv1.AutoGenPastebinServiceImpl interface var _ genv1.AutoGenPastebinServiceImpl = (*PastebinService)(nil) // List 列表 func (receiver *PastebinService) List(ctx *coco.Context, req *genv1.PastebinServiceListReq) (resp *genv1.PastebinServiceListResp, err error) { resp = new(genv1.PastebinServiceListResp) 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() var records []genv1.ModelPastebin 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 { item := &genv1.PastebinServiceRecord{ 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 新建 func (receiver *PastebinService) Add(ctx *coco.Context, req *genv1.PastebinServiceAddReq) (resp *genv1.PastebinServiceAddResp, err error) { resp = new(genv1.PastebinServiceAddResp) var expiredAt = time.Now() switch req.Record.ExpireLevel { case genv1.ExpireLevel_Forever: expiredAt = expiredAt.Add(time.Hour * 87600) case genv1.ExpireLevel_Day: expiredAt = expiredAt.Add(time.Hour * 24) case genv1.ExpireLevel_Week: expiredAt = expiredAt.Add(time.Hour * 168) case genv1.ExpireLevel_Month: expiredAt = expiredAt.Add(time.Hour * 720) case genv1.ExpireLevel_Year: expiredAt = expiredAt.Add(time.Hour * 8760) } var record = genv1.ModelPastebin{ 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 更新 func (receiver *PastebinService) Update(ctx *coco.Context, req *genv1.PastebinServiceUpdateReq) (resp *genv1.PastebinServiceUpdateResp, err error) { resp = new(genv1.PastebinServiceUpdateResp) // TODO impl... return resp, nil } // Get 获取一条记录 func (receiver *PastebinService) Get(ctx *coco.Context, req *genv1.PastebinServiceGetReq) (resp *genv1.PastebinServiceGetResp, err error) { resp = new(genv1.PastebinServiceGetResp) db := repositories.GetPastebin() var record genv1.ModelPastebin 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") } item := &genv1.PastebinServiceRecord{ 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 } // Raw 获取原文内容 func (receiver *PastebinService) Raw(ctx *coco.Context, req *genv1.PastebinServiceRawReq) (resp *genv1.PastebinServiceRawResp, err error) { resp = new(genv1.PastebinServiceRawResp) db := repositories.GetPastebin() var record genv1.ModelPastebin 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 }