Initial commit

This commit is contained in:
2022-08-26 21:10:50 +02:00
parent f5b7072065
commit 2bf97a0194
21 changed files with 1339 additions and 2 deletions

View File

@@ -0,0 +1,46 @@
package logic
import (
"context"
"fmt"
"5feet11/internal/db"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ExpandUrlLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewExpandUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandUrlLogic {
return &ExpandUrlLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ExpandUrlLogic) ExpandUrl(req *types.ExpandReq) (resp *types.ExpandResp, err error) {
queryUrl := db.UrlTable.SelectBuilder("redirect_url").Query(l.svcCtx.DB)
queryUrl.BindStruct(db.UrlModel{Id: req.Snowflake})
var urls []db.UrlModel
if err := queryUrl.Select(&urls); err != nil {
return nil, err
}
if len(urls) != 1 {
return nil, fmt.Errorf("no URL found")
}
resp = &types.ExpandResp{
RedirectUrl: urls[0].RedirectUrl,
}
return resp, err
}

View File

@@ -0,0 +1,46 @@
package logic
import (
"context"
"time"
"5feet11/internal/db"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ShortenUrlLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewShortenUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenUrlLogic {
return &ShortenUrlLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.ShortenResp, err error) {
id := l.svcCtx.Snowflake.Generate().Base58()
insertUrl := db.UrlTable.InsertBuilder().TTL(30 * time.Second).Query(l.svcCtx.DB)
insertUrl.BindStruct(db.UrlModel{
Id: id,
RedirectUrl: req.RedirectUrl,
Secret: &req.Secret,
})
if err := insertUrl.ExecRelease(); err != nil {
return nil, err
}
resp = &types.ShortenResp{
Id: id,
}
return resp, nil
}