package api_servicesv1 import ( "github.com/sirupsen/logrus" "gitter.top/coco/coco/core" "pastebin/common" "pastebin/internal/repositories" modelv1 "pastebin/model/v1" "time" ) type PastebinService struct{} // IDE: PastebinService implemented PastebinServiceImpl interface var _ AutoGenPastebinServiceImpl = (*PastebinService)(nil) // List 列表 func (receiver *PastebinService) List(ctx *core.Context, req *PastebinServiceListReq) (resp *PastebinServiceListResp, err error) { resp = new(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 []modelv1.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 := &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 *core.Context, req *PastebinServiceAddReq) (resp *PastebinServiceAddResp, err error) { resp = new(PastebinServiceAddResp) var expiredAt = time.Now() switch req.Record.ExpireLevel { case ExpireLevel_Forever: expiredAt = expiredAt.Add(time.Hour * 87600) case ExpireLevel_Day: expiredAt = expiredAt.Add(time.Hour * 24) case ExpireLevel_Week: expiredAt = expiredAt.Add(time.Hour * 168) case ExpireLevel_Month: expiredAt = expiredAt.Add(time.Hour * 720) case ExpireLevel_Year: expiredAt = expiredAt.Add(time.Hour * 8760) } var record = modelv1.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 *core.Context, req *PastebinServiceUpdateReq) (resp *PastebinServiceUpdateResp, err error) { resp = new(PastebinServiceUpdateResp) // TODO impl... return resp, nil } // Get 获取一条记录 func (receiver *PastebinService) Get(ctx *core.Context, req *PastebinServiceGetReq) (resp *PastebinServiceGetResp, err error) { resp = new(PastebinServiceGetResp) db := repositories.GetPastebin() var record modelv1.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, core.CreateErrorWithMsg(4001, "pastebin need password") } if record.NeedPassword && req.Password != record.Password { return nil, core.CreateErrorWithMsg(4002, "incorrect password") } item := &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 }