Skip to content

Commit

Permalink
Merge pull request #38 from gsainfoteam/37-dh
Browse files Browse the repository at this point in the history
✨ feat: 수강신청 속도 관련 db 테이블 추가
  • Loading branch information
controlZ authored Aug 10, 2024
2 parents cd925ba + 71a03ef commit af0db56
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 19 deletions.
17 changes: 13 additions & 4 deletions prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Table lecture_section_professor {
sectionId Int [not null]
lectureId Int [not null]
professorId Int [not null]
year Int [not null]
semester Semester [not null]
LectureSection lecture_section [not null]
Professor professor [not null]

Expand All @@ -51,20 +53,27 @@ Table lecture {
Table lecture_section {
id Int [not null]
lectureId Int [not null]
year Int [not null]
semester Semester [not null]
capacity Int [not null]
registrationCount Int [not null]
fullCapacityTime Int [not null]
Lecture lecture [not null]
LectureSectionProfessor lecture_section_professor [not null]
Record record [not null]
BookMark bookmark [not null]

indexes {
(id, lectureId) [pk]
(id, lectureId, year, semester) [pk]
}
}

Table bookmark {
lectureId Int [not null]
sectionId Int [not null]
userUuid String [not null]
year Int [not null]
semester Semester [not null]
LectureSection lecture_section [not null]
User user [not null]

Expand Down Expand Up @@ -118,21 +127,21 @@ Enum Recommendation {
MAYBE
}

Ref: lecture_section_professor.(sectionId, lectureId) > lecture_section.(id, lectureId)
Ref: lecture_section_professor.(sectionId, lectureId, year, semester) > lecture_section.(id, lectureId, year, semester)

Ref: lecture_section_professor.professorId > professor.id

Ref: lecture_code.lectureId > lecture.id

Ref: lecture_section.lectureId > lecture.id

Ref: bookmark.(lectureId, sectionId) > lecture_section.(id, lectureId)
Ref: bookmark.(lectureId, sectionId, year, semester) > lecture_section.(lectureId, id, year, semester)

Ref: bookmark.userUuid > user.uuid

Ref: record.userUuid > user.uuid

Ref: record.(lectureId, sectionId) > lecture_section.(lectureId, id)
Ref: record.(lectureId, sectionId, year, semester) > lecture_section.(lectureId, id, year, semester)

Ref: record_like.userUuid > user.uuid

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Warnings:
- The primary key for the `lecture_section` table will be changed. If it partially fails, the table could be left without primary key constraint.
- Added the required column `semester` to the `bookmark` table without a default value. This is not possible if the table is not empty.
- Added the required column `year` to the `bookmark` table without a default value. This is not possible if the table is not empty.
- Added the required column `capacity` to the `lecture_section` table without a default value. This is not possible if the table is not empty.
- Added the required column `full_capacity_time` to the `lecture_section` table without a default value. This is not possible if the table is not empty.
- Added the required column `registration_count` to the `lecture_section` table without a default value. This is not possible if the table is not empty.
- Added the required column `semester` to the `lecture_section` table without a default value. This is not possible if the table is not empty.
- Added the required column `year` to the `lecture_section` table without a default value. This is not possible if the table is not empty.
- Added the required column `semester` to the `lecture_section_professor` table without a default value. This is not possible if the table is not empty.
- Added the required column `year` to the `lecture_section_professor` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "bookmark" DROP CONSTRAINT "bookmark_lecture_id_section_id_fkey";

-- DropForeignKey
ALTER TABLE "lecture_section_professor" DROP CONSTRAINT "lecture_section_professor_section_id_lecture_id_fkey";

-- DropForeignKey
ALTER TABLE "record" DROP CONSTRAINT "record_lecture_id_section_id_fkey";

-- AlterTable
ALTER TABLE "bookmark" ADD COLUMN "semester" "Semester" NOT NULL,
ADD COLUMN "year" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "lecture_section" DROP CONSTRAINT "lecture_section_pkey",
ADD COLUMN "capacity" INTEGER NOT NULL,
ADD COLUMN "full_capacity_time" INTEGER NOT NULL,
ADD COLUMN "registration_count" INTEGER NOT NULL,
ADD COLUMN "semester" "Semester" NOT NULL,
ADD COLUMN "year" INTEGER NOT NULL,
ADD CONSTRAINT "lecture_section_pkey" PRIMARY KEY ("id", "lecture_id", "year", "semester");

-- AlterTable
ALTER TABLE "lecture_section_professor" ADD COLUMN "semester" "Semester" NOT NULL,
ADD COLUMN "year" INTEGER NOT NULL;

-- AddForeignKey
ALTER TABLE "lecture_section_professor" ADD CONSTRAINT "lecture_section_professor_section_id_lecture_id_year_semes_fkey" FOREIGN KEY ("section_id", "lecture_id", "year", "semester") REFERENCES "lecture_section"("id", "lecture_id", "year", "semester") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bookmark" ADD CONSTRAINT "bookmark_lecture_id_section_id_year_semester_fkey" FOREIGN KEY ("lecture_id", "section_id", "year", "semester") REFERENCES "lecture_section"("lecture_id", "id", "year", "semester") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "record" ADD CONSTRAINT "record_lecture_id_section_id_year_semester_fkey" FOREIGN KEY ("lecture_id", "section_id", "year", "semester") REFERENCES "lecture_section"("lecture_id", "id", "year", "semester") ON DELETE RESTRICT ON UPDATE CASCADE;
32 changes: 21 additions & 11 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ model User {
Record Record[]
RecordLike RecordLike[]
BookMark BookMark[]
BookMark BookMark[]
@@map("user")
}
Expand All @@ -54,7 +54,9 @@ model LectureSectionProfessor {
sectionId Int @map("section_id")
lectureId Int @map("lecture_id")
professorId Int @map("professor_id")
LectureSection LectureSection @relation(fields: [sectionId, lectureId], references: [id, lectureId])
year Int
semester Semester
LectureSection LectureSection @relation(fields: [sectionId, lectureId, year, semester], references: [id, lectureId, year, semester])
Professor Professor @relation(fields: [professorId], references: [id])
@@id([sectionId, lectureId, professorId])
Expand Down Expand Up @@ -82,24 +84,32 @@ model Lecture {

model LectureSection {
id Int
lectureId Int @map("lecture_id")
lectureId Int @map("lecture_id")
year Int
semester Semester
capacity Int
registrationCount Int @map("registration_count")
fullCapacityTime Int @map("full_capacity_time")
Lecture Lecture @relation(fields: [lectureId], references: [id])
LectureSectionProfessor LectureSectionProfessor[]
Record Record[]
BookMark BookMark[]
BookMark BookMark[]
@@id(name: "lectureSectionId", [id, lectureId])
@@id(name: "lectureSectionId", [id, lectureId, year, semester])
@@map("lecture_section")
}

model BookMark {
lectureId Int @map("lecture_id")
sectionId Int @map("section_id")
userUuid String @db.Uuid @map("user_uuid")
lectureId Int @map("lecture_id")
sectionId Int @map("section_id")
userUuid String @map("user_uuid") @db.Uuid
year Int
semester Semester
LectureSection LectureSection @relation(fields: [lectureId,sectionId], references: [id,lectureId])
User User @relation(fields: [userUuid], references: [uuid])
LectureSection LectureSection @relation(fields: [lectureId, sectionId, year, semester], references: [lectureId, id, year, semester])
User User @relation(fields: [userUuid], references: [uuid])
@@id([lectureId, sectionId, userUuid])
@@map("bookmark")
Expand Down Expand Up @@ -128,7 +138,7 @@ model Record {
userUuid String @map("user_uuid") @db.Uuid
User User @relation(fields: [userUuid], references: [uuid])
LectureSection LectureSection @relation(fields: [lectureId, sectionId], references: [lectureId, id])
LectureSection LectureSection @relation(fields: [lectureId, sectionId, year, semester], references: [lectureId, id, year, semester])
RecordLike RecordLike[]
Expand Down
26 changes: 25 additions & 1 deletion src/lecture/dto/req/bookmarkReq.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsNumber } from 'class-validator';
import { IsEnum, IsNumber } from 'class-validator';
import { Semester } from '@prisma/client';

export class BookMarkQueryDto {
@ApiProperty({
Expand All @@ -18,6 +19,29 @@ export class BookMarkQueryDto {
@IsNumber()
lectureId: number;

@ApiProperty({
example: 2024,
description: '수강 년도',
required: true,
})
@Transform(({ value }) => {
try {
return parseInt(value);
} catch {
return value;
}
})
@IsNumber()
year: number;

@ApiProperty({
example: 'FALL',
description: '수강 학기',
required: true,
})
@IsEnum(Semester)
semester: Semester;

@ApiProperty({
example: 1,
description: '분반 id',
Expand Down
38 changes: 37 additions & 1 deletion src/lecture/dto/res/lectureRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Prisma, Professor, LectureCode, LectureSection } from '@prisma/client';
import {
Prisma,
Professor,
LectureCode,
LectureSection,
Semester,
} from '@prisma/client';

class ProfessorResDto implements Professor {
@ApiProperty({
Expand Down Expand Up @@ -42,6 +48,36 @@ class LectureSectionResDto implements LectureSection {
})
lectureId: number;

@ApiProperty({
example: 2024,
description: '강의 년도',
})
year: number;

@ApiProperty({
example: 'FALL',
description: '강의 학기',
})
semester: Semester;

@ApiProperty({
example: 1,
description: '수강 정원',
})
capacity: number;

@ApiProperty({
example: 1,
description: '수강 신청 인원수',
})
registrationCount: number;

@ApiProperty({
example: 1,
description: '정원 다차는데 걸리는 시간',
})
fullCapacityTime: number;

@ApiProperty({
description: '교수 정보',
type: [ProfessorResDto],
Expand Down
5 changes: 5 additions & 0 deletions src/lecture/lecture.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export class LectureMapper {
const LectureSection = expandedLecture.LectureSection.map((section) => ({
id: section.id,
lectureId: section.lectureId,
year: section.year,
semester: section.semester,
capacity: section.capacity,
registrationCount: section.registrationCount,
fullCapacityTime: section.fullCapacityTime,
Professor: section.LectureSectionProfessor.map((professor) => ({
id: professor.Professor.id,
name: professor.Professor.name,
Expand Down
39 changes: 38 additions & 1 deletion src/lecture/lecture.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ForbiddenException,
Injectable,
InternalServerErrorException,
NotFoundException,
Expand Down Expand Up @@ -170,14 +171,16 @@ export class LectureRepository {
}

async addBookMark(
{ lectureId, sectionId }: BookMarkQueryDto,
{ lectureId, sectionId, year, semester }: BookMarkQueryDto,
userUuid: string,
): Promise<BookMark> {
return this.prismaService.bookMark
.create({
data: {
lectureId,
sectionId,
year,
semester,
userUuid,
},
})
Expand All @@ -187,6 +190,13 @@ export class LectureRepository {
throw new InternalServerErrorException(
'Unexpected Database Error Occurred',
);
if (err.code === 'P2003')
throw new ForbiddenException(
'invalid fKey. check lectureId, sectionId, year, semester value',
);
throw new InternalServerErrorException(
'Unexpected Database Error Occurred',
);
}
throw new InternalServerErrorException('Unexpected Error Occurred');
});
Expand Down Expand Up @@ -225,6 +235,33 @@ export class LectureRepository {
where: {
userUuid,
},
include: { LectureSection: true },
})
.then((bookMarks) => {
const fullCapacityItems = bookMarks.filter(
(item) =>
item.LectureSection.capacity ===
item.LectureSection.registrationCount,
);

const partialCapacityItems = bookMarks.filter(
(item) =>
item.LectureSection.capacity !==
item.LectureSection.registrationCount,
);

fullCapacityItems.sort(
(a, b) =>
a.LectureSection.fullCapacityTime -
b.LectureSection.fullCapacityTime,
);
partialCapacityItems.sort(
(a, b) =>
a.LectureSection.fullCapacityTime -
b.LectureSection.fullCapacityTime,
);

return [...fullCapacityItems, ...partialCapacityItems];
})
.catch((err) => {
if (err instanceof PrismaClientKnownRequestError) {
Expand Down
17 changes: 16 additions & 1 deletion src/record/dto/res/expandedRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { RecordResDto } from './recordRes.dto';
import { Lecture, LectureSection, Professor } from '@prisma/client';
import { Lecture, LectureSection, Professor, Semester } from '@prisma/client';

class LectureResDto implements Lecture {
@ApiProperty()
Expand All @@ -25,6 +25,21 @@ class LectureSectionResDto implements LectureSection {
@ApiProperty()
lectureId: number;

@ApiProperty()
year: number;

@ApiProperty()
semester: Semester;

@ApiProperty()
capacity: number;

@ApiProperty()
registrationCount: number;

@ApiProperty()
fullCapacityTime: number;

@ApiProperty({
type: [ProfessorResDto],
})
Expand Down
5 changes: 5 additions & 0 deletions src/record/record.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export class RecordMapper {
const LectureSection = {
id: expandedRecordType.LectureSection.id,
lectureId: expandedRecordType.LectureSection.lectureId,
year: expandedRecordType.LectureSection.year,
semester: expandedRecordType.LectureSection.semester,
capacity: expandedRecordType.LectureSection.capacity,
registrationCount: expandedRecordType.LectureSection.registrationCount,
fullCapacityTime: expandedRecordType.LectureSection.fullCapacityTime,
Lecture: {
id: expandedRecordType.LectureSection.Lecture.id,
name: expandedRecordType.LectureSection.Lecture.name,
Expand Down

0 comments on commit af0db56

Please sign in to comment.