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

@ -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 {

@ -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,

@ -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 {