-
Notifications
You must be signed in to change notification settings - Fork 0
[Backend] Create Course Module #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: backend
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { CoursesController } from './courses.controller'; | ||
|
|
||
| describe('CoursesController', () => { | ||
| let controller: CoursesController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [CoursesController], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<CoursesController>(CoursesController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Controller, Get, Post, Delete } from '@nestjs/common'; | ||
| import { Body, Param, Patch } from '@nestjs/common/decorators'; | ||
| import { CreateCourseDto } from 'src/courses/dtos/create-courseDto.dto'; | ||
| import { UpdateCourseDto } from 'src/courses/dtos/update-courseDto'; | ||
|
|
||
| import { CoursesService } from 'src/courses/services/courses/courses.service'; | ||
|
|
||
| @Controller('courses') | ||
| export class CoursesController { | ||
| constructor(private readonly courseService: CoursesService) {} | ||
| @Get() | ||
| getCourses() { | ||
| return this.courseService.getCourses(); | ||
| } | ||
|
|
||
| @Post() | ||
| createCourse(@Body() courseDto: CreateCourseDto) { | ||
| return this.courseService.createCourse(courseDto); | ||
| } | ||
| @Patch(':id') | ||
| updateCourse( | ||
| @Param('id') id: string, | ||
| @Body() updateCourseDto: UpdateCourseDto, | ||
| ) { | ||
| return this.courseService.updateCourse(id, updateCourseDto); | ||
| } | ||
| @Delete(':id') | ||
| deleteCourse(@Param('id') id: string) { | ||
| return this.courseService.deleteCourse(id); | ||
| } | ||
| @Get(':id') | ||
| getUserById(@Param('id') id: string) { | ||
| return this.courseService.getUserById(id); | ||
| } | ||
| } |
| 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 { CoursesController } from './controllers/courses/courses.controller'; | ||
| import { Course, CourseSchema } from './schemas/course.schema'; | ||
| import { CoursesService } from './services/courses/courses.service'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| MongooseModule.forFeature([{ name: Course.name, schema: CourseSchema }]), | ||
| ], | ||
| controllers: [CoursesController], | ||
| providers: [CoursesService], | ||
| }) | ||
| export class CoursesModule {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export class CreateCourseDto { | ||
| name: string; | ||
| code: string; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { CreateCourseDto } from './create-courseDto.dto'; | ||
| import { OmitType } from '@nestjs/swagger'; | ||
|
|
||
| export class UpdateCourseDto extends OmitType(CreateCourseDto, [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkout the documentation for omitType |
||
| 'name', | ||
| 'code', | ||
| ] as const) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { Document } from 'mongoose'; | ||
|
|
||
| export interface ICourse extends Document { | ||
| name: string; | ||
| code: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
|
|
||
| @Schema({ timestamps: true }) | ||
| export class Course { | ||
| @Prop({ required: true }) | ||
| name: string; | ||
| @Prop({ required: true }) | ||
| code: string; | ||
| } | ||
|
|
||
| export const CourseSchema = SchemaFactory.createForClass(Course); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { CoursesService } from './courses.service'; | ||
|
|
||
| describe('CoursesService', () => { | ||
| let service: CoursesService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [CoursesService], | ||
| }).compile(); | ||
|
|
||
| service = module.get<CoursesService>(CoursesService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { BadRequestException } from '@nestjs/common/exceptions'; | ||
| import { InjectModel } from '@nestjs/mongoose'; | ||
| import { Model } from 'mongoose'; | ||
| import { CreateCourseDto } from 'src/courses/dtos/create-courseDto.dto'; | ||
| import { UpdateCourseDto } from 'src/courses/dtos/update-courseDto'; | ||
| import { ICourse } from 'src/courses/interfaces/course.interface'; | ||
| import { Course } from 'src/courses/schemas/course.schema'; | ||
|
|
||
| @Injectable() | ||
| export class CoursesService { | ||
| constructor(@InjectModel(Course.name) private courseModel: Model<ICourse>) {} | ||
|
|
||
| async getCourses(): Promise<ICourse[]> { | ||
| let courses: ICourse[]; | ||
| try { | ||
| courses = await this.courseModel.find(); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
| if (!courses) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should compare length |
||
| throw new NotFoundException('Courses Not Found'); | ||
| } | ||
| return courses; | ||
| } | ||
| async createCourse(courseDto: CreateCourseDto): Promise<ICourse> { | ||
| const course = new this.courseModel(courseDto); | ||
| let savedCourse: ICourse; | ||
| try { | ||
| savedCourse = await course.save(); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
|
|
||
| if (!savedCourse) { | ||
| throw new BadRequestException("Course can't be created"); | ||
| } | ||
| return savedCourse; | ||
| } | ||
|
|
||
| async updateCourse( | ||
| id: string, | ||
| updateCourseDto: UpdateCourseDto, | ||
| ): Promise<ICourse> { | ||
| let updatedcourse: ICourse; | ||
| try { | ||
| updatedcourse = await this.courseModel.findByIdAndUpdate( | ||
| { id }, | ||
| { updateCourseDto }, | ||
| { new: true }, | ||
| ); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
| if (!updatedcourse) { | ||
| throw new NotFoundException('course not found'); | ||
| } | ||
| return updatedcourse; | ||
| } | ||
| async deleteCourse(id: string): Promise<ICourse> { | ||
| let deletedCourse: ICourse; | ||
| try { | ||
| deletedCourse = await this.courseModel.findByIdAndDelete(id); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
| if (!deletedCourse) { | ||
| throw new NotFoundException('Course Not Found'); | ||
| } | ||
| return deletedCourse; | ||
| } | ||
| async getUserById(id: string): Promise<ICourse> { | ||
| let course: ICourse; | ||
| try { | ||
| course = await this.courseModel.findById(id); | ||
| } catch (err) { | ||
| console.log(err); | ||
| throw err.reason; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling not consistent |
||
| } | ||
| if (!course) { | ||
| throw new NotFoundException('Course Not Found'); | ||
| } | ||
| return course; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate dto