Added CreatedAt field to URL model

This commit is contained in:
strNophix 2022-08-27 17:22:44 +02:00
parent 131cc2640a
commit 4539c926ec
3 changed files with 14 additions and 7 deletions

View File

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

View File

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

View File

@ -38,6 +38,7 @@ func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.Shorten
insertUrl.BindStruct(db.UrlModel{ insertUrl.BindStruct(db.UrlModel{
ID: id, ID: id,
LongUrl: req.LongUrl, LongUrl: req.LongUrl,
CreatedAt: time.Now(),
}) })
if err := insertUrl.ExecRelease(); err != nil { if err := insertUrl.ExecRelease(); err != nil {