Skip to content

Commit

Permalink
Added Course schema, controller, module and service (#35)
Browse files Browse the repository at this point in the history
* added Course schema, controller, module and service

* fixed lint error

* minor modifications

* corrections in service and controller
  • Loading branch information
ritvij14 authored Apr 5, 2021
1 parent 204f24b commit 8b206a7
Show file tree
Hide file tree
Showing 9 changed files with 221 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 @@ -7,6 +7,7 @@ import { MongooseModule } from '@nestjs/mongoose';
import { FirebaseModule } from './firebase/firebase.module';
import { UserModule } from './user/user.module';
import * as path from 'path';
import { CourseModule } from './modules/course/course.module';

@Module({
imports: [
Expand All @@ -17,6 +18,7 @@ import * as path from 'path';
AuthModule,
MongooseModule.forRoot('mongodb://localhost/nest'),
UserModule,
CourseModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
18 changes: 18 additions & 0 deletions src/modules/course/course.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CourseController } from './course.controller';

describe('CourseController', () => {
let controller: CourseController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CourseController],
}).compile();

controller = module.get<CourseController>(CourseController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
64 changes: 64 additions & 0 deletions src/modules/course/course.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
Body,
Controller,
Get,
HttpStatus,
NotFoundException,
Param,
Post,
Put,
Query,
Res,
} from '@nestjs/common';
import { Response } from 'express';
import { CourseService } from './course.service';
import { CourseDTO } from './create-course.dto';

@Controller('course')
export class CourseController {
constructor(private courseService: CourseService) {}

// get all courses
@Get('/all')
async getAllCourses(@Res() res: Response) {
const courses = await this.courseService.getAllCourses();
return res.status(HttpStatus.OK).json(courses);
}

// get selected course
@Get('/:CourseID')
async getSelectedCourse(@Res() res: Response, @Param('CourseID') CourseID) {
const course = await this.courseService.getSelectedCourse(CourseID);
if (!course) {
throw new NotFoundException('Selected course not found');
}
return res.status(HttpStatus.OK).json(course);
}

// add a Course
@Post('/create')
async addCourse(@Res() res: Response, @Body() courseDTO: CourseDTO) {
const course = await this.courseService.addCourse(courseDTO);
return res.status(HttpStatus.OK).json({
message: 'Course has been added successfully',
course,
});
}

// update a course
@Put('/edit')
async editCourse(
@Res() res: Response,
@Query('CourseID') CourseID,
@Body() courseDTO: CourseDTO,
) {
const Course = await this.courseService.editCourse(CourseID, courseDTO);
if (!Course) {
throw new NotFoundException('Course does not exist');
}
return res.status(HttpStatus.OK).json({
message: 'Course has been successfully edited',
Course,
});
}
}
14 changes: 14 additions & 0 deletions src/modules/course/course.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Document } from 'mongoose';

export interface Course extends Document {
readonly id: string;
readonly name: string;
readonly price: number;
readonly start_date: Date;
readonly end_date: Date;
readonly duration: string;
readonly active: boolean;
readonly coupons: number;
readonly student_num: number;
readonly video_num: number;
}
14 changes: 14 additions & 0 deletions src/modules/course/course.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CourseSchema } from '../../schemas/course.schema';
import { CourseController } from './course.controller';
import { CourseService } from './course.service';

@Module({
imports: [
MongooseModule.forFeature([{ name: 'Course', schema: CourseSchema }]),
],
controllers: [CourseController],
providers: [CourseService],
})
export class CourseModule {}
18 changes: 18 additions & 0 deletions src/modules/course/course.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CourseService } from './course.service';

describe('CourseService', () => {
let service: CourseService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CourseService],
}).compile();

service = module.get<CourseService>(CourseService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
40 changes: 40 additions & 0 deletions src/modules/course/course.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Course } from './course.interface';
import { CourseDTO } from './create-course.dto';

@Injectable()
export class CourseService {
constructor(
@InjectModel('Course') private readonly CourseModel: Model<Course>,
) {}

// fetch all courses
async getAllCourses(): Promise<Course[]> {
const Courses = await this.CourseModel.find().exec();
return Courses;
}

// fetch selected course
async getSelectedCourse(CourseID: string): Promise<Course> {
const Course = await this.CourseModel.findById(CourseID).exec();
return Course;
}

// add course
async addCourse(courseDTO: CourseDTO): Promise<Course> {
const newCourse = new this.CourseModel(courseDTO);
return newCourse.save();
}

// edit course
async editCourse(CourseId: string, courseDTO: CourseDTO): Promise<Course> {
const updatedCourse = await this.CourseModel.findByIdAndUpdate(
CourseId,
courseDTO,
{ new: true },
);
return updatedCourse;
}
}
12 changes: 12 additions & 0 deletions src/modules/course/create-course.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class CourseDTO {
readonly id: string;
readonly name: string;
readonly price: number;
readonly start_date: Date;
readonly end_date: Date;
readonly duration: string;
readonly active: boolean;
readonly coupons: number;
readonly student_num: number;
readonly video_num: number;
}
39 changes: 39 additions & 0 deletions src/schemas/course.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CourseDocument = Course & Document;

@Schema()
export class Course {
@Prop({ required: true })
id: string;

@Prop({ required: true })
name: string;

@Prop({ required: true })
price: number;

@Prop({ required: true, default: Date.now })
start_date: Date;

@Prop()
end_date: Date;

@Prop()
duration: string;

@Prop()
active: boolean;

@Prop()
coupons: number;

@Prop({ default: 0 })
student_num: number;

@Prop()
video_num: number;
}

export const CourseSchema = SchemaFactory.createForClass(Course);

0 comments on commit 8b206a7

Please sign in to comment.