Skip to content

Commit

Permalink
feat(api): implement movies module
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispinkney authored and manekenpix committed Mar 1, 2024
1 parent a4bc9b2 commit 5dea878
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 174 deletions.
2 changes: 1 addition & 1 deletion api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ module.exports = {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars': 'warn',
},
};
10 changes: 9 additions & 1 deletion api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import { UsersModule } from './users/users.module';
import { ListsModule } from './lists/lists.module';
import { EmailModule } from './email/email.module';
import { ImageModule } from './image/image.module';
import { MoviesModule } from './movies/movies.module';

@Module({
imports: [ConfigModule, UsersModule, ListsModule, EmailModule, ImageModule],
imports: [
ConfigModule,
UsersModule,
ListsModule,
EmailModule,
ImageModule,
MoviesModule,
],
controllers: [AppController],
providers: [
{
Expand Down
119 changes: 4 additions & 115 deletions api/src/lists/daos/list.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';
import { PrismaService } from '../../prisma/prisma.service';
import { UpdateListDto } from '../dtos/update-list.dto';
import { CreateListDto } from '../dtos/create-list.dto';
import { Role, User } from '@prisma/client';

@Injectable()
export class ListsDao {
export class ListDao {
constructor(private readonly prisma: PrismaService) {}

async getPublicList(listId: string) {
Expand Down Expand Up @@ -112,67 +111,18 @@ export class ListsDao {
});
}

async createList(createList: CreateListDto, userId: string) {
let newMovies;
const list = await this.prisma.list.create({
async createList(listName: string, userId: string) {
return await this.prisma.list.create({
data: {
id: uuidv4(),
name: createList.name,
name: listName,
isPrivate: true,
creatorId: userId,
user: {
connect: { id: userId },
},
},
include: {
movie: true,
},
});

if (createList?.movie?.length) {
newMovies = await Promise.all(
createList.movie.map((movie) =>
this.prisma.movie.upsert({
where: { imdbId: movie.imdbId },
create: {
id: uuidv4(),
title: movie.title,
description: movie.description,
genre: movie.genre,
releaseDate: movie.releaseDate,
posterUrl: movie.posterUrl,
rating: movie.rating,
imdbId: movie.imdbId,
},
update: {},
}),
),
);

await Promise.all(
newMovies.map((movie) =>
this.prisma.movie.update({
where: { id: movie.id },
data: {
list: {
connect: {
id: list.id,
},
},
},
}),
),
);
}

const createdList = await this.prisma.list.findUniqueOrThrow({
where: { id: list.id },
include: {
movie: true,
},
});

return createdList;
}

async updateListPrivacy(listId: string) {
Expand All @@ -190,44 +140,6 @@ export class ListsDao {
}

async updateList(updateListDto: UpdateListDto) {
let newMovies = [];

if (updateListDto?.movie?.length) {
newMovies = await Promise.all(
updateListDto.movie.map((movie) =>
this.prisma.movie.upsert({
where: { imdbId: movie.imdbId },
create: {
id: uuidv4(),
title: movie.title,
description: movie.description,
genre: movie.genre,
releaseDate: movie.releaseDate,
posterUrl: movie.posterUrl,
rating: movie.rating,
imdbId: movie.imdbId,
},
update: {},
}),
),
);

await Promise.all(
newMovies.map((movie) =>
this.prisma.movie.update({
where: { id: movie.id },
data: {
list: {
connect: {
id: updateListDto.listId,
},
},
},
}),
),
);
}

return await this.prisma.list.update({
where: { id: updateListDto.listId },
data: {
Expand All @@ -237,29 +149,6 @@ export class ListsDao {
});
}

async updateWatchedStatus(movieId: string, userId: string) {
const user = await this.getWatchedMovies(userId);

const hasWatched = user.movie.find(
(watchedMovie) => watchedMovie.id === movieId,
);

return await this.prisma.user.update({
where: { id: userId },
data: {
movie: {
...(hasWatched
? {
disconnect: { id: movieId },
}
: {
connect: { id: movieId },
}),
},
},
});
}

async deleteList(listId: string, user: User) {
const list = await this.prisma.list.findUniqueOrThrow({
where: { id: listId },
Expand Down
33 changes: 1 addition & 32 deletions api/src/lists/dtos/create-list-return.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,7 @@ import { ValidateNested } from 'class-validator';
import { UserDto } from '../../users/dtos/user.dto';
import { CommentDto } from './comments.dto';

class Movie {
@Expose()
id: number;

@Expose()
title: string;

@Expose()
description?: string;

@Expose()
genre: string[];

@Expose()
releaseDate: string;

@Expose()
posterUrl: string;

@Expose()
rating: string;

@Expose()
imdbId: string;
}

class List {
export class List {
@Expose()
id: number;

Expand All @@ -48,11 +22,6 @@ class List {
@Expose()
updatedAt: Date;

@Expose()
@Type(() => Movie)
@ValidateNested()
movie: Movie[];

@Expose()
@Type(() => UserDto)
@ValidateNested()
Expand Down
3 changes: 2 additions & 1 deletion api/src/lists/guards/list.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export class ListAuthGuard implements CanActivate {
const request = context.switchToHttp().getRequest();
const { user } = request;

const listId = request.params.id || request.body.listId;
const listId =
request.params.id || request.body.listId || request.params.listId;

const list = await this.prisma.list.findUniqueOrThrow({
where: { id: listId },
Expand Down
8 changes: 0 additions & 8 deletions api/src/lists/lists.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,6 @@ export class ListsController {
return this.listsService.updateList(body);
}

@UseInterceptors(RemoveListFieldsInterceptor)
@Patch('/movies/:id/updateWatchedStatus')
@HttpCode(HttpStatus.NO_CONTENT)
updateWatchedStatus(@Param('id') movieId: string, @Req() req: Request) {
if (!req.user) throw new BadRequestException('req contains no user');
return this.listsService.updateWatchedStatus(movieId, req.user.id);
}

@UseInterceptors(RemoveListFieldsInterceptor)
@UseGuards(ListAuthGuard)
@Delete('/:id')
Expand Down
7 changes: 4 additions & 3 deletions api/src/lists/lists.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import { UsersService } from '../users/users.service';
import { PrismaModule } from '../prisma/prisma.module';
import { EmailModule } from '../email/email.module';
import { UsersModule } from '../users/users.module';
import { ListsDao } from './daos/list.dao';
import { ListDao } from './daos/list.dao';
import { CommentDao } from './daos/comment.dao';
import { UsersDao } from '../users/daos/user.dao';
import { CommentsService } from './comments.service';
import { MoviesModule } from '../movies/movies.module';

@Module({
imports: [PrismaModule, EmailModule, UsersModule],
imports: [PrismaModule, EmailModule, UsersModule, MoviesModule],
controllers: [ListsController],
providers: [
ListsService,
ListsDao,
ListDao,
UsersService,
UsersDao,
CommentsService,
Expand Down
31 changes: 22 additions & 9 deletions api/src/lists/lists.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Injectable } from '@nestjs/common';
import { CreateListDto } from './dtos/create-list.dto';
import { UpdateListDto } from './dtos/update-list.dto';
import { ListsDao } from './daos/list.dao';
import { ListDao } from './daos/list.dao';
import { UsersService } from '../users/users.service';
import { EmailService } from '../email/email.service';
import { CloneListDto } from './dtos/clone-list.dto';
import { MoviesService } from '../movies/movies.service';

interface Comment {
id: string;
Expand All @@ -18,9 +19,10 @@ interface Comment {
@Injectable()
export class ListsService {
constructor(
private listsDao: ListsDao,
private listsDao: ListDao,
private usersService: UsersService,
private emailService: EmailService,
private moviesService: MoviesService,
) {}

async getPublicList(listId: string) {
Expand Down Expand Up @@ -90,8 +92,12 @@ export class ListsService {
return userListDetails;
}

async createList(createList: CreateListDto, userId: string) {
return await this.listsDao.createList(createList, userId);
async createList(body: CreateListDto, userId: string) {
const list = await this.listsDao.createList(body.name, userId);

await this.moviesService.createMovies(body.movie, list.id);

return await this.listsDao.getList(list.id);
}

async cloneList(cloneList: CloneListDto, userId: string) {
Expand All @@ -110,7 +116,13 @@ export class ListsService {
})),
};

const clonedList = await this.listsDao.createList(clonedListData, userId);
const clonedList = await this.createList(
{
name: clonedListData.name,
movie: clonedListData.movie,
},
userId,
);

return clonedList;
}
Expand All @@ -120,11 +132,12 @@ export class ListsService {
}

async updateList(updateListDto: UpdateListDto) {
return await this.listsDao.updateList(updateListDto);
}
await this.moviesService.createMovies(
updateListDto.movie,
updateListDto.listId,
);

async updateWatchedStatus(movieId: string, userId: string) {
return await this.listsDao.updateWatchedStatus(movieId, userId);
return await this.listsDao.updateList(updateListDto);
}

async deleteList(listId: string, userId: string) {
Expand Down
Loading

0 comments on commit 5dea878

Please sign in to comment.