Skip to content

Commit

Permalink
feat(api): add ListMovie bridging table
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispinkney committed Jun 18, 2024
1 parent 915ec4b commit 0a95e2b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:
- You are about to drop the `_ListToMovie` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "_ListToMovie" DROP CONSTRAINT "_ListToMovie_A_fkey";

-- DropForeignKey
ALTER TABLE "_ListToMovie" DROP CONSTRAINT "_ListToMovie_B_fkey";

-- DropTable
DROP TABLE "_ListToMovie";

-- CreateTable
CREATE TABLE "ListMovie" (
"list_id" UUID NOT NULL,
"movie_id" UUID NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "ListMovie_pkey" PRIMARY KEY ("list_id","movie_id")
);

-- AddForeignKey
ALTER TABLE "ListMovie" ADD CONSTRAINT "ListMovie_list_id_fkey" FOREIGN KEY ("list_id") REFERENCES "List"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ListMovie" ADD CONSTRAINT "ListMovie_movie_id_fkey" FOREIGN KEY ("movie_id") REFERENCES "Movie"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
36 changes: 23 additions & 13 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,40 @@ datasource db {
}

model List {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
isPrivate Boolean @map("is_private")
creatorId String @map("creator_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
movie Movie[]
isPrivate Boolean @map("is_private")
creatorId String @map("creator_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
listMovie ListMovie[]
user User[]
comments Comment[]
}

model ListMovie {
listId String @map("list_id") @db.Uuid
movieId String @map("movie_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at")
List List @relation(fields: [listId], references: [id])
Movie Movie @relation(fields: [movieId], references: [id])
@@id([listId, movieId])
}

model Movie {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
title String
description String
genre String[]
releaseDate String @map("release_date")
posterUrl String @map("poster_url")
releaseDate String @map("release_date")
posterUrl String @map("poster_url")
rating Float
tmdbId Int @unique @map("tmdb_id")
tmdbId Int @unique @map("tmdb_id")
eTag String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
list List[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
listMovie ListMovie[]
user User[]
}

Expand Down
7 changes: 4 additions & 3 deletions api/src/list/dao/list.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PrismaService } from '../../prisma/prisma.service';
import { Role, User } from '@prisma/client';
import { UpdateListDto } from '../dto/update-list.dto';
import { QueryDto } from '../dto/query.dto';
import { PER_PAGE, PAGE_NUMBER } from '../../utils';

@Injectable()
export class ListDao {
Expand Down Expand Up @@ -60,8 +61,8 @@ export class ListDao {
const [lists, count] = await Promise.all([
this.prisma.list.findMany({
where: queryCondition,
take: query.per_page || 10,
skip: (query.page_number || 0) * (query.per_page || 10),
take: query.per_page || PER_PAGE,
skip: (query.page_number || PAGE_NUMBER) * (query.per_page || PER_PAGE),
orderBy: {
createdAt: 'desc',
},
Expand Down Expand Up @@ -118,7 +119,7 @@ export class ListDao {
async updateListPrivacy(listId: string) {
const list = await this.prisma.list.findUniqueOrThrow({
where: { id: listId },
include: { user: true, movie: true },
include: { user: true, listMovie: true },
});

return await this.prisma.list.update({
Expand Down
43 changes: 27 additions & 16 deletions api/src/movie/dao/movie.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
import { PrismaService } from '../../prisma/prisma.service';
import { MovieDto } from '../dto/movie.dto';
import { QueryDto } from '../dto/query.dto';
import { PER_PAGE, PAGE_NUMBER } from '../../utils';

@Injectable()
export class MovieDao {
Expand Down Expand Up @@ -34,15 +35,18 @@ export class MovieDao {

await Promise.all(
newMovies.map((movie) =>
this.prisma.movie.update({
where: { id: movie.id },
data: {
list: {
connect: {
id: listId,
},
this.prisma.listMovie.upsert({
where: {
listId_movieId: {
listId,
movieId: movie.id,
},
},
create: {
listId,
movieId: movie.id,
},
update: {},
}),
),
);
Expand All @@ -56,24 +60,31 @@ export class MovieDao {
...(query?.userId && {
user: {
some: {
id: query?.userId,
id: query.userId,
},
},
}),
...(query?.listId && {
list: {
listMovie: {
some: {
id: query?.listId,
listId: query.listId,
},
},
}),
};

const [movies, count] = await Promise.all([
this.prisma.movie.findMany({
where: queryCondition,
take: query?.per_page || 10,
skip: (query?.page_number || 0) * (query?.per_page || 10),
this.prisma.listMovie.findMany({
where: query?.listId ? { listId: query?.listId } : {},
include: {
Movie: true,
},
orderBy: {
createdAt: 'desc',
},
take: query?.per_page || PER_PAGE,
skip:
(query?.page_number || PAGE_NUMBER) * (query?.per_page || PER_PAGE),
}),

this.prisma.movie.count({
Expand Down Expand Up @@ -116,8 +127,8 @@ export class MovieDao {
return await this.prisma.movie.update({
where: { id: movieId },
data: {
list: {
disconnect: { id: listId },
listMovie: {
delete: { listId_movieId: { listId, movieId } },
},
},
});
Expand Down
12 changes: 11 additions & 1 deletion api/src/movie/movie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ export class MovieService {
constructor(private moviesDao: MovieDao, private tmdbDao: TMDBDao) {}

async getMovies(query: QueryDto) {
return await this.moviesDao.getMovies(query);
const { movies, count } = await this.moviesDao.getMovies(query);

const moviesWithCreatedAt = movies.map((listMovie) => {
const { Movie, createdAt } = listMovie;
return {
...Movie,
createdAt,
};
});

return { movies: moviesWithCreatedAt, count };
}

async createMovies(movies: MovieDto[], listId: string) {
Expand Down
7 changes: 4 additions & 3 deletions api/src/user/dao/user.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CreateUserDto } from '../dto/create-user.dto';
import { QueryDto } from '../dto/query.dto';
import { FriendStatus } from '../user.service';
import { Role } from '@prisma/client';
import { PAGE_NUMBER, PER_PAGE } from '../../utils';

@Injectable()
export class UserDao {
Expand All @@ -24,8 +25,8 @@ export class UserDao {
const [users, count] = await Promise.all([
this.prisma.user.findMany({
where: queryCondition,
take: query.per_page || 10,
skip: (query.page_number || 0) * (query.per_page || 10),
take: query.per_page || PER_PAGE,
skip: (query.page_number || PAGE_NUMBER) * (query.per_page || PER_PAGE),
}),

this.prisma.user.count({
Expand Down Expand Up @@ -60,7 +61,7 @@ export class UserDao {
include: {
list: {
include: {
movie: true,
listMovie: true,
},
},
movie: true,
Expand Down

0 comments on commit 0a95e2b

Please sign in to comment.