Skip to content

Commit

Permalink
⚡️ Tweet Hash replaced by Tweet UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Dec 12, 2024
1 parent 240753c commit 93fc0db
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ erDiagram
tweets {
INTEGER id PK
TEXT uuid
TEXT hash
TEXT author
TEXT avatar
TIMESTAMP posted_at
Expand Down
1 change: 0 additions & 1 deletion cmd/api/tweets/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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"`
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")
MissingTweetHash = errors.New("missing tweet hash")
MissingTweetUUID = errors.New("missing tweet UUID")
MissingTweetSearchCriteriaID = errors.New("missing tweet search criteria ID")
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/api/tweets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func InsertHandlerV1(insertTweets Insert) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

var tweets []TweetDTO
err := json.NewDecoder(r.Body).Decode(&tweets)
if err != nil {
Expand Down 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.Hash == nil {
return MissingTweetHash
if tweet.UUID == "" {
return MissingTweetUUID
}

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_failsWhenTweetHashIsNotPresentInBody(t *testing.T) {
func TestInsertHandlerV1_failsWhenTweetUUIDIsNotPresentInBody(t *testing.T) {
mockInsert := tweets.MockInsert(nil)
mockResponseWriter := httptest.NewRecorder()
mockTweets := tweets.MockTweetsDTOs()
mockTweets[0].Hash = nil
mockTweets[0].UUID = ""
mockBody, _ := json.Marshal(mockTweets)
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/tweets/v1", bytes.NewReader(mockBody))

Expand Down
10 changes: 5 additions & 5 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(uuid, hash, author, avatar, posted_at, is_a_reply, text_content, images, quote_id, search_criteria_id)
INSERT INTO tweets(uuid, 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;
ON CONFLICT (uuid, search_criteria_id) DO NOTHING;
`
parameters = 10
parameters = 9
)

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, $%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)
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)

var postedAt *time.Time
if tweet.PostedAt != "" {
Expand Down
2 changes: 0 additions & 2 deletions cmd/api/tweets/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func MockInsert(err error) Insert {

// MockTweetDTO mocks a TweetDTO
func MockTweetDTO() TweetDTO {
hash := "02bd92faa38aaa6cc0ea75e59937a1ef8d6ad3a9f75f3ac4166fef23da9f209b"
avatar := "https://testuseravatar.com"

textContent := "test"
Expand All @@ -24,7 +23,6 @@ func MockTweetDTO() TweetDTO {

return TweetDTO{
UUID: "1234567890987654321",
Hash: &hash,
IsAReply: true,
Author: "TestAuthor",
Avatar: &avatar,
Expand Down
6 changes: 2 additions & 4 deletions migrations/005_create_tweets_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
CREATE TABLE IF NOT EXISTS tweets (
id SERIAL PRIMARY KEY,
uuid TEXT NOT NULL,
hash TEXT NOT NULL,
author TEXT NOT NULL,
avatar TEXT,
posted_at TIMESTAMP WITH TIME ZONE,
Expand All @@ -12,7 +11,7 @@ CREATE TABLE IF NOT EXISTS tweets (
quote_id INTEGER NULL,
search_criteria_id INTEGER NOT NULL,

CONSTRAINT uq_hash_search_criteria UNIQUE (hash, search_criteria_id),
CONSTRAINT uq_uuid_search_criteria UNIQUE (uuid, 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 @@ -24,8 +23,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.uuid IS 'UUID 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';
COMMENT ON COLUMN tweets.posted_at IS 'Timestamp indicating when the tweet was posted';
Expand Down

0 comments on commit 93fc0db

Please sign in to comment.