Skip to content

Commit

Permalink
⚡️ Added UUID to the tweets table
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Dec 5, 2024
1 parent 0d843ad commit 240753c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea

.env
.env

.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ erDiagram
tweets }|--|{ search_criteria : ""
tweets {
INTEGER id PK
TEXT uuid
TEXT hash
TEXT author
TEXT avatar
Expand Down
1 change: 1 addition & 0 deletions cmd/api/tweets/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "ahbcc/cmd/api/tweets/quotes"

// TweetDTO represents a tweet to be inserted into the 'tweets' table
type TweetDTO struct {
UUID string `json:"uuid"`
Hash *string `json:"hash,omitempty"`
Author string `json:"author"`
Avatar *string `json:"avatar,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/tweets/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type Insert func(ctx context.Context, tweet []TweetDTO) error
func MakeInsert(db database.Connection, insertQuote quotes.InsertSingle, deleteOrphanQuotes quotes.DeleteOrphans) Insert {
const (
query string = `
INSERT INTO tweets(hash, author, avatar, posted_at, is_a_reply, text_content, images, quote_id, search_criteria_id)
INSERT INTO tweets(uuid, hash, author, avatar, posted_at, is_a_reply, text_content, images, quote_id, search_criteria_id)
VALUES %s
ON CONFLICT (hash, search_criteria_id) DO NOTHING;
`
parameters = 9
parameters = 10
)

return func(ctx context.Context, tweets []TweetDTO) error {
Expand All @@ -31,8 +31,8 @@ func MakeInsert(db database.Connection, insertQuote quotes.InsertSingle, deleteO
quoteIDs := make([]int, 0, len(tweets))
for i, tweet := range tweets {
idx := i * parameters
placeholders = append(placeholders, fmt.Sprintf("($%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d)", idx+1, idx+2, idx+3, idx+4, idx+5, idx+6, idx+7, idx+8, idx+9))
values = append(values, tweet.Hash, tweet.Author, tweet.Avatar)
placeholders = append(placeholders, fmt.Sprintf("($%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d)", idx+1, idx+2, idx+3, idx+4, idx+5, idx+6, idx+7, idx+8, idx+9, idx+10))
values = append(values, tweet.UUID, tweet.Hash, tweet.Author, tweet.Avatar)

var postedAt *time.Time
if tweet.PostedAt != "" {
Expand Down
1 change: 1 addition & 0 deletions cmd/api/tweets/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func MockTweetDTO() TweetDTO {
searchCriteriaID := 1

return TweetDTO{
UUID: "1234567890987654321",
Hash: &hash,
IsAReply: true,
Author: "TestAuthor",
Expand Down
2 changes: 2 additions & 0 deletions migrations/005_create_tweets_table.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Create the tweets table
CREATE TABLE IF NOT EXISTS tweets (
id SERIAL PRIMARY KEY,
uuid TEXT NOT NULL,
hash TEXT NOT NULL,
author TEXT NOT NULL,
avatar TEXT,
Expand All @@ -23,6 +24,7 @@ SELECT create_index_if_not_exists('idx_tweets_search_criteria', 'tweets', 'searc
-- Table comments
COMMENT ON TABLE tweets IS 'Contains the tweets scrapped by GoXCrap';
COMMENT ON COLUMN tweets.id IS 'Auto-incrementing ID of the tweet, agnostic to business logic';
COMMENT ON COLUMN tweets.uuid IS 'UUID of the tweet';
COMMENT ON COLUMN tweets.hash IS 'Unique hash identifier for the tweet. It is part of the primary key';
COMMENT ON COLUMN tweets.author IS 'The user that wrote the tweet';
COMMENT ON COLUMN tweets.avatar IS 'The user profile image';
Expand Down

0 comments on commit 240753c

Please sign in to comment.