diff --git a/package-lock.json b/package-lock.json index 18e72a8..ca7f929 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8125,4 +8125,4 @@ "dev": true } } -} +} \ No newline at end of file diff --git a/src/assignment/assignment.controller.ts b/src/assignment/assignment.controller.ts index 923ec5e..16a4412 100644 --- a/src/assignment/assignment.controller.ts +++ b/src/assignment/assignment.controller.ts @@ -1,15 +1,19 @@ -import {Body, Controller, Post} from '@nestjs/common'; +import {Body, Controller, Post, Req, UseGuards} from '@nestjs/common'; import {CreateLectureDto} from "../lecture/dto/create-lecture.dto"; import {LectureService} from "../lecture/lecture.service"; import {AssignmentService} from "./assignment.service"; +import { CreateAssignmentDto } from './dto/create-assignment.dto'; +import { AuthGuard } from '@nestjs/passport'; @Controller('assignment') +@UseGuards(AuthGuard()) export class AssignmentController { - constructor(private readonly assignmentService: AssignmentService) {} - // @Post('/add') - // createLectureAssignment(@Body() assignmentData: CreateassignmentDto): Promise { - // return this.assignmentService.createProfLecture(lectureData); - // } -} + //과제 평가 + @Post('add') + @UseGuards(AuthGuard()) + createLectureAssignment(@Req() req, @Body() createLectureAssignment: CreateAssignmentDto): Promise { + return this.assignmentService.createAssignment(createLectureAssignment, req.user.id); + } + } diff --git a/src/assignment/assignment.module.ts b/src/assignment/assignment.module.ts index 132bbcc..e37ba6b 100644 --- a/src/assignment/assignment.module.ts +++ b/src/assignment/assignment.module.ts @@ -3,9 +3,14 @@ import { AssignmentService } from './assignment.service'; import { AssignmentController } from './assignment.controller'; import {TypeOrmModule} from "@nestjs/typeorm"; import {Assignment} from "./entity/assignment.entity"; +import { Semester } from 'src/semester/entity/semester.entity'; +import { Year } from 'src/year/entity/year.entity'; +import { User } from 'src/user/entity/user.entity'; +import { Lecture } from 'src/lecture/entity/lecture.entity'; + @Module({ - imports: [TypeOrmModule.forFeature([Assignment])], + imports: [TypeOrmModule.forFeature([Assignment, Lecture, Semester, Year, User])], providers: [AssignmentService], controllers: [AssignmentController] }) diff --git a/src/assignment/assignment.service.ts b/src/assignment/assignment.service.ts index d081fd7..a44c9fa 100644 --- a/src/assignment/assignment.service.ts +++ b/src/assignment/assignment.service.ts @@ -1,6 +1,120 @@ -import { Injectable } from '@nestjs/common'; +import { + Injectable, + ConflictException, + NotFoundException +} from '@nestjs/common'; +import { CreateAssignmentDto } from './dto/create-assignment.dto'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Assignment } from './entity/assignment.entity'; +import { Repository } from 'typeorm'; +import { Lecture } from 'src/lecture/entity/lecture.entity'; +import { Semester } from 'src/semester/entity/semester.entity'; +import { Year } from 'src/year/entity/year.entity'; +import { User } from 'src/user/entity/user.entity'; @Injectable() export class AssignmentService { + constructor( + @InjectRepository(Assignment) + private assignmentRepository: Repository, + @InjectRepository(Lecture) + private lectureRepository: Repository, + @InjectRepository(Semester) + private semesterRepository: Repository, + @InjectRepository(Year) + private yearRepository: Repository, + @InjectRepository(User) + private userRepository: Repository, + ) {} + + async getAll(): Promise { + return this.assignmentRepository.find({}); + } + + async createAssignment(createAssignmentDto: CreateAssignmentDto, id:number): Promise{ + const { + practice, + report, + project, + other, + lecture_id, + semester_id, + year + } = createAssignmentDto; + + const user_id = id; + + //강의 검색 + const found = await this.lectureRepository.findOneBy({ id: lecture_id }); + + //해당 과제 작성이력 조회 + const found_user = await this.assignmentRepository.findOne({ + relations: { + lecture: true, + user : true + }, + where: { + user : { + id: user_id + }, + lecture : { + id: lecture_id + }, + } + }); + + if (found_user) { + throw new ConflictException( + `이미 강의의 과제를 평가` + ); + } else { // conditon 이전에 (lecture id , uuid)의 꼴이 같지 않는 경우를 세야함. + if (found) { + const assignment = new Assignment(); + assignment.practice = practice; + assignment.report = report; + assignment.project = project; + assignment.other = other; + assignment.user = await this.userRepository.findOne({ + relations : { + records : true, + }, + where : { + id : user_id + } + }) + assignment.years = await this.yearRepository.findOne({ + relations : { + records : true, + }, + where : { + year : year + } + }) + assignment.semesters = await this.semesterRepository.findOne({ + relations: { + records: true, + }, + where : { + id : semester_id + } + }) + assignment.lecture = await this.lectureRepository.findOne({ + relations: { + records: true, + }, + where: { + id: lecture_id, + }, + }); + + await this.assignmentRepository.manager.save(assignment); + return "success"; + } else { + throw new NotFoundException( + `해당되는 id : ${lecture_id} 강의가 없습니다.`, + ); + } + } + } } diff --git a/src/assignment/dto/create-assignment.dto.ts b/src/assignment/dto/create-assignment.dto.ts new file mode 100644 index 0000000..f67eeeb --- /dev/null +++ b/src/assignment/dto/create-assignment.dto.ts @@ -0,0 +1,20 @@ +export class CreateAssignmentDto { + + readonly lecture_id: number; + + readonly semester_id: number; + + readonly year: string; + + readonly assignment_EV: string; + + readonly practice: boolean; + + readonly report: boolean; + + readonly project: boolean; + + readonly other: boolean; + + +} \ No newline at end of file diff --git a/src/assignment/entity/assignment.entity.ts b/src/assignment/entity/assignment.entity.ts index e44b59e..93a9ea5 100644 --- a/src/assignment/entity/assignment.entity.ts +++ b/src/assignment/entity/assignment.entity.ts @@ -1,12 +1,17 @@ -import { Lecture } from 'src/lecture/entity/lecture.entity'; import { Entity, Column, PrimaryGeneratedColumn, JoinColumn, OneToOne, + ManyToMany, + ManyToOne, + JoinTable, } from 'typeorm'; -import {Record} from "../../record/entity/record.entity"; +import { Lecture } from 'src/lecture/entity/lecture.entity'; +import { Semester } from 'src/semester/entity/semester.entity'; +import { User } from 'src/user/entity/user.entity'; +import { Year } from 'src/year/entity/year.entity'; @Entity() export class Assignment { @@ -23,11 +28,23 @@ export class Assignment { project: boolean; @Column() - others: boolean; - - - - @OneToOne(() => Record, (record ) => record.assignment) - @JoinColumn() - record : Record; + other: boolean; + + @ManyToOne((Type) => Lecture, (lecture) => lecture.lecture_name, { + cascade: true, + eager: true, + }) + lecture: Lecture; + + @ManyToOne((Type) => Semester, { cascade: true }) + @JoinTable() + semesters: Semester; + + @ManyToOne((Type) => Year, { cascade: true }) + @JoinTable() + years: Year; + + @ManyToOne((Type) => User, { cascade: true }) + @JoinTable() + user: User; } diff --git a/src/record/entity/record.entity.ts b/src/record/entity/record.entity.ts index 7f586eb..28a67be 100644 --- a/src/record/entity/record.entity.ts +++ b/src/record/entity/record.entity.ts @@ -13,8 +13,6 @@ import { ManyToOne, JoinTable, OneToOne, } from 'typeorm'; -import {Scoring} from "../../scoring/entity/scoring.entity"; -import {Assignment} from "../../assignment/entity/assignment.entity"; @Entity() export class Record { @@ -62,7 +60,4 @@ export class Record { @ManyToOne((Type) => User, { cascade: true }) @JoinTable() user: User; - - @OneToOne(() => Assignment, (assignment) => assignment.record) - assignment : Assignment; }