Skip to content

Commit

Permalink
🛠️ Rename: changed UUID by ID. Now the property UUID is used as the t…
Browse files Browse the repository at this point in the history
…able's PK
  • Loading branch information
lhbelfanti committed Dec 13, 2024
1 parent 93fc0db commit 1068522
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/tweets/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/tweets/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/tweets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/tweets/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions cmd/api/tweets/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/tweets/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func MockTweetDTO() TweetDTO {
searchCriteriaID := 1

return TweetDTO{
UUID: "1234567890987654321",
ID: "1234567890987654321",
IsAReply: true,
Author: "TestAuthor",
Avatar: &avatar,
Expand Down
10 changes: 5 additions & 5 deletions migrations/005_create_tweets_table.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
);
Expand All @@ -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';
Expand Down

0 comments on commit 1068522

Please sign in to comment.