From 106852242a13baa42b9e399dca525522753fbcbc Mon Sep 17 00:00:00 2001 From: Lucas Belfanti Date: Fri, 13 Dec 2024 01:48:12 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Rename:=20changed=20UUI?= =?UTF-8?q?D=20by=20ID.=20Now=20the=20property=20UUID=20is=20used=20as=20t?= =?UTF-8?q?he=20table's=20PK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- cmd/api/tweets/dtos.go | 2 +- cmd/api/tweets/errors.go | 2 +- cmd/api/tweets/handler.go | 4 ++-- cmd/api/tweets/handler_test.go | 4 ++-- cmd/api/tweets/insert.go | 6 +++--- cmd/api/tweets/mocks.go | 2 +- migrations/005_create_tweets_table.sql | 10 +++++----- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3328bfa..3b6e1dd 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ erDiagram tweets ||--o| tweets_quotes : "" tweets }|--|{ search_criteria : "" tweets { - INTEGER id PK - TEXT uuid + INTEGER uuid PK + TEXT id TEXT author TEXT avatar TIMESTAMP posted_at diff --git a/cmd/api/tweets/dtos.go b/cmd/api/tweets/dtos.go index 28f71ee..aaf34ca 100644 --- a/cmd/api/tweets/dtos.go +++ b/cmd/api/tweets/dtos.go @@ -4,7 +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"` + ID string `json:"id"` Author string `json:"author"` Avatar *string `json:"avatar,omitempty"` PostedAt string `json:"posted_at"` diff --git a/cmd/api/tweets/errors.go b/cmd/api/tweets/errors.go index d116868..41ebdc4 100644 --- a/cmd/api/tweets/errors.go +++ b/cmd/api/tweets/errors.go @@ -4,7 +4,7 @@ import "errors" var ( FailedToInsertTweets = errors.New("failed to insert tweets") - MissingTweetUUID = errors.New("missing tweet UUID") + MissingTweetID = errors.New("missing tweet ID") MissingTweetSearchCriteriaID = errors.New("missing tweet search criteria ID") ) diff --git a/cmd/api/tweets/handler.go b/cmd/api/tweets/handler.go index 11df1d9..a914d55 100644 --- a/cmd/api/tweets/handler.go +++ b/cmd/api/tweets/handler.go @@ -42,8 +42,8 @@ func InsertHandlerV1(insertTweets Insert) http.HandlerFunc { // validateBody validates that mandatory fields are present func validateBody(body []TweetDTO) error { for _, tweet := range body { - if tweet.UUID == "" { - return MissingTweetUUID + if tweet.ID == "" { + return MissingTweetID } if tweet.SearchCriteriaID == nil { diff --git a/cmd/api/tweets/handler_test.go b/cmd/api/tweets/handler_test.go index b476991..bb63335 100644 --- a/cmd/api/tweets/handler_test.go +++ b/cmd/api/tweets/handler_test.go @@ -47,11 +47,11 @@ func TestInsertHandlerV1_failsWhenTheBodyCantBeParsed(t *testing.T) { assert.Equal(t, want, got) } -func TestInsertHandlerV1_failsWhenTweetUUIDIsNotPresentInBody(t *testing.T) { +func TestInsertHandlerV1_failsWhenTweetIDIsNotPresentInBody(t *testing.T) { mockInsert := tweets.MockInsert(nil) mockResponseWriter := httptest.NewRecorder() mockTweets := tweets.MockTweetsDTOs() - mockTweets[0].UUID = "" + mockTweets[0].ID = "" mockBody, _ := json.Marshal(mockTweets) mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/tweets/v1", bytes.NewReader(mockBody)) diff --git a/cmd/api/tweets/insert.go b/cmd/api/tweets/insert.go index 9cdd506..77aaa5c 100644 --- a/cmd/api/tweets/insert.go +++ b/cmd/api/tweets/insert.go @@ -18,9 +18,9 @@ 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(uuid, author, avatar, posted_at, is_a_reply, text_content, images, quote_id, search_criteria_id) + INSERT INTO tweets(id, author, avatar, posted_at, is_a_reply, text_content, images, quote_id, search_criteria_id) VALUES %s - ON CONFLICT (uuid, search_criteria_id) DO NOTHING; + ON CONFLICT (id, search_criteria_id) DO NOTHING; ` parameters = 9 ) @@ -32,7 +32,7 @@ func MakeInsert(db database.Connection, insertQuote quotes.InsertSingle, deleteO 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.UUID, tweet.Author, tweet.Avatar) + values = append(values, tweet.ID, tweet.Author, tweet.Avatar) var postedAt *time.Time if tweet.PostedAt != "" { diff --git a/cmd/api/tweets/mocks.go b/cmd/api/tweets/mocks.go index 25a1ee6..0324ae4 100644 --- a/cmd/api/tweets/mocks.go +++ b/cmd/api/tweets/mocks.go @@ -22,7 +22,7 @@ func MockTweetDTO() TweetDTO { searchCriteriaID := 1 return TweetDTO{ - UUID: "1234567890987654321", + ID: "1234567890987654321", IsAReply: true, Author: "TestAuthor", Avatar: &avatar, diff --git a/migrations/005_create_tweets_table.sql b/migrations/005_create_tweets_table.sql index f01e22b..386cb0b 100644 --- a/migrations/005_create_tweets_table.sql +++ b/migrations/005_create_tweets_table.sql @@ -1,7 +1,7 @@ -- Create the tweets table CREATE TABLE IF NOT EXISTS tweets ( - id SERIAL PRIMARY KEY, - uuid TEXT NOT NULL, + uuid SERIAL PRIMARY KEY, + id TEXT NOT NULL, author TEXT NOT NULL, avatar TEXT, posted_at TIMESTAMP WITH TIME ZONE, @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS tweets ( quote_id INTEGER NULL, search_criteria_id INTEGER NOT NULL, - CONSTRAINT uq_uuid_search_criteria UNIQUE (uuid, search_criteria_id), + CONSTRAINT uq_id_search_criteria UNIQUE (id, search_criteria_id), CONSTRAINT fk_quote_id FOREIGN KEY(quote_id) REFERENCES tweets_quotes(id), CONSTRAINT fk_search_criteria_id FOREIGN KEY(search_criteria_id) REFERENCES search_criteria(id) ); @@ -22,8 +22,8 @@ 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 identifier for the tweet. It is part of the primary key'; +COMMENT ON COLUMN tweets.uuid IS 'Auto-incrementing UUID of the tweet, agnostic to business logic'; +COMMENT ON COLUMN tweets.id IS 'ID of 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'; COMMENT ON COLUMN tweets.posted_at IS 'Timestamp indicating when the tweet was posted';