🚧 wip: implement collection upsert logic
This commit is contained in:
@ -369,6 +369,7 @@ Table user_logs {
|
|||||||
Table collections {
|
Table collections {
|
||||||
id String [pk]
|
id String [pk]
|
||||||
name String [not null]
|
name String [not null]
|
||||||
|
slug String [not null]
|
||||||
medias medias [not null]
|
medias medias [not null]
|
||||||
owner users [not null]
|
owner users [not null]
|
||||||
ownerId String [not null]
|
ownerId String [not null]
|
||||||
@ -379,6 +380,10 @@ Table collections {
|
|||||||
deletedAt DateTime
|
deletedAt DateTime
|
||||||
createdAt DateTime [default: `now()`, not null]
|
createdAt DateTime [default: `now()`, not null]
|
||||||
updatedAt DateTime [default: `now()`, not null]
|
updatedAt DateTime [default: `now()`, not null]
|
||||||
|
|
||||||
|
indexes {
|
||||||
|
(slug, ownerId) [unique]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Table watch_histories {
|
Table watch_histories {
|
||||||
|
|||||||
@ -1,10 +1,23 @@
|
|||||||
import { Context, Static } from "elysia";
|
import { Context, Static } from "elysia";
|
||||||
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
||||||
import { addItemToCollectionSchema } from "../schemas/addItemToCollection.schema";
|
import { addItemToCollectionSchema } from "../schemas/addItemToCollection.schema";
|
||||||
|
import { addItemToCollectionService } from "../services/addItemToCollection.service";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
|
||||||
export const addItemToCollectionController = async (ctx: {
|
export const addItemToCollectionController = async (ctx: {
|
||||||
set: Context["set"];
|
set: Context["set"];
|
||||||
headers: Static<typeof addItemToCollectionSchema.headers>;
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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");
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user