diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index a7c579e..6bc3663 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -5,7 +5,6 @@ import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; import * as dotenv from 'dotenv'; import { ConfigModule } from '@nestjs/config'; -import { RoomModule } from './room/room.module'; import { UserModule } from './user/user.module'; import { LectureModule } from './lecture/lecture.module'; @@ -18,7 +17,6 @@ dotenv.config(); isGlobal: true }), AuthModule, - RoomModule, UserModule, LectureModule ], diff --git a/backend/src/auth/auth.module.ts b/backend/src/auth/auth.module.ts index 2f4c47d..4dc1a91 100644 --- a/backend/src/auth/auth.module.ts +++ b/backend/src/auth/auth.module.ts @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { JwtModule } from '@nestjs/jwt'; import { MongooseModule } from '@nestjs/mongoose'; -import { User, UserSchema } from 'src/schema/user.schema'; +import { User, UserSchema } from 'src/user/user.schema'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { GoogleStrategy } from './google.strategy'; diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index 4ebd53f..70d1ad9 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; -import { User } from 'src/schema/user.schema'; +import { User } from 'src/user/user.schema'; import { UserInfoDto } from './dto/userInfo.dto'; @Injectable() diff --git a/backend/src/room/dto/create-room.dto.ts b/backend/src/lecture/dto/create-lecture.dto.ts similarity index 64% rename from backend/src/room/dto/create-room.dto.ts rename to backend/src/lecture/dto/create-lecture.dto.ts index 2c873ed..318ad6a 100644 --- a/backend/src/room/dto/create-room.dto.ts +++ b/backend/src/lecture/dto/create-lecture.dto.ts @@ -1,5 +1,5 @@ -export class CreateRoomDto { - email: string; +export class CreateLectureDto { title: string; description: string; + email: string; } diff --git a/backend/src/lecture/dto/enter-lecture.dto.ts b/backend/src/lecture/dto/enter-lecture.dto.ts new file mode 100644 index 0000000..4cea86d --- /dev/null +++ b/backend/src/lecture/dto/enter-lecture.dto.ts @@ -0,0 +1,3 @@ +export class EnterLectureDto { + email: string; +} diff --git a/backend/src/lecture/dto/update-lecture.dto.ts b/backend/src/lecture/dto/update-lecture.dto.ts new file mode 100644 index 0000000..e6fe850 --- /dev/null +++ b/backend/src/lecture/dto/update-lecture.dto.ts @@ -0,0 +1,4 @@ +export class UpdateLectureDto { + code: string; + audio: string; +} diff --git a/backend/src/room/room-code.schema.ts b/backend/src/lecture/lecture-code.schema.ts similarity index 86% rename from backend/src/room/room-code.schema.ts rename to backend/src/lecture/lecture-code.schema.ts index 6bd76e5..5f64413 100644 --- a/backend/src/room/room-code.schema.ts +++ b/backend/src/lecture/lecture-code.schema.ts @@ -1,6 +1,6 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import mongoose from 'mongoose'; -import { Lecture } from '../lecture/lecture.schema'; +import { Lecture } from './lecture.schema'; @Schema() export class EnterCode { diff --git a/backend/src/lecture/lecture.controller.ts b/backend/src/lecture/lecture.controller.ts new file mode 100644 index 0000000..f0736a0 --- /dev/null +++ b/backend/src/lecture/lecture.controller.ts @@ -0,0 +1,39 @@ +import { Body, Controller, HttpStatus, Param, Patch, Post, Res } from '@nestjs/common'; +import { Response } from 'express'; +import { UserService } from 'src/user/user.service'; +import { CreateLectureDto } from './dto/create-lecture.dto'; +import { EnterLectureDto } from './dto/enter-lecture.dto'; +import { UpdateLectureDto } from './dto/update-lecture.dto'; +import { LectureService } from './lecture.service'; + +@Controller('lecture') +export class LectureController { + constructor( + private readonly lectureService: LectureService, + private readonly userService: UserService + ) {} + + @Post() + async create(@Body() createLecture: CreateLectureDto, @Res() res: Response) { + const user = await this.userService.findOneByEmail(createLecture.email); + const code = await this.lectureService.createLecture(createLecture, user.id); + res.status(HttpStatus.CREATED).send({ code: code }); + } + + @Patch('end') + async end(@Body() updateLectureDto: UpdateLectureDto, @Res() res: Response) { + await this.lectureService.endLecture(updateLectureDto); + res.status(HttpStatus.OK).send(); + } + + @Patch('/:code') + async enter(@Param('code') code: string, @Body() enterLectureDto: EnterLectureDto, @Res() res: Response) { + const enterCodeDocument = await this.lectureService.findLectureByCode(code); + if (!enterCodeDocument) { + res.status(HttpStatus.NOT_FOUND).send(); + return; + } + const result = await this.userService.updateLecture(enterLectureDto.email, enterCodeDocument.lecture_id); + res.status(HttpStatus.OK).send(result); + } +} diff --git a/backend/src/lecture/lecture.module.ts b/backend/src/lecture/lecture.module.ts index 63114ed..569a636 100644 --- a/backend/src/lecture/lecture.module.ts +++ b/backend/src/lecture/lecture.module.ts @@ -1,8 +1,21 @@ import { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; +import { User, UserSchema } from 'src/user/user.schema'; +import { UserService } from 'src/user/user.service'; +import { EnterCode, EnterCodeSchema } from './lecture-code.schema'; +import { LectureController } from './lecture.controller'; import { Lecture, LectureSchema } from './lecture.schema'; +import { LectureService } from './lecture.service'; @Module({ - imports: [MongooseModule.forFeature([{ name: Lecture.name, schema: LectureSchema }])] + imports: [ + MongooseModule.forFeature([ + { name: Lecture.name, schema: LectureSchema }, + { name: EnterCode.name, schema: EnterCodeSchema }, + { name: User.name, schema: UserSchema } + ]) + ], + controllers: [LectureController], + providers: [LectureService, UserService] }) export class LectureModule {} diff --git a/backend/src/lecture/lecture.schema.ts b/backend/src/lecture/lecture.schema.ts index 4fba0d5..496f83f 100644 --- a/backend/src/lecture/lecture.schema.ts +++ b/backend/src/lecture/lecture.schema.ts @@ -1,6 +1,6 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import mongoose from 'mongoose'; -import { User } from '../schema/user.schema'; +import { User } from 'src/user/user.schema'; @Schema() export class Lecture { @@ -15,6 +15,12 @@ export class Lecture { @Prop({ default: false }) is_end: boolean; + + @Prop() + audio_file: string; + + @Prop() + start_time: Date; } export const LectureSchema = SchemaFactory.createForClass(Lecture); diff --git a/backend/src/lecture/lecture.service.ts b/backend/src/lecture/lecture.service.ts new file mode 100644 index 0000000..b0b4f82 --- /dev/null +++ b/backend/src/lecture/lecture.service.ts @@ -0,0 +1,54 @@ +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { GenerateUtils } from 'src/utils/GenerateUtils'; +import { CreateLectureDto } from './dto/create-lecture.dto'; +import { UpdateLectureDto } from './dto/update-lecture.dto'; +import { EnterCode } from './lecture-code.schema'; +import { Lecture } from './lecture.schema'; + +@Injectable() +export class LectureService { + constructor( + @InjectModel(Lecture.name) + private lectureModel: Model, + @InjectModel(EnterCode.name) + private enterCodeModel: Model + ) {} + + async createLecture(createLectureDto: CreateLectureDto, userId: string) { + const lecture = new this.lectureModel({ + title: createLectureDto.title, + description: createLectureDto.description, + presenter_id: userId + }); + const lectureCode = new this.enterCodeModel({ + code: await this.generateRoomCode(), + lecture_id: lecture.id + }); + + await Promise.all([lecture.save(), lectureCode.save()]); + + return lectureCode.code; + } + + async endLecture(updateLectureDto: UpdateLectureDto) { + const lecture = await this.findLectureByCode(updateLectureDto.code); + return await this.lectureModel + .findByIdAndUpdate(lecture.lecture_id, { $set: { is_end: true, audio_file: updateLectureDto.audio } }) + .exec(); + } + + async generateRoomCode() { + const generateUtils = new GenerateUtils(); + let lectureCode = generateUtils.generateRandomNumber(); + while (await this.findLectureByCode(lectureCode)) { + lectureCode = generateUtils.generateRandomNumber(); + } + return lectureCode; + } + + async findLectureByCode(code: string) { + return await this.enterCodeModel.findOne({ code: code }); + } +} diff --git a/backend/src/room/dto/enter-room.dto.ts b/backend/src/room/dto/enter-room.dto.ts deleted file mode 100644 index dc5408a..0000000 --- a/backend/src/room/dto/enter-room.dto.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class EnterRoomDto { - email: string; -} diff --git a/backend/src/room/room.controller.ts b/backend/src/room/room.controller.ts deleted file mode 100644 index 8f8152c..0000000 --- a/backend/src/room/room.controller.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Body, Controller, HttpStatus, Param, Patch, Post, Res } from '@nestjs/common'; -import { RoomService } from './room.service'; -import { Response } from 'express'; -import { CreateRoomDto } from './dto/create-room.dto'; -import { EnterRoomDto } from './dto/enter-room.dto'; -import { UserService } from '../user/user.service'; - -@Controller('/room') -export class RoomController { - constructor( - private readonly roomService: RoomService, - private readonly userService: UserService - ) {} - - @Post() - async create(@Body() createRoomDto: CreateRoomDto, @Res() res: Response) { - const code = await this.roomService.createRoom(createRoomDto); - res.status(HttpStatus.CREATED).send({ code: code }); - } - - @Patch('/:code') - async enter(@Param('code') code: string, @Body() enterRoomDto: EnterRoomDto, @Res() res: Response) { - const enterCodeDocument = await this.roomService.findRoomByCode(code); - if (!enterCodeDocument) { - res.status(HttpStatus.NOT_FOUND).send(); - return; - } - const result = await this.userService.updateLecture(enterRoomDto.email, enterCodeDocument.lecture_id); - res.status(HttpStatus.OK).send(result); - } -} diff --git a/backend/src/room/room.module.ts b/backend/src/room/room.module.ts deleted file mode 100644 index 043015a..0000000 --- a/backend/src/room/room.module.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Module } from '@nestjs/common'; -import { RoomController } from './room.controller'; -import { RoomService } from './room.service'; -import { MongooseModule } from '@nestjs/mongoose'; -import { Room, RoomSchema } from './room.schema'; -import { EnterCode, EnterCodeSchema } from './room-code.schema'; -import { UserService } from '../user/user.service'; -import { User, UserSchema } from '../schema/user.schema'; - -@Module({ - imports: [ - MongooseModule.forFeature([ - { name: Room.name, schema: RoomSchema }, - { name: EnterCode.name, schema: EnterCodeSchema }, - { name: User.name, schema: UserSchema } - ]) - ], - controllers: [RoomController], - providers: [RoomService, UserService] -}) -export class RoomModule {} diff --git a/backend/src/room/room.schema.ts b/backend/src/room/room.schema.ts deleted file mode 100644 index aa31ec5..0000000 --- a/backend/src/room/room.schema.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; -import { HydratedDocument } from 'mongoose'; - -export type RoomDocument = HydratedDocument; - -@Schema() -export class Room { - @Prop({ required: true }) - title: string; - - @Prop({ required: true }) - description: string; - - @Prop() - presenter_id: string; -} - -export const RoomSchema = SchemaFactory.createForClass(Room); diff --git a/backend/src/room/room.service.ts b/backend/src/room/room.service.ts deleted file mode 100644 index f8ede81..0000000 --- a/backend/src/room/room.service.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { GenerateUtils } from '../utils/GenerateUtils'; -import { Injectable } from '@nestjs/common'; -import { InjectModel } from '@nestjs/mongoose'; -import { Room } from './room.schema'; -import { Model } from 'mongoose'; -import { CreateRoomDto } from './dto/create-room.dto'; -import { EnterCode } from './room-code.schema'; -import { UserService } from '../user/user.service'; - -@Injectable() -export class RoomService { - constructor( - @InjectModel(Room.name) - private roomModel: Model, - @InjectModel(EnterCode.name) - private enterCodeModel: Model, - private readonly userService: UserService - ) {} - - async createRoom(createRoomDto: CreateRoomDto) { - const user = await this.userService.findOneByEmail(createRoomDto.email); - const createdRoom = new this.roomModel({ ...createRoomDto, presenter_id: user.id }); - const createdEnterCode = new this.enterCodeModel({ - code: await this.generateRoomCode(), - lecture_id: createdRoom.id - }); - await Promise.all([createdRoom.save(), createdEnterCode.save()]); - return createdEnterCode.code; - } - - async generateRoomCode() { - const generateUtils = new GenerateUtils(); - let roomCode = generateUtils.generateRandomNumber(); - while (await this.findRoomByCode(roomCode)) { - roomCode = generateUtils.generateRandomNumber(); - } - return roomCode; - } - - async findRoomByCode(code: string) { - return await this.enterCodeModel.findOne({ code: code }); - } -} diff --git a/backend/src/user/user.module.ts b/backend/src/user/user.module.ts index c2e9f3b..515474e 100644 --- a/backend/src/user/user.module.ts +++ b/backend/src/user/user.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; -import { User, UserSchema } from '../schema/user.schema'; +import { User, UserSchema } from './user.schema'; import { UserController } from './user.controller'; import { UserService } from './user.service'; diff --git a/backend/src/schema/user.schema.ts b/backend/src/user/user.schema.ts similarity index 100% rename from backend/src/schema/user.schema.ts rename to backend/src/user/user.schema.ts diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 62d94ee..cb0894e 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; -import { User, UserDocument } from '../schema/user.schema'; +import { User, UserDocument } from './user.schema'; import { Model } from 'mongoose'; @Injectable()