Compare commits

...

3 Commits

6 changed files with 18 additions and 11 deletions

View File

@ -32,5 +32,5 @@ service fivefeeteleven-api {
get /:id(ExpandReq) returns(ExpandResp)
@handler ShortenUrl
post /redirect(ShortenReq) returns(ShortenResp)
post /api/v1/links(ShortenReq) returns(ShortenResp)
}

View File

@ -7,8 +7,8 @@ A URL shortener that generates links even shorter than 5'11.
# Shorten a URL
curl --header "Content-Type: application/json" \
--request POST \
--data '{"redirectUrl":"https://news.ycombinator.com"}' \
http://localhost:5111/redirect
--data '{"longUrl":"https://news.ycombinator.com"}' \
http://localhost:5111/api/v1/links
# Expand the URL
curl -iL http://localhost:5111/{id}

View File

@ -1,15 +1,20 @@
package db
import "github.com/scylladb/gocqlx/v2/table"
import (
"time"
"github.com/scylladb/gocqlx/v2/table"
)
var UrlTable = table.New(table.Metadata{
Name: "fivefeeteleven.urls",
Columns: []string{"id", "long_url"},
Columns: []string{"id", "long_url", "created_at"},
PartKey: []string{"id"},
SortKey: []string{},
})
type UrlModel struct {
ID string
LongUrl string
ID string
LongUrl string
CreatedAt time.Time
}

View File

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

View File

@ -19,7 +19,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/redirect",
Path: "/api/v1/links",
Handler: ShortenUrlHandler(serverCtx),
},
},

View File

@ -36,8 +36,9 @@ func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.Shorten
insertUrl := insertBuilder.Query(l.svcCtx.DB)
insertUrl.BindStruct(db.UrlModel{
ID: id,
LongUrl: req.LongUrl,
ID: id,
LongUrl: req.LongUrl,
CreatedAt: time.Now(),
})
if err := insertUrl.ExecRelease(); err != nil {