From 4bad0b90f8e9d2e1780a8967bbc2bc2a0b289349 Mon Sep 17 00:00:00 2001 From: Devesh Kumar <59202075+Devesh21700Kumar@users.noreply.github.com> Date: Thu, 8 Apr 2021 08:00:13 +0530 Subject: [PATCH] added Assignment get and add methods (#45) Co-authored-by: Abhishek Kumar --- src/app.module.ts | 2 + src/assignment/assignment.controller.ts | 100 ++++++++++++++++++ src/assignment/assignment.module.ts | 15 +++ src/assignment/assignment.service.ts | 65 ++++++++++++ src/assignment/dto/create-assignment.dto.ts | 14 +++ .../interfaces/assignment.interface.ts | 13 +++ src/schemas/assignment.schema.ts | 24 +++++ 7 files changed, 233 insertions(+) create mode 100644 src/assignment/assignment.controller.ts create mode 100644 src/assignment/assignment.module.ts create mode 100644 src/assignment/assignment.service.ts create mode 100644 src/assignment/dto/create-assignment.dto.ts create mode 100644 src/assignment/interfaces/assignment.interface.ts create mode 100644 src/schemas/assignment.schema.ts diff --git a/src/app.module.ts b/src/app.module.ts index 02fe91b..24b5358 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { AuthModule } from './auth/auth.module'; import { MongooseModule } from '@nestjs/mongoose'; import { FirebaseModule } from './firebase/firebase.module'; import { UserModule } from './user/user.module'; +import { AssignmentModule } from './assignment/assignment.module'; import { ChatModule } from './chat/chat.module'; import * as path from 'path'; import { CourseModule } from './modules/course/course.module'; @@ -17,6 +18,7 @@ import { CourseModule } from './modules/course/course.module'; ), FirebaseModule, AuthModule, + AssignmentModule, MongooseModule.forRoot('mongodb://localhost/nest'), UserModule, CourseModule, diff --git a/src/assignment/assignment.controller.ts b/src/assignment/assignment.controller.ts new file mode 100644 index 0000000..9d5a1d4 --- /dev/null +++ b/src/assignment/assignment.controller.ts @@ -0,0 +1,100 @@ +import { + Controller, + Get, + Res, + HttpStatus, + Post, + Body, + Put, + Query, + NotFoundException, + Delete, + Param, + UsePipes, + ValidationPipe, +} from '@nestjs/common'; +import { AssignmentService } from './assignment.service'; //eslint-disable-line +import { CreateAssignmentDTO } from './dto/create-assignment.dto'; //eslint-disable-line +import { ApiCreatedResponse, ApiProperty } from '@nestjs/swagger'; + +class AssignmentResponseBody { + @ApiProperty({ required: true, example: '605e3fd9acc33583fb389aec' }) + id: string; + + @ApiProperty({ required: true, example: 'Noob' }) + name: string; + + @ApiProperty({ required: true, example: 'Coder' }) + link: string; + + @ApiProperty({ required: true, example: 'noobcoder@gmai.com' }) + submit_by: string; +} + +@Controller('Assignment') +export class AssignmentController { + constructor(private AssignmentService: AssignmentService) {} + + // add a Assignment + @Post() + @UsePipes(ValidationPipe) + async addAssignment( + @Res() res, + @Body() CreateAssignmentDTO: CreateAssignmentDTO, + ) { + const Assignment = await this.AssignmentService.addAssignment( + CreateAssignmentDTO, + ); + return res.status(HttpStatus.OK).json({ + message: 'Assignment has been created successfully', + Assignment, + }); + } + + // Retrieve Assignments list + @ApiCreatedResponse({ type: [AssignmentResponseBody] }) + @Get() + async getAllAssignment(@Res() res) { + const Assignments = await this.AssignmentService.getAllAssignment(); + return res.status(HttpStatus.OK).json(Assignments); + } + + // Fetch a particular Assignment using ID + @ApiCreatedResponse({ type: AssignmentResponseBody }) + @Get('/:AssignmentId') + async getAssignment(@Res() res, @Param('AssignmentId') AssignmentId: string) { + const Assignment = await this.AssignmentService.getAssignment(AssignmentId); + return res.status(HttpStatus.OK).json(Assignment); + } + + @Put('/update') + async updateAssignment( + @Res() res, + @Query('uid') uid, + @Body() createAssignmentDTO: CreateAssignmentDTO, + ) { + console.log('AssignmentId', uid); + const Assignment = await this.AssignmentService.updateAssignment( + uid, + createAssignmentDTO, + ); + + if (!Assignment) throw new NotFoundException('Assignment does not exist!'); + + return res.status(HttpStatus.OK).json({ + message: 'Assignment has been successfully updated', + Assignment: Assignment, + }); + } + + // Delete a Assignment + @Delete('/delete') + async deleteAssignment(@Res() res, @Query('uid') uid) { + const Assignment = await this.AssignmentService.deleteAssignment(uid); + if (!Assignment) throw new NotFoundException('Assignment does not exist'); + return res.status(HttpStatus.OK).json({ + message: 'Assignment has been deleted', + Assignment: Assignment, + }); + } +} diff --git a/src/assignment/assignment.module.ts b/src/assignment/assignment.module.ts new file mode 100644 index 0000000..9a2dd37 --- /dev/null +++ b/src/assignment/assignment.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { AssignmentController } from './assignment.controller'; +import { AssignmentService } from './assignment.service'; +import { MongooseModule } from '@nestjs/mongoose'; +import { AssignmentSchema } from '../schemas/assignment.schema'; +@Module({ + imports: [ + MongooseModule.forFeature([ + { name: 'Assignment', schema: AssignmentSchema }, + ]), + ], + controllers: [AssignmentController], + providers: [AssignmentService], +}) +export class AssignmentModule {} diff --git a/src/assignment/assignment.service.ts b/src/assignment/assignment.service.ts new file mode 100644 index 0000000..a7b4cda --- /dev/null +++ b/src/assignment/assignment.service.ts @@ -0,0 +1,65 @@ +import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; +import { Assignment } from './interfaces/assignment.interface'; +import { CreateAssignmentDTO } from './dto/create-assignment.dto'; + +@Injectable() +export class AssignmentService { + constructor( + @InjectModel('Assignment') + private readonly AssignmentModel: Model, + ) {} + + // fetch all Assignments + async getAllAssignment(): Promise { + const Assignments = await this.AssignmentModel.find().exec(); + return Assignments; + } + + // Get a single Assignment + async getAssignment(AssignmentId): Promise { + const Assignment = await this.AssignmentModel.findById(AssignmentId).exec(); + + if (Assignment) { + return Assignment; + } + + throw new HttpException( + { + status: HttpStatus.NOT_FOUND, + error: 'Assignment Not Found', + }, + HttpStatus.NOT_FOUND, + ); + } + + // post a single Assignment + async addAssignment( + CreateAssignmentDTO: CreateAssignmentDTO, + ): Promise { + const newAssignment = await new this.AssignmentModel(CreateAssignmentDTO); + return newAssignment.save(); + } + + // Edit Assignment details + async updateAssignment( + AssignmentID, + CreateAssignmentDTO: CreateAssignmentDTO, + ): Promise { + const updatedAssignment = await this.AssignmentModel.findByIdAndUpdate( + AssignmentID, + CreateAssignmentDTO, + { new: true }, + ); + return updatedAssignment; + } + + // Delete a Assignment + async deleteAssignment(AssignmentID): Promise { + const deletedAssignment = await this.AssignmentModel.findByIdAndRemove( + AssignmentID, + ); + return deletedAssignment; + } +} diff --git a/src/assignment/dto/create-assignment.dto.ts b/src/assignment/dto/create-assignment.dto.ts new file mode 100644 index 0000000..7e87544 --- /dev/null +++ b/src/assignment/dto/create-assignment.dto.ts @@ -0,0 +1,14 @@ +import { IsNotEmpty } from 'class-validator'; + +export class CreateAssignmentDTO { + @IsNotEmpty() + readonly name: string; + + @IsNotEmpty() + readonly link: string; + + readonly submit_by: number; + + @IsNotEmpty() + readonly created_at: Date; +} diff --git a/src/assignment/interfaces/assignment.interface.ts b/src/assignment/interfaces/assignment.interface.ts new file mode 100644 index 0000000..3cf34ce --- /dev/null +++ b/src/assignment/interfaces/assignment.interface.ts @@ -0,0 +1,13 @@ +import { Document } from 'mongoose'; + +export interface Assignment extends Document { + readonly first_name: string; + readonly last_name: string; + readonly email: string; + readonly phone: string; + readonly address: string; + readonly description: string; + readonly score: number; + readonly isAdmin: boolean; + readonly created_at: Date; +} diff --git a/src/schemas/assignment.schema.ts b/src/schemas/assignment.schema.ts new file mode 100644 index 0000000..f20a206 --- /dev/null +++ b/src/schemas/assignment.schema.ts @@ -0,0 +1,24 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + +export type AssignmentDocument = Assignment & Document; + +@Schema() +export class Assignment { + @Prop() + id: string; + + @Prop() + name: string; + + @Prop() + link: string; + + @Prop() + submit_by: number; + + @Prop() + created_at: Date; +} + +export const AssignmentSchema = SchemaFactory.createForClass(Assignment);