Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: /api/users/:id/wishlist/add-lecture API #68

Merged
merged 16 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/common/interfaces/dto/wishlist/wishlist.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Type } from 'class-transformer';
import { IsNumber } from 'class-validator';

export class WishlistAddLectureDto {
@IsNumber()
@Type(() => Number)
lecture!: number;
}
5 changes: 5 additions & 0 deletions src/common/interfaces/dto/wishlist/wishlist.response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NestedLectureResponseDto } from '../lecture/lecture.response.dto';

export interface WishlistWithLecturesResponseDto {
lectures: NestedLectureResponseDto[];
}
42 changes: 23 additions & 19 deletions src/common/interfaces/serializer/lecture.serializer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { applyOrder } from 'src/common/utils/search.utils';
import { LectureDetails, NESTED } from '../../schemaTypes/types';
import {
LectureDetails,
LectureExtended,
NESTED,
isLectureDetails,
} from '../../schemaTypes/types';
import { LectureResponseDto } from '../dto/lecture/lecture.response.dto';
import { toJsonClasstime } from './classtime.serializer';
import { toJsonExamtime } from './examtime.serializer';
import { toJsonProfessor } from './professor.serializer';

export function toJsonLecture<T>(
lecture: T extends NESTED
? Omit<LectureDetails, 'subject_classtime' & 'subject_examtime'>
: LectureDetails,
nested: T extends NESTED ? true : false,
export function toJsonLecture<T extends boolean>(
lecture: T extends NESTED ? LectureExtended : LectureDetails,
nested: T,
): LectureResponseDto {
let result = {
id: lecture.id,
Expand Down Expand Up @@ -52,17 +55,18 @@ export function toJsonLecture<T>(
return result;
}

result = Object.assign(result, {
grade: lecture.grade,
load: lecture.load,
speech: lecture.speech,
classtimes: lecture.subject_classtime.map((classtime) =>
toJsonClasstime(classtime),
),
examtimes: lecture.subject_examtime.map((examtime) =>
toJsonExamtime(examtime),
),
});

return result;
if (isLectureDetails(lecture)) {
result = Object.assign(result, {
grade: lecture.grade,
load: lecture.load,
speech: lecture.speech,
classtimes: lecture.subject_classtime.map((classtime) =>
toJsonClasstime(classtime),
),
examtimes: lecture.subject_examtime.map((examtime) =>
toJsonExamtime(examtime),
),
});
return result;
} else throw new Error("Lecture is not of type 'LectureDetails'");
ddungiii marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 13 additions & 0 deletions src/common/interfaces/serializer/wishlist.serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WishlistWithLectures } from '../../schemaTypes/types';
import { WishlistWithLecturesResponseDto } from '../dto/wishlist/wishlist.response.dto';
import { toJsonLecture } from './lecture.serializer';

export const toJsonWishlist = (
wishlist: WishlistWithLectures,
): WishlistWithLecturesResponseDto => {
return {
lectures: wishlist.timetable_wishlist_lectures.map((lecture) =>
toJsonLecture(lecture.subject_lecture, true),
),
};
};
34 changes: 34 additions & 0 deletions src/common/schemaTypes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const courseDetails = Prisma.validator<Prisma.subject_courseArgs>()({
},
});

export const lectureExtended = Prisma.validator<Prisma.subject_lectureArgs>()({
include: {
subject_department: true,
subject_lecture_professors: { include: { professor: true } },
},
});

export const lectureDetails = Prisma.validator<Prisma.subject_lectureArgs>()({
include: {
subject_department: true,
Expand Down Expand Up @@ -59,6 +66,20 @@ const lectureReviews = Prisma.validator<Prisma.subject_lectureArgs>()({
},
});

export const wishlistLectures =
Prisma.validator<Prisma.timetable_wishlistArgs>()({
include: {
timetable_wishlist_lectures: {
include: {
subject_lecture: {
include: lectureExtended.include,
},
},
where: { subject_lecture: { deleted: false } },
},
},
});

export type LectureReviewDetails = Prisma.subject_lectureGetPayload<
typeof lectureReviews
>;
Expand All @@ -68,6 +89,9 @@ export type ReviewDetails = Prisma.review_reviewGetPayload<
export type LectureDetails = Prisma.subject_lectureGetPayload<
typeof lectureDetails
>;
export type LectureExtended = Prisma.subject_lectureGetPayload<
typeof lectureExtended
>;
export type LectureBasic = Prisma.subject_lectureGetPayload<null>;
export type CourseDetails = Prisma.subject_courseGetPayload<
typeof courseDetails
Expand All @@ -77,3 +101,13 @@ export type TimeTableDetails = Prisma.timetable_timetableGetPayload<
>;
export type TimeTableBasic = Prisma.timetable_timetableGetPayload<null>;
export type SemesterBasic = Prisma.subject_semesterGetPayload<null>;

export type WishlistWithLectures = Prisma.timetable_wishlistGetPayload<
typeof wishlistLectures
>;

export function isLectureDetails(
lecture: LectureExtended | LectureDetails,
): lecture is LectureDetails {
return 'subject_classtime' in lecture;
ddungiii marked this conversation as resolved.
Show resolved Hide resolved
}
30 changes: 27 additions & 3 deletions src/modules/wishlist/wishlist.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { Controller } from '@nestjs/common';
import {
Body,
Controller,
Param,
Post,
UnauthorizedException,
} from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { GetUser } from 'src/common/decorators/get-user.decorator';
import { WishlistAddLectureDto } from 'src/common/interfaces/dto/wishlist/wishlist.request.dto';
import { toJsonWishlist } from 'src/common/interfaces/serializer/wishlist.serializer';
import { WishlistService } from './wishlist.service';

@Controller('wishlist')
export class WishlistController {}
@Controller('api/users/:userId/wishlist')
export class WishlistController {
constructor(private readonly wishlistService: WishlistService) {}

@Post('add-lecture')
async addLecture(
@Param('userId') userId: number,
@Body() body: WishlistAddLectureDto,
@GetUser() user: session_userprofile,
) {
if (userId !== user.id) throw new UnauthorizedException(); // TODO: Better message
const wishlist = await this.wishlistService.addLecture(user.id, body);
return toJsonWishlist(wishlist);
}
}
38 changes: 36 additions & 2 deletions src/modules/wishlist/wishlist.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
import { Injectable } from '@nestjs/common';
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { WishlistAddLectureDto } from 'src/common/interfaces/dto/wishlist/wishlist.request.dto';
import { LectureRepository } from 'src/prisma/repositories/lecture.repository';
import { WishlistRepository } from 'src/prisma/repositories/wishlist.repository';

@Injectable()
export class WishlistService {}
export class WishlistService {
constructor(
private readonly wishlistRepository: WishlistRepository,
private readonly lectureRepository: LectureRepository,
) {}

async addLecture(userId: number, body: WishlistAddLectureDto) {
const wishlist = await this.wishlistRepository.getOrCreateWishlist(userId);

if (
await this.wishlistRepository.lectureExistsInWishlist(
wishlist.id,
body.lecture,
)
)
throw new BadRequestException("Wrong field 'lecture' in request data");

const lecture = await this.lectureRepository.getLectureById(body.lecture);
if (!lecture)
throw new NotFoundException(
`Lecture with id ${body.lecture} does not exist`,
);

await this.wishlistRepository.addLecture(wishlist.id, lecture.id);

return await this.wishlistRepository.getWishlistLectures(wishlist.id);
}
}
45 changes: 45 additions & 0 deletions src/prisma/repositories/wishlist.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable } from '@nestjs/common';
import {
WishlistWithLectures,
wishlistLectures,
} from 'src/common/schemaTypes/types';
import { PrismaService } from '../prisma.service';

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

async getOrCreateWishlist(userId: number) {
return await this.prisma.timetable_wishlist.upsert({
where: { user_id: userId },
create: { user_id: userId },
update: {},
});
}

async addLecture(wishlistId: number, lectureId: number) {
return await this.prisma.timetable_wishlist_lectures.upsert({
where: {
wishlist_id_lecture_id: {
lecture_id: lectureId,
wishlist_id: wishlistId,
},
},
create: { wishlist_id: wishlistId, lecture_id: lectureId },
update: {},
});
}

async lectureExistsInWishlist(wishlistId: number, lectureId: number) {
ddungiii marked this conversation as resolved.
Show resolved Hide resolved
return await this.prisma.timetable_wishlist_lectures.findFirst({
where: { lecture_id: lectureId, wishlist_id: wishlistId },
});
}

async getWishlistLectures(wishlistId: number) {
return (await this.prisma.timetable_wishlist.findUnique({
where: { id: wishlistId },
include: wishlistLectures.include,
})) as WishlistWithLectures;
ddungiii marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading