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

Commit

Permalink
Add UpdateTwitterTags to adapter (#850)
Browse files Browse the repository at this point in the history
* Add UpdateTwitterTags to adapter

* Make small change

* Update backend/app/adapter/sqldb/short_link.go

Co-authored-by: Harry Liu <[email protected]>

* Reorder UpdateOpenGraphTags and UpdateTwitterTags

* Update tests

* Format

Co-authored-by: Harry Liu <[email protected]>
  • Loading branch information
kapillamba4 and coderworld10 authored Jun 15, 2020
1 parent af000cf commit ad42a52
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
27 changes: 27 additions & 0 deletions backend/app/adapter/sqldb/short_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ WHERE "%s"=$4;`,
return s.GetShortLinkByAlias(alias)
}

// UpdateTwitterTags updates Twitter meta tags for a given short link.
func (s *ShortLinkSql) UpdateTwitterTags(alias string, twitterTags metatag.Twitter) (entity.ShortLink, error) {
statement := fmt.Sprintf(`
UPDATE "%s"
SET "%s"=$1, "%s"=$2, "%s"=$3
WHERE "%s"=$4;`,
table.ShortLink.TableName,
table.ShortLink.ColumnTwitterTitle,
table.ShortLink.ColumnTwitterDescription,
table.ShortLink.ColumnTwitterImageURL,
table.ShortLink.ColumnAlias,
)

_, err := s.db.Exec(
statement,
twitterTags.Title,
twitterTags.Description,
twitterTags.ImageURL,
alias,
)
if err != nil {
return entity.ShortLink{}, err
}

return s.GetShortLinkByAlias(alias)
}

// IsAliasExist checks whether a given alias exist in short_link table.
func (s ShortLinkSql) IsAliasExist(alias string) (bool, error) {
query := fmt.Sprintf(`
Expand Down
104 changes: 104 additions & 0 deletions backend/app/adapter/sqldb/short_link_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,110 @@ func TestShortLinkSql_UpdateOGMetaTags(t *testing.T) {
}
}

func TestShortLinkSql_UpdateTwitterTags(t *testing.T) {
title1 := "title1"
description1 := "description1"
imageURL1 := "url1"
title2 := "title2"
description2 := "description2"
imageURL2 := "url2"

testCases := []struct {
name string
tableRows []shortLinkTableRow
alias string
metaTags metatag.Twitter
expectedShortLink entity.ShortLink
}{
{
name: "Twitter tags not provided",
tableRows: []shortLinkTableRow{
{
alias: "220uFicCJj",
longLink: "http://www.google.com",
ogTitle: &title1,
ogDescription: &description1,
ogImageURL: &imageURL1,
},
},
alias: "220uFicCJj",
metaTags: metatag.Twitter{
Title: &title2,
Description: &description2,
ImageURL: &imageURL2,
},
expectedShortLink: entity.ShortLink{
Alias: "220uFicCJj",
LongLink: "http://www.google.com",
OpenGraphTags: metatag.OpenGraph{
Title: &title1,
Description: &description1,
ImageURL: &imageURL1,
},
TwitterTags: metatag.Twitter{
Title: &title2,
Description: &description2,
ImageURL: &imageURL2,
},
},
},
{
name: "Twitter tags provided",
tableRows: []shortLinkTableRow{
{
alias: "220uFicCJj",
longLink: "http://www.google.com",
ogTitle: &title1,
ogDescription: &description1,
ogImageURL: &imageURL1,
twitterTitle: &title1,
twitterDescription: &description1,
twitterImageURL: &imageURL1,
},
},
alias: "220uFicCJj",
metaTags: metatag.Twitter{
Title: &title2,
Description: &description2,
ImageURL: &imageURL2,
},
expectedShortLink: entity.ShortLink{
Alias: "220uFicCJj",
LongLink: "http://www.google.com",
OpenGraphTags: metatag.OpenGraph{
Title: &title1,
Description: &description1,
ImageURL: &imageURL1,
},
TwitterTags: metatag.Twitter{
Title: &title2,
Description: &description2,
ImageURL: &imageURL2,
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
dbtest.AccessTestDB(
dbConnector,
dbMigrationTool,
dbMigrationRoot,
dbConfig,
func(sqlDB *sql.DB) {
insertShortLinkTableRows(t, sqlDB, testCase.tableRows)

shortLinkRepo := sqldb.NewShortLinkSql(sqlDB)

shortLink, err := shortLinkRepo.UpdateTwitterTags(testCase.alias, testCase.metaTags)
assert.Equal(t, nil, err)
assert.Equal(t, testCase.expectedShortLink, shortLink)
})
})
}
}

func TestShortLinkSql_IsAliasExist(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit ad42a52

Please sign in to comment.