Added link info endpoint

This commit is contained in:
strNophix 2022-08-28 15:01:02 +02:00
parent 0944a14a97
commit 9da60fa4e7
8 changed files with 107 additions and 2 deletions

View File

@ -27,10 +27,22 @@ type (
}
)
type (
GetLinkResp {
ID string `json:"id"`
LongUrl string `json:"longUrl"`
CreatedAt string `json:"createdAt"`
Lifespan int64 `json:"lifespan"`
}
)
service fivefeeteleven-api {
@handler ExpandUrl
get /:id(ExpandReq) returns(ExpandResp)
@handler ShortenUrl
post /api/v1/links(ShortenReq) returns(ShortenResp)
@handler GetLink
get /api/v1/links/:id(ExpandReq) returns(GetLinkResp)
}

View File

@ -8,7 +8,7 @@ import (
var UrlTable = table.New(table.Metadata{
Name: "fivefeeteleven.urls",
Columns: []string{"id", "long_url", "created_at"},
Columns: []string{"id", "long_url", "lifespan", "created_at"},
PartKey: []string{"id"},
SortKey: []string{},
})
@ -16,5 +16,6 @@ var UrlTable = table.New(table.Metadata{
type UrlModel struct {
ID string
LongUrl string
Lifespan int64
CreatedAt time.Time
}

View File

@ -16,7 +16,8 @@ func Seed(session gocqlx.Session) error {
CREATE TABLE IF NOT EXISTS fivefeeteleven.urls (
id text PRIMARY KEY,
long_url text,
created_at timestamp
created_at timestamp,
lifespan bigint
)`)
if err != nil {

View File

@ -0,0 +1,28 @@
package handler
import (
"net/http"
"5feet11/internal/logic"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
func GetLinkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ExpandReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewGetLinkLogic(r.Context(), svcCtx)
resp, err := l.GetLink(&req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}

View File

@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/v1/links",
Handler: ShortenUrlHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/v1/links/:id",
Handler: GetLinkHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,49 @@
package logic
import (
"context"
"fmt"
"5feet11/internal/db"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLinkLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetLinkLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLinkLogic {
return &GetLinkLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetLinkLogic) GetLink(req *types.ExpandReq) (resp *types.GetLinkResp, err error) {
queryUrl := db.UrlTable.SelectQuery(l.svcCtx.DB)
queryUrl.BindStruct(db.UrlModel{ID: req.ID})
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.GetLinkResp{
ID: urls[0].ID,
LongUrl: urls[0].LongUrl,
CreatedAt: urls[0].CreatedAt.String(),
Lifespan: urls[0].Lifespan,
}
return resp, err
}

View File

@ -38,6 +38,7 @@ func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.Shorten
insertUrl.BindStruct(db.UrlModel{
ID: id,
LongUrl: req.LongUrl,
Lifespan: req.ExpiresAfter,
CreatedAt: time.Now(),
})
@ -48,5 +49,6 @@ func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.Shorten
resp = &types.ShortenResp{
ID: id,
}
return resp, nil
}

View File

@ -17,3 +17,10 @@ type ShortenReq struct {
type ShortenResp struct {
ID string `json:"id"`
}
type GetLinkResp struct {
ID string `json:"id"`
LongUrl string `json:"longUrl"`
CreatedAt string `json:"createdAt"`
Lifespan int64 `json:"lifespan"`
}