Add ability to update post media and retrieve posts with their associated media

Introduce `getPostWithMedia` and `updatePostMedia` methods to `DatabaseStorage` for handling post media associations.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/ER8Naji
This commit is contained in:
michaelschal committed 2025-10-09 08:20:51 +00:00
1 parent 344da0f78e
commit 5439566577
1 file changed
+32
+32
View File
@@ -58,9 +58,11 @@ export interface IStorage {
// Posts
getPosts(userId: string): Promise<Post[]>;
getPost(id: string): Promise<Post | undefined>;
getPostWithMedia(id: string): Promise<{ post: Post; media: Media[] } | undefined>;
createPost(post: InsertPost): Promise<Post>;
updatePost(id: string, post: Partial<InsertPost>): Promise<Post>;
deletePost(id: string): Promise<void>;
updatePostMedia(postId: string, mediaIds: string[]): Promise<void>;
// Scheduled Posts
getScheduledPosts(userId: string, startDate?: Date, endDate?: Date): Promise<ScheduledPost[]>;
@@ -181,6 +183,23 @@ export class DatabaseStorage implements IStorage {
return post || undefined;
}
async getPostWithMedia(id: string): Promise<{ post: Post; media: Media[] } | undefined> {
const post = await this.getPost(id);
if (!post) return undefined;
const postMediaLinks = await this.getPostMedia(id);
const mediaItems: Media[] = [];
for (const link of postMediaLinks) {
const mediaItem = await this.getMediaById(link.mediaId);
if (mediaItem) {
mediaItems.push(mediaItem);
}
}
return { post, media: mediaItems };
}
async createPost(post: InsertPost): Promise<Post> {
const [newPost] = await db.insert(posts).values(post).returning();
return newPost;
@@ -195,6 +214,19 @@ export class DatabaseStorage implements IStorage {
await db.delete(posts).where(eq(posts.id, id));
}
async updatePostMedia(postId: string, mediaIds: string[]): Promise<void> {
await db.delete(postMedia).where(eq(postMedia.postId, postId));
if (mediaIds.length > 0) {
const postMediaEntries = mediaIds.map((mediaId, index) => ({
postId,
mediaId,
displayOrder: index,
}));
await db.insert(postMedia).values(postMediaEntries);
}
}
// Scheduled Posts
async getScheduledPosts(userId: string, startDate?: Date, endDate?: Date): Promise<any[]> {
let query = db