Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Rename tables in backend (#802)
Browse files Browse the repository at this point in the history
* rename tables in backend

* table names

Co-authored-by: Harry Liu <[email protected]>
  • Loading branch information
arberiii and coderworld10 authored May 21, 2020
1 parent 97ecd08 commit e506d99
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
68 changes: 34 additions & 34 deletions backend/app/adapter/sqldb/short_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (

var _ repository.URL = (*URLSql)(nil)

// URLSql accesses URL information in url table through SQL.
// URLSql accesses ShortLink information in short_link table through SQL.
type URLSql struct {
db *sql.DB
}

// IsAliasExist checks whether a given alias exist in url table.
// IsAliasExist checks whether a given alias exist in short_link table.
func (u URLSql) IsAliasExist(alias string) (bool, error) {
query := fmt.Sprintf(`
SELECT "%s"
FROM "%s"
WHERE "%s"=$1;`,
table.URL.ColumnAlias,
table.URL.TableName,
table.URL.ColumnAlias,
table.ShortLink.ColumnAlias,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
)

err := u.db.QueryRow(query, alias).Scan(&alias)
Expand All @@ -38,18 +38,18 @@ WHERE "%s"=$1;`,
return true, nil
}

// Create inserts a new URL into url table.
// Create inserts a new ShortLink into short_link table.
// TODO(issue#698): change to CreateURL
func (u *URLSql) Create(url entity.URL) error {
statement := fmt.Sprintf(`
INSERT INTO "%s" ("%s","%s","%s","%s","%s")
VALUES ($1, $2, $3, $4, $5);`,
table.URL.TableName,
table.URL.ColumnAlias,
table.URL.ColumnOriginalURL,
table.URL.ColumnExpireAt,
table.URL.ColumnCreatedAt,
table.URL.ColumnUpdatedAt,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
table.ShortLink.ColumnLongLink,
table.ShortLink.ColumnExpireAt,
table.ShortLink.ColumnCreatedAt,
table.ShortLink.ColumnUpdatedAt,
)
_, err := u.db.Exec(
statement,
Expand All @@ -62,17 +62,17 @@ VALUES ($1, $2, $3, $4, $5);`,
return err
}

// UpdateURL updates a URL that exists within the url table.
// UpdateURL updates a ShortLink that exists within the short_link table.
func (u *URLSql) UpdateURL(oldAlias string, newURL entity.URL) (entity.URL, error) {
statement := fmt.Sprintf(`
UPDATE "%s"
SET "%s"=$1, "%s"=$2, "%s"=$3, "%s"=$4
WHERE "%s"=$5;`,
table.URL.TableName,
table.URL.ColumnAlias,
table.URL.ColumnOriginalURL,
table.URL.ColumnExpireAt,
table.URL.ColumnUpdatedAt,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
table.ShortLink.ColumnLongLink,
table.ShortLink.ColumnExpireAt,
table.ShortLink.ColumnUpdatedAt,
oldAlias,
)

Expand All @@ -92,19 +92,19 @@ WHERE "%s"=$5;`,
return newURL, nil
}

// GetByAlias finds an URL in url table given alias.
// GetByAlias finds an ShortLink in short_link table given alias.
func (u URLSql) GetByAlias(alias string) (entity.URL, error) {
statement := fmt.Sprintf(`
SELECT "%s","%s","%s","%s","%s"
FROM "%s"
WHERE "%s"=$1;`,
table.URL.ColumnAlias,
table.URL.ColumnOriginalURL,
table.URL.ColumnExpireAt,
table.URL.ColumnCreatedAt,
table.URL.ColumnUpdatedAt,
table.URL.TableName,
table.URL.ColumnAlias,
table.ShortLink.ColumnAlias,
table.ShortLink.ColumnLongLink,
table.ShortLink.ColumnExpireAt,
table.ShortLink.ColumnCreatedAt,
table.ShortLink.ColumnUpdatedAt,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
)

row := u.db.QueryRow(statement, alias)
Expand All @@ -128,7 +128,7 @@ WHERE "%s"=$1;`,
return url, nil
}

// GetByAliases finds URLs for a list of aliases
// GetByAliases finds ShortLinks for a list of aliases
func (u URLSql) GetByAliases(aliases []string) ([]entity.URL, error) {
if len(aliases) == 0 {
return []entity.URL{}, nil
Expand All @@ -149,13 +149,13 @@ func (u URLSql) GetByAliases(aliases []string) ([]entity.URL, error) {
SELECT "%s","%s","%s","%s","%s"
FROM "%s"
WHERE "%s" IN (%s);`,
table.URL.ColumnAlias,
table.URL.ColumnOriginalURL,
table.URL.ColumnExpireAt,
table.URL.ColumnCreatedAt,
table.URL.ColumnUpdatedAt,
table.URL.TableName,
table.URL.ColumnAlias,
table.ShortLink.ColumnAlias,
table.ShortLink.ColumnLongLink,
table.ShortLink.ColumnExpireAt,
table.ShortLink.ColumnCreatedAt,
table.ShortLink.ColumnUpdatedAt,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
parameterStr,
)

Expand Down
12 changes: 6 additions & 6 deletions backend/app/adapter/sqldb/short_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
var insertURLRowSQL = fmt.Sprintf(`
INSERT INTO %s (%s, %s, %s, %s, %s)
VALUES ($1, $2, $3, $4, $5)`,
table.URL.TableName,
table.URL.ColumnAlias,
table.URL.ColumnOriginalURL,
table.URL.ColumnCreatedAt,
table.URL.ColumnExpireAt,
table.URL.ColumnUpdatedAt,
table.ShortLink.TableName,
table.ShortLink.ColumnAlias,
table.ShortLink.ColumnLongLink,
table.ShortLink.ColumnCreatedAt,
table.ShortLink.ColumnExpireAt,
table.ShortLink.ColumnUpdatedAt,
)

type urlTableRow struct {
Expand Down
28 changes: 14 additions & 14 deletions backend/app/adapter/sqldb/table/short_link.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package table

// URL represents database table columns for 'url' table
var URL = struct {
TableName string
ColumnAlias string
ColumnOriginalURL string
ColumnCreatedAt string
ColumnExpireAt string
ColumnUpdatedAt string
// ShortLink represents database table columns for 'short_link' table
var ShortLink = struct {
TableName string
ColumnAlias string
ColumnLongLink string
ColumnCreatedAt string
ColumnExpireAt string
ColumnUpdatedAt string
}{
TableName: "short_link",
ColumnAlias: "alias",
ColumnOriginalURL: "long_link",
ColumnCreatedAt: "created_at",
ColumnExpireAt: "expire_at",
ColumnUpdatedAt: "updated_at",
TableName: "short_link",
ColumnAlias: "alias",
ColumnLongLink: "long_link",
ColumnCreatedAt: "created_at",
ColumnExpireAt: "expire_at",
ColumnUpdatedAt: "updated_at",
}
16 changes: 8 additions & 8 deletions backend/app/adapter/sqldb/table/user_short_link.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package table

// UserURLRelation represents database table columns for 'user_url_relation' table
var UserURLRelation = struct {
TableName string
ColumnUserID string
ColumnURLAlias string
// UserShortLink represents database table columns for 'user_short_link' table
var UserShortLink = struct {
TableName string
ColumnUserID string
ColumnShortLinkAlias string
}{
TableName: "user_short_link",
ColumnUserID: "user_id",
ColumnURLAlias: "short_link_alias",
TableName: "user_short_link",
ColumnUserID: "user_id",
ColumnShortLinkAlias: "short_link_alias",
}
18 changes: 9 additions & 9 deletions backend/app/adapter/sqldb/user_short_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ import (

var _ repository.UserURLRelation = (*UserURLRelationSQL)(nil)

// UserURLRelationSQL accesses UserURLRelation information in user_url_relation
// UserURLRelationSQL accesses UserShortLink information in user_short_link
// table.
type UserURLRelationSQL struct {
db *sql.DB
}

// CreateRelation establishes bi-directional relationship between a user and a
// url in user_url_relation table.
// short link in user_short_link table.
func (u UserURLRelationSQL) CreateRelation(user entity.User, url entity.URL) error {
statement := fmt.Sprintf(`
INSERT INTO "%s" ("%s","%s")
VALUES ($1,$2)
`,
table.UserURLRelation.TableName,
table.UserURLRelation.ColumnUserID,
table.UserURLRelation.ColumnURLAlias,
table.UserShortLink.TableName,
table.UserShortLink.ColumnUserID,
table.UserShortLink.ColumnShortLinkAlias,
)

_, err := u.db.Exec(statement, user.ID, url.Alias)
return err
}

// FindAliasesByUser fetches the aliases of all the URLs created by the given user.
// FindAliasesByUser fetches the aliases of all the ShortLinks created by the given user.
// TODO(issue#260): allow API client to filter urls based on visibility.
func (u UserURLRelationSQL) FindAliasesByUser(user entity.User) ([]string, error) {
statement := fmt.Sprintf(`SELECT "%s" FROM "%s" WHERE "%s"=$1;`,
table.UserURLRelation.ColumnURLAlias,
table.UserURLRelation.TableName,
table.UserURLRelation.ColumnUserID,
table.UserShortLink.ColumnShortLinkAlias,
table.UserShortLink.TableName,
table.UserShortLink.ColumnUserID,
)

var aliases []string
Expand Down
6 changes: 3 additions & 3 deletions backend/app/adapter/sqldb/user_short_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
var insertUserURLRelationRowSQL = fmt.Sprintf(`
INSERT INTO %s (%s, %s)
VALUES ($1, $2)`,
table.UserURLRelation.TableName,
table.UserURLRelation.ColumnURLAlias,
table.UserURLRelation.ColumnUserID,
table.UserShortLink.TableName,
table.UserShortLink.ColumnShortLinkAlias,
table.UserShortLink.ColumnUserID,
)

type userURLRelationTableRow struct {
Expand Down

0 comments on commit e506d99

Please sign in to comment.