Skip to content

Commit

Permalink
added Assignment get and add methods (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: Abhishek Kumar <[email protected]>
  • Loading branch information
Devesh21700Kumar and Abhishek-kumar09 authored Apr 8, 2021
1 parent 66791b8 commit 4bad0b9
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,6 +18,7 @@ import { CourseModule } from './modules/course/course.module';
),
FirebaseModule,
AuthModule,
AssignmentModule,
MongooseModule.forRoot('mongodb://localhost/nest'),
UserModule,
CourseModule,
Expand Down
100 changes: 100 additions & 0 deletions src/assignment/assignment.controller.ts
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,
});
}
}
15 changes: 15 additions & 0 deletions src/assignment/assignment.module.ts
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 {}
65 changes: 65 additions & 0 deletions src/assignment/assignment.service.ts
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;
}
}
14 changes: 14 additions & 0 deletions src/assignment/dto/create-assignment.dto.ts
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;
}
13 changes: 13 additions & 0 deletions src/assignment/interfaces/assignment.interface.ts
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;
}
24 changes: 24 additions & 0 deletions src/schemas/assignment.schema.ts
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);

0 comments on commit 4bad0b9

Please sign in to comment.