🚧 wip: implement collection upsert logic

This commit is contained in:
2026-03-28 21:59:44 +07:00
parent 56c921e800
commit 3768ae4c26
4 changed files with 77 additions and 1 deletions

View File

@ -369,6 +369,7 @@ Table user_logs {
Table collections {
id String [pk]
name String [not null]
slug String [not null]
medias medias [not null]
owner users [not null]
ownerId String [not null]
@ -379,6 +380,10 @@ Table collections {
deletedAt DateTime
createdAt DateTime [default: `now()`, not null]
updatedAt DateTime [default: `now()`, not null]
indexes {
(slug, ownerId) [unique]
}
}
Table watch_histories {

View File

@ -1,10 +1,23 @@
import { Context, Static } from "elysia";
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
import { addItemToCollectionSchema } from "../schemas/addItemToCollection.schema";
import { addItemToCollectionService } from "../services/addItemToCollection.service";
import { mainErrorHandler } from "../../../helpers/error/handler";
export const addItemToCollectionController = async (ctx: {
set: Context["set"];
headers: Static<typeof addItemToCollectionSchema.headers>;
params: Static<typeof addItemToCollectionSchema.params>;
body: Static<typeof addItemToCollectionSchema.body>;
}) => {
return returnWriteResponse(ctx.set, 200, "Item added to collection successfully" + ctx.headers.cookie);
try {
const savedItem = await addItemToCollectionService({
cookie: ctx.headers.cookie,
collectionName: ctx.params.name,
mediaId: ctx.body.itemId,
});
return returnWriteResponse(ctx.set, 200, "Item added to collection successfully", savedItem);
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,43 @@
import slugify from "slugify";
import { AppError } from "../../../helpers/error/instances/app";
import { prisma } from "../../../utils/databases/prisma/connection";
import { generateUUIDv7 } from "../../../helpers/databases/uuidv7";
export interface UpsertUserCollectionRepositoryPayload {
userId: string;
collectionName: string;
mediaConnectId: string;
}
export const upsertUserCollectionRepository = async (payload: UpsertUserCollectionRepositoryPayload) => {
try {
return await prisma.collection.upsert({
where: {
slug_ownerId: {
slug: slugify(payload.collectionName, { lower: true }),
ownerId: payload.userId,
},
},
update: {
medias: {
connect: {
id: payload.mediaConnectId,
},
},
},
create: {
id: generateUUIDv7(),
name: payload.collectionName,
slug: slugify(payload.collectionName, { lower: true }),
ownerId: payload.userId,
medias: {
connect: {
id: payload.mediaConnectId,
},
},
},
});
} catch (error) {
throw new AppError(500, "Failed to upsert user collection");
}
};

View File

@ -0,0 +1,15 @@
import { parse } from "cookie";
import { jwtDecode } from "../../../helpers/http/jwt/decode";
import { tokenValidationService } from "../../auth/services/http/tokenValidation.service";
import slugify from "slugify";
export type AddItemToCollectionPayload = {
cookie: string;
collectionName: string;
mediaId: string;
};
export const addItemToCollectionService = async (payload: AddItemToCollectionPayload) => {
const { auth_token } = parse(payload.cookie);
return (await tokenValidationService(auth_token as string)).user.id;
};