Obey supplied expiration time ShortenReq.ExpiresAfter

This commit is contained in:
strNophix 2022-08-27 17:02:15 +02:00
parent 0e8dccb9d0
commit 131cc2640a
3 changed files with 11 additions and 5 deletions

View File

@ -19,7 +19,7 @@ type (
type ( type (
ShortenReq { ShortenReq {
LongUrl string `json:"longUrl"` LongUrl string `json:"longUrl"`
ExpiresIn int64 `json:"expiresIn,optional"` ExpiresAfter int64 `json:"expiresAfter,optional"`
} }
ShortenResp { ShortenResp {

View File

@ -27,8 +27,14 @@ func NewShortenUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Shorte
func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.ShortenResp, err error) { func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.ShortenResp, err error) {
id := l.svcCtx.Snowflake.Generate().Base58() id := l.svcCtx.Snowflake.Generate().Base58()
insertBuilder := db.UrlTable.InsertBuilder()
insertUrl := db.UrlTable.InsertBuilder().TTL(30 * time.Second).Query(l.svcCtx.DB) logx.Info(req.ExpiresAfter)
if req.ExpiresAfter != 0 {
insertBuilder.TTL(time.Second * time.Duration(req.ExpiresAfter))
}
insertUrl := insertBuilder.Query(l.svcCtx.DB)
insertUrl.BindStruct(db.UrlModel{ insertUrl.BindStruct(db.UrlModel{
ID: id, ID: id,
LongUrl: req.LongUrl, LongUrl: req.LongUrl,

View File

@ -11,7 +11,7 @@ type ExpandResp struct {
type ShortenReq struct { type ShortenReq struct {
LongUrl string `json:"longUrl"` LongUrl string `json:"longUrl"`
ExpiresIn int64 `json:"expiresIn,optional"` ExpiresAfter int64 `json:"expiresAfter,optional"`
} }
type ShortenResp struct { type ShortenResp struct {