-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Assignment get and add methods (#45)
Co-authored-by: Abhishek Kumar <[email protected]>
- Loading branch information
1 parent
66791b8
commit 4bad0b9
Showing
7 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: '[email protected]' }) | ||
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Assignment>, | ||
) {} | ||
|
||
// fetch all Assignments | ||
async getAllAssignment(): Promise<Assignment[]> { | ||
const Assignments = await this.AssignmentModel.find().exec(); | ||
return Assignments; | ||
} | ||
|
||
// Get a single Assignment | ||
async getAssignment(AssignmentId): Promise<Assignment> { | ||
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<Assignment> { | ||
const newAssignment = await new this.AssignmentModel(CreateAssignmentDTO); | ||
return newAssignment.save(); | ||
} | ||
|
||
// Edit Assignment details | ||
async updateAssignment( | ||
AssignmentID, | ||
CreateAssignmentDTO: CreateAssignmentDTO, | ||
): Promise<Assignment> { | ||
const updatedAssignment = await this.AssignmentModel.findByIdAndUpdate( | ||
AssignmentID, | ||
CreateAssignmentDTO, | ||
{ new: true }, | ||
); | ||
return updatedAssignment; | ||
} | ||
|
||
// Delete a Assignment | ||
async deleteAssignment(AssignmentID): Promise<any> { | ||
const deletedAssignment = await this.AssignmentModel.findByIdAndRemove( | ||
AssignmentID, | ||
); | ||
return deletedAssignment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |