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 all 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
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SemestersModule } from './modules/semesters/semesters.module';
import { StatusModule } from './modules/status/status.module';
import { TimetablesModule } from './modules/timetables/timetables.module';
import { UserModule } from './modules/user/user.module';
import { WishlistModule } from './modules/wishlist/wishlist.module';
import { PrismaModule } from './prisma/prisma.module';

@Module({
Expand All @@ -26,6 +27,7 @@ import { PrismaModule } from './prisma/prisma.module';
SemestersModule,
TimetablesModule,
StatusModule,
WishlistModule,
],
controllers: [AppController],
providers: [
Expand Down
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[];
}
19 changes: 12 additions & 7 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,6 +55,9 @@ export function toJsonLecture<T>(
return result;
}

if (!isLectureDetails(lecture))
throw new Error("Lecture is not of type 'LectureDetails'");

result = Object.assign(result, {
grade: lecture.grade,
load: lecture.load,
Expand All @@ -63,6 +69,5 @@ export function toJsonLecture<T>(
toJsonExamtime(examtime),
),
});

return result;
}
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, false),
),
};
};
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 wishlistWithLectures =
Prisma.validator<Prisma.timetable_wishlistArgs>()({
include: {
timetable_wishlist_lectures: {
include: {
subject_lecture: {
include: lectureDetails.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 wishlistWithLectures
>;

export function isLectureDetails(
lecture: LectureExtended | LectureDetails,
): lecture is LectureDetails {
return 'subject_classtime' in lecture && 'subject_examtime' in lecture;
}
54 changes: 51 additions & 3 deletions src/modules/wishlist/wishlist.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
import { Controller } from '@nestjs/common';
import {
Body,
Controller,
Get,
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) {}

@Get()
async getLectures(
@Param('userId') userId: number,
@GetUser() user: session_userprofile,
) {
if (userId !== user.id) throw new UnauthorizedException(); // TODO: Better message
const wishlist = await this.wishlistService.getWishlistWithLectures(
user.id,
);
return toJsonWishlist(wishlist);
}

@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);
}

@Post('remove-lecture')
async removeLecture(
@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.removeLecture(user.id, body);
return toJsonWishlist(wishlist);
}
}
4 changes: 4 additions & 0 deletions src/modules/wishlist/wishlist.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from 'src/prisma/prisma.module';
import { WishlistController } from './wishlist.controller';
import { WishlistService } from './wishlist.service';

@Module({
imports: [PrismaModule],
controllers: [WishlistController],
providers: [WishlistService],
})
export class WishlistModule {}
62 changes: 60 additions & 2 deletions src/modules/wishlist/wishlist.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
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 getWishlistWithLectures(userId: number) {
return await this.wishlistRepository.getOrCreateWishlist(userId);
}

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

if (
await this.wishlistRepository.getLectureInWishlist(
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);
const updatedWishlist =
await this.wishlistRepository.getWishlistWithLectures(wishlist.id);
if (!updatedWishlist) throw new Error('Wishlist not found');
return updatedWishlist;
}

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

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

await this.wishlistRepository.removeLecture(wishlist.id, body.lecture);
const updatedWishlist =
await this.wishlistRepository.getWishlistWithLectures(wishlist.id);
if (!updatedWishlist) throw new Error('Wishlist not found');
return updatedWishlist;
}
}
3 changes: 3 additions & 0 deletions src/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ReviewsRepository } from './repositories/review.repository';
import { SemesterRepository } from './repositories/semester.repository';
import { TimetableRepository } from './repositories/timetable.repository';
import { UserRepository } from './repositories/user.repository';
import { WishlistRepository } from './repositories/wishlist.repository';

@Module({
providers: [
Expand All @@ -18,6 +19,7 @@ import { UserRepository } from './repositories/user.repository';
CourseRepository,
SemesterRepository,
TimetableRepository,
WishlistRepository,
],
exports: [
PrismaService,
Expand All @@ -28,6 +30,7 @@ import { UserRepository } from './repositories/user.repository';
CourseRepository,
SemesterRepository,
TimetableRepository,
WishlistRepository,
],
})
export class PrismaModule {}
26 changes: 16 additions & 10 deletions src/prisma/repositories/course.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,28 @@ export class CourseRepository {
return null;
}

const filter = [];
const filters = [];
const includeHumanity = group.includes('Humanity');

if ('Basic' in group) {
filter.push('Basic Required', 'Basic Elective');
if (group.includes('Basic')) {
group.splice(group.indexOf('Basic'), 1);
filters.push({ type_en: { in: ['Basic Required', 'Basic Elective'] } });
}
if ('Humanity' in group) {
filter.push('Humanities & Social Elective');
if (group.includes('Humanity')) {
group.splice(group.indexOf('Humanity'), 1);
filters.push({ type_en: { startsWith: 'Humanities & Social Elective' } });
}
if (group.length > 2) {
filter.push('Major Required', 'Major Elective', 'Elective(Graduate)');
if (group.length) {
filters.push({
type_en: {
in: ['Major Required', 'Major Elective', 'Elective(Graduate)'],
},
subject_department: { code: { in: group } },
});
}

return {
type_en: {
in: { filter },
},
OR: filters,
};
}

Expand Down
Loading
Loading