From 9da60fa4e743f34ab732b721505b78d044a60962 Mon Sep 17 00:00:00 2001 From: strNophix Date: Sun, 28 Aug 2022 15:01:02 +0200 Subject: [PATCH] Added link info endpoint --- 5feet11.api | 12 ++++++++ internal/db/models.go | 3 +- internal/db/seed.go | 3 +- internal/handler/getlinkhandler.go | 28 +++++++++++++++++ internal/handler/routes.go | 5 +++ internal/logic/getlinklogic.go | 49 ++++++++++++++++++++++++++++++ internal/logic/shortenurllogic.go | 2 ++ internal/types/types.go | 7 +++++ 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 internal/handler/getlinkhandler.go create mode 100644 internal/logic/getlinklogic.go diff --git a/5feet11.api b/5feet11.api index fe8fb3b..4c27b04 100644 --- a/5feet11.api +++ b/5feet11.api @@ -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) } \ No newline at end of file diff --git a/internal/db/models.go b/internal/db/models.go index c96f8eb..062e1c4 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -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 } diff --git a/internal/db/seed.go b/internal/db/seed.go index 0136603..6b2bab9 100644 --- a/internal/db/seed.go +++ b/internal/db/seed.go @@ -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 { diff --git a/internal/handler/getlinkhandler.go b/internal/handler/getlinkhandler.go new file mode 100644 index 0000000..cd78045 --- /dev/null +++ b/internal/handler/getlinkhandler.go @@ -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) + } + } +} diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 33d8c13..3d6f230 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -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), + }, }, ) } diff --git a/internal/logic/getlinklogic.go b/internal/logic/getlinklogic.go new file mode 100644 index 0000000..830a70e --- /dev/null +++ b/internal/logic/getlinklogic.go @@ -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 +} diff --git a/internal/logic/shortenurllogic.go b/internal/logic/shortenurllogic.go index 2d144cd..e355fe7 100644 --- a/internal/logic/shortenurllogic.go +++ b/internal/logic/shortenurllogic.go @@ -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 } diff --git a/internal/types/types.go b/internal/types/types.go index 675c87d..c318402 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -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"` +}