Skip to content

Commit 0e16f4e

Browse files
committed
feat: save reddit post title
1 parent 9bcee6a commit 0e16f4e

6 files changed

Lines changed: 108 additions & 2 deletions

File tree

ApplicationData/Services/LinkProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ SELECT 1 FROM links
164164
if (existingLink is true)
165165
return Result<int, CreateLinkError>.CreateFailure(new LinkAlreadyExists());
166166

167-
using var tx = await connection.CreateTransaction(IsolationLevel.Serializable, CancellationToken.None);
167+
await using var tx = await connection.CreateTransaction(IsolationLevel.Serializable, CancellationToken.None);
168+
169+
await tx.Execute("""
170+
INSERT INTO reddit_posts (reddit_post_id, post_title)
171+
VALUES (@redditPostId, @postTitle)
172+
ON CONFLICT DO NOTHING
173+
""", new { link.RedditPostId, postTitle = link.RedditPostTitle });
168174

169175
var linkIdResult = await tx.ExecuteScalar<int?>("""
170176
INSERT INTO links (reddit_post_id, link_url, link_type, created_at, is_deleted, owner)

DataClasses/DbModels.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Link = {
3636

3737
type NewLink = {
3838
RedditPostId : string
39+
RedditPostTitle : string
3940
LinkUrl : string
4041
LinkType : LinkKind
4142
OwnerUserId : int32
@@ -46,4 +47,4 @@ type NewUser = {
4647
DeveloperUsername : string
4748
Weight : int32 option
4849
IsAdministrator : bool
49-
}
50+
}

WebApi/Controllers/LinkController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public async Task<IActionResult> SubmitLink([FromBody] SubmitLinkRequest linkReq
6969
async () =>
7070
await _linkProvider.CreateLink(new NewLink(
7171
redditPostId: linkRequest.RedditPostId,
72+
redditPostTitle: submission.Title,
7273
linkUrl: validUrl.OriginalString,
7374
linkKind,
7475
ownerUserId: loggedInUserId
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using FruityFoundation.DataAccess.Abstractions;
2+
3+
namespace WebApi.Scripts;
4+
5+
/// <summary>
6+
/// Create reddit posts table
7+
/// </summary>
8+
public class Script_2024_08_24_01_CreateRedditPostsTable : IDbMaintenanceScript
9+
{
10+
/// <inheritdoc />
11+
public async Task Run(IDatabaseConnection<ReadWrite> dbConnection)
12+
{
13+
var tableExists = await dbConnection.ExecuteScalar<bool>(
14+
"SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type='table' AND name = 'reddit_posts')");
15+
16+
if (tableExists)
17+
return;
18+
19+
await dbConnection.Execute(
20+
"""
21+
create table reddit_posts
22+
(
23+
reddit_post_id TEXT not null
24+
constraint PK_reddit_posts_reddit_post_id
25+
primary key,
26+
post_title TEXT null -- nullable for now because we have several thousand existing rows. tomorrow's problem.
27+
);
28+
""");
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using FruityFoundation.DataAccess.Abstractions;
2+
3+
namespace WebApi.Scripts;
4+
5+
/// <summary>
6+
/// Add foreign key to reddit posts
7+
/// </summary>
8+
public class Script_2024_08_24_02_PopulateRedditPostIds : IDbMaintenanceScript
9+
{
10+
/// <inheritdoc />
11+
public async Task Run(IDatabaseConnection<ReadWrite> dbConnection)
12+
{
13+
await dbConnection.Execute(
14+
"""
15+
INSERT INTO reddit_posts (reddit_post_id, post_title)
16+
SELECT rc.reddit_post_id, NULL
17+
FROM reddit_comments rc
18+
WHERE NOT EXISTS (
19+
SELECT 1
20+
FROM reddit_posts rp
21+
WHERE rp.reddit_post_id = rc.reddit_post_id
22+
)
23+
""");
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using FruityFoundation.DataAccess.Abstractions;
2+
3+
namespace WebApi.Scripts;
4+
5+
/// <summary>
6+
/// Add foreign key to reddit posts
7+
/// </summary>
8+
public class Script_2024_08_24_03_AddForeignKeyToRedditPosts : IDbMaintenanceScript
9+
{
10+
/// <inheritdoc />
11+
public async Task Run(IDatabaseConnection<ReadWrite> dbConnection)
12+
{
13+
var foreignKeyExists = await dbConnection.ExecuteScalar<bool>(
14+
"SELECT EXISTS (SELECT 1 FROM pragma_foreign_key_list('reddit_comments') WHERE \"table\" = 'reddit_posts' AND \"from\" = 'reddit_post_id')");
15+
16+
if (foreignKeyExists)
17+
return;
18+
19+
await dbConnection.Execute(
20+
"""
21+
create table reddit_comments_dg_tmp
22+
(
23+
reddit_post_id TEXT not null
24+
constraint PK_RedditComments_RedditPostId
25+
primary key
26+
constraint FK_reddit_comments_reddit_posts_reddit_post_id
27+
references reddit_posts,
28+
reddit_comment_id TEXT not null,
29+
posted_at TEXT not null,
30+
last_updated_at TEXT not null
31+
);
32+
33+
insert into reddit_comments_dg_tmp(reddit_post_id, reddit_comment_id, posted_at, last_updated_at)
34+
select reddit_post_id, reddit_comment_id, posted_at, last_updated_at
35+
from reddit_comments;
36+
37+
drop table reddit_comments;
38+
39+
alter table reddit_comments_dg_tmp
40+
rename to reddit_comments;
41+
""");
42+
}
43+
}

0 commit comments

Comments
 (0)