Add password protected links

This commit is contained in:
Andrey Chervyakov 2021-04-03 22:25:11 +06:00
parent 2baa74d520
commit a01f540a29
6 changed files with 67 additions and 28 deletions

View file

@ -34,12 +34,12 @@ func (r *PgRepository) Save(link *Link) error {
defer cancel()
sql := `
INSERT INTO links (id, name, redirect_url, creation_time)
VALUES ($1, $2, $3, $4::timestamp)
INSERT INTO links (id, name, redirect_url, password, creation_time)
VALUES ($1, $2, $3, $4, $5::timestamp)
`
database.LogPoolState(r.pool, "Saving link")
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.Password, link.CreationTime.Format("2006-01-02 15:04:05"))
if err != nil {
return err
}
@ -52,7 +52,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
defer cancel()
sql := `
SELECT id, name, redirect_url, creation_time
SELECT id, name, redirect_url, password, creation_time
FROM links
WHERE id = $1
`
@ -82,7 +82,7 @@ func (r *PgRepository) GetAll(limit int, offset int) (Links, error) {
defer cancel()
sql := `
SELECT id, name, redirect_url, creation_time
SELECT id, name, redirect_url, password, creation_time
FROM links
LIMIT $1
OFFSET $2
@ -122,12 +122,12 @@ func (r *PgRepository) Update(link *Link) error {
sql := `
UPDATE links
SET name = $1, redirect_url = $2
WHERE id = $3
SET name = $1, redirect_url = $2, password = $3
WHERE id = $4
`
database.LogPoolState(r.pool, "Updating link")
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Password, link.Id)
if err != nil {
return err
}
@ -159,11 +159,11 @@ func mapRowToEntity(r interface{}) (*Link, error) {
switch v := r.(type) {
case pgx.Row:
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &t); err != nil {
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
return nil, err
}
case pgx.Rows:
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &t); err != nil {
if err := v.Scan(&entity.Id, &entity.Name, &urlStr, &entity.Password, &t); err != nil {
return nil, err
}
default: