From 131cc2640a530946df4c4a97973bcbe11fd17004 Mon Sep 17 00:00:00 2001 From: strNophix Date: Sat, 27 Aug 2022 17:02:15 +0200 Subject: [PATCH] Obey supplied expiration time `ShortenReq.ExpiresAfter` --- 5feet11.api | 4 ++-- internal/logic/shortenurllogic.go | 8 +++++++- internal/types/types.go | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/5feet11.api b/5feet11.api index 5570e20..8aaaf4e 100644 --- a/5feet11.api +++ b/5feet11.api @@ -18,8 +18,8 @@ type ( type ( ShortenReq { - LongUrl string `json:"longUrl"` - ExpiresIn int64 `json:"expiresIn,optional"` + LongUrl string `json:"longUrl"` + ExpiresAfter int64 `json:"expiresAfter,optional"` } ShortenResp { diff --git a/internal/logic/shortenurllogic.go b/internal/logic/shortenurllogic.go index 5fe1ee4..ab9b549 100644 --- a/internal/logic/shortenurllogic.go +++ b/internal/logic/shortenurllogic.go @@ -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) { 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{ ID: id, LongUrl: req.LongUrl, diff --git a/internal/types/types.go b/internal/types/types.go index 3b9ceda..675c87d 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -10,8 +10,8 @@ type ExpandResp struct { } type ShortenReq struct { - LongUrl string `json:"longUrl"` - ExpiresIn int64 `json:"expiresIn,optional"` + LongUrl string `json:"longUrl"` + ExpiresAfter int64 `json:"expiresAfter,optional"` } type ShortenResp struct {