Skip to content

Commit

Permalink
fix(api): make sure movies are filtered and sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
manekenpix committed Jul 13, 2024
1 parent a257325 commit e960584
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion api/src/list/dto/query.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class QueryDto {
@IsInt()
@Type(() => Number)
@IsOptional()
per_page = 10;
per_page = 500;

@IsInt()
@Type(() => Number)
Expand Down
70 changes: 48 additions & 22 deletions api/src/movie/dao/movie.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 { MovieDto } from '../dto/movie.dto';
import { QueryDto } from '../dto/query.dto';
import { PER_PAGE, PAGE_NUMBER } from '../../utils';
import { Movie } from '@prisma/client';

@Injectable()
export class MovieDao {
Expand Down Expand Up @@ -57,31 +58,29 @@ export class MovieDao {

async getMovies(query: QueryDto) {
const queryCondition = {
...(query?.userId && {
user: {
some: {
id: query.userId,
},
},
}),
...(query?.listId && {
listMovie: {
some: {
listId: query.listId,
},
AND: [
{
...(query?.userId && {
user: {
some: {
id: query.userId,
},
},
}),
...(query?.listId && {
listMovie: {
some: {
listId: query.listId,
},
},
}),
},
}),
],
};

const [movies, count] = await Promise.all([
this.prisma.listMovie.findMany({
where: query?.listId ? { listId: query?.listId } : {},
include: {
Movie: true,
},
orderBy: {
createdAt: 'desc',
},
this.prisma.movie.findMany({
where: queryCondition,
take: query?.per_page || PER_PAGE,
skip:
(query?.page_number || PAGE_NUMBER) * (query?.per_page || PER_PAGE),
Expand All @@ -92,7 +91,34 @@ export class MovieDao {
}),
]);

return { movies, count };
let sortedMovies: Movie[] = [];

if (query.listId && count) {
const m = movies.map((m) => m.id);

const lm = await this.prisma.listMovie.findMany({
where: {
movieId: {
in: m,
},
listId: query.listId,
},
orderBy: {
createdAt: 'desc',
},
});

lm.forEach((l) => {
const movie = movies.find((mv) => mv.id === l.movieId);

if (movie) {
movie.createdAt = l.createdAt;
sortedMovies.push(movie);
}
});
} else sortedMovies = movies;

return { movies: sortedMovies, count };
}

async updateWatchedStatus(
Expand Down
2 changes: 1 addition & 1 deletion api/src/movie/dto/query.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class QueryDto {
@IsInt()
@Type(() => Number)
@IsOptional()
per_page? = 10;
per_page? = 500;

@IsInt()
@Type(() => Number)
Expand Down
14 changes: 3 additions & 11 deletions api/src/movie/movie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ export class MovieService {
async getMovies(query: QueryDto) {
const { movies, count } = await this.moviesDao.getMovies(query);

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

return { movies: moviesWithCreatedAt, count };
return { movies, count };
}

async createMovies(movies: MovieDto[], listId: string) {
Expand All @@ -44,14 +36,14 @@ export class MovieService {
async updateWatchedStatus(movieId: string, userId: string) {
const { movies } = await this.getMovies({ userId });

const hasWatched = !!movies.find(
const hasWatched = movies.find(
(watchedMovie) => watchedMovie.id === movieId,
);

return await this.moviesDao.updateWatchedStatus(
movieId,
userId,
hasWatched,
!!hasWatched,
);
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/user/dto/query.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class QueryDto {
@IsInt()
@Type(() => Number)
@IsOptional()
per_page = 10;
per_page = 500;

@IsInt()
@Type(() => Number)
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const PER_PAGE = 10;
export const PER_PAGE = 500;
export const PAGE_NUMBER = 0;

0 comments on commit e960584

Please sign in to comment.