Skip to content

Commit

Permalink
feat: Lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Smirnov committed Sep 17, 2023
1 parent 4d19bd0 commit 156e4d8
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "typeorm-ts-node-commonjs"
"typeorm": "typeorm-ts-node-commonjs",
"generate-postman": "node swaggerConverter.js"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IsExistConstraint } from '@/validators/isExist/isExistConstraint';
import { TeacherModule } from '@/teacher/teacher.module';
import { AuthModule } from '@/auth/auth.module';
import { UserModule } from '@/user/user.module';
import { LessonModule } from '@/lesson/lesson.module';

@Module({
imports: [
Expand All @@ -27,6 +28,7 @@ import { UserModule } from '@/user/user.module';
TeacherModule,
AuthModule,
UserModule,
LessonModule,
],
providers: [IsExistConstraint],
})
Expand Down
5 changes: 0 additions & 5 deletions src/child/child.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
ApiBody,
ApiNotFoundResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse,
Expand All @@ -45,7 +43,6 @@ export class ChildController {
}

@ApiOperation({ summary: 'Update child by id' })
@ApiParam({ name: 'id', type: Number })
@ApiBody({ type: UpdateChildDto })
@ApiResponse({ status: 200, type: Child })
@ApiNotFoundResponse({ type: NotFoundException })
Expand All @@ -55,7 +52,6 @@ export class ChildController {
return this.childrenService.update(id, dto);
}
@ApiOperation({ summary: 'Get child by id' })
@ApiParam({ name: 'id', type: Number })
@ApiResponse({ status: 200, type: Child })
@ApiNotFoundResponse({ type: NotFoundException })
@HttpCode(200)
Expand All @@ -73,7 +69,6 @@ export class ChildController {
}

@ApiOperation({ summary: 'Remove child' })
@ApiQuery({ name: 'id', type: Number })
@ApiResponse({ status: 204 })
@HttpCode(204)
@Delete(':id')
Expand Down
5 changes: 0 additions & 5 deletions src/client/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
ApiBody,
ApiNotFoundResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse,
Expand All @@ -43,7 +41,6 @@ export class ClientController {
}

@ApiOperation({ summary: 'Update client by id' })
@ApiParam({ name: 'id', type: Number })
@ApiBody({ type: UpdateClientDto })
@ApiResponse({ status: 200, type: Client })
@ApiNotFoundResponse({ type: NotFoundException })
Expand All @@ -54,7 +51,6 @@ export class ClientController {
}

@ApiOperation({ summary: 'Get client by id' })
@ApiParam({ name: 'id', type: Number })
@ApiResponse({ status: 200, type: Client })
@ApiNotFoundResponse({ type: NotFoundException })
@HttpCode(200)
Expand All @@ -72,7 +68,6 @@ export class ClientController {
}

@ApiOperation({ summary: 'Remove client' })
@ApiQuery({ name: 'id', type: Number })
@ApiResponse({ status: 204 })
@HttpCode(204)
@Delete(':id')
Expand Down
10 changes: 10 additions & 0 deletions src/lesson/dto/create-lesson.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsExist } from '@/validators';
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class CreateLessonDto {
@ApiProperty({ example: 'Математика' })
@IsString()
@IsExist({ column: 'name', tableName: 'lesson', isUnique: true })
name: string;
}
4 changes: 4 additions & 0 deletions src/lesson/dto/update-lesson.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateLessonDto } from '@/lesson/dto/create-lesson.dto';

export class UpdateLessonDto extends PartialType(CreateLessonDto) {}
30 changes: 30 additions & 0 deletions src/lesson/entities/lesson.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Teacher } from '@/teacher/entities/teacher.entity';
import { ApiProperty } from '@nestjs/swagger';
import {
Column,
Entity,
JoinTable,
ManyToMany,
PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Lesson {
@ApiProperty({ example: 1 })
@PrimaryGeneratedColumn()
id: number;

@ApiProperty({ example: 'Математика' })
@Column({ nullable: false, unique: true })
name: string;

@ApiProperty({ type: [Teacher] })
@ManyToMany(() => Teacher, {
nullable: false,
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT',
eager: true,
})
@JoinTable()
teachers: Teacher[];
}
77 changes: 77 additions & 0 deletions src/lesson/lesson.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Lesson } from '@/lesson/entities/lesson.entity';
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UnauthorizedException,
HttpCode,
NotFoundException,
} from '@nestjs/common';
import { LessonService } from '@/lesson/lesson.service';
import { CreateLessonDto } from '@/lesson/dto/create-lesson.dto';
import { UpdateLessonDto } from '@/lesson/dto/update-lesson.dto';
import {
ApiBearerAuth,
ApiBody,
ApiNotFoundResponse,
ApiOperation,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

@ApiTags('Lesson')
@ApiBearerAuth()
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@Controller('lesson')
export class LessonController {
constructor(private readonly lessonService: LessonService) {}

@ApiOperation({ summary: 'Create new lesson' })
@ApiResponse({ status: 201, type: Lesson })
@ApiBody({ type: CreateLessonDto })
@HttpCode(201)
@Post()
create(@Body() createLessonDto: CreateLessonDto) {
return this.lessonService.create(createLessonDto);
}

@ApiOperation({ summary: 'Get list of lesson' })
@ApiResponse({ status: 200, type: [Lesson] })
@HttpCode(200)
@Get()
getAll() {
return this.lessonService.findAll();
}

@ApiOperation({ summary: 'Get lesson by id' })
@ApiResponse({ status: 200, type: Lesson })
@ApiNotFoundResponse({ type: NotFoundException })
@HttpCode(200)
@Get(':id')
getOne(@Param('id') id: number) {
return this.lessonService.findOne(id);
}

@ApiOperation({ summary: 'Update lesson by id' })
@ApiBody({ type: UpdateLessonDto })
@ApiResponse({ status: 200, type: Lesson })
@ApiNotFoundResponse({ type: NotFoundException })
@HttpCode(200)
@Patch(':id')
update(@Param('id') id: number, @Body() updateLessonDto: UpdateLessonDto) {
return this.lessonService.update(id, updateLessonDto);
}

@ApiOperation({ summary: 'Remove lesson' })
@ApiResponse({ status: 204 })
@HttpCode(204)
@Delete(':id')
remove(@Param('id') id: number) {
return this.lessonService.remove(id);
}
}
13 changes: 13 additions & 0 deletions src/lesson/lesson.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AuthModule } from '@/auth/auth.module';
import { Lesson } from '@/lesson/entities/lesson.entity';
import { Module } from '@nestjs/common';
import { LessonService } from '@/lesson/lesson.service';
import { LessonController } from '@/lesson/lesson.controller';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
controllers: [LessonController],
providers: [LessonService],
imports: [TypeOrmModule.forFeature([Lesson]), AuthModule],
})
export class LessonModule {}
38 changes: 38 additions & 0 deletions src/lesson/lesson.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Lesson } from '@/lesson/entities/lesson.entity';
import { Injectable } from '@nestjs/common';
import { CreateLessonDto } from '@/lesson/dto/create-lesson.dto';
import { UpdateLessonDto } from '@/lesson/dto/update-lesson.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class LessonService {
constructor(
@InjectRepository(Lesson)
private readonly lessonRepository: Repository<Lesson>,
) {}

create(createLessonDto: CreateLessonDto) {
return this.lessonRepository.save(createLessonDto);
}

findAll() {
return this.lessonRepository.find({
relations: {
teachers: true,
},
});
}

findOne(id: number) {
return this.lessonRepository.findOneBy({ id, teachers: true });
}

update(id: number, updateLessonDto: UpdateLessonDto) {
return this.lessonRepository.update({ id }, updateLessonDto);
}

remove(id: number) {
return this.lessonRepository.delete(id);
}
}
5 changes: 0 additions & 5 deletions src/teacher/teacher.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import {
ApiBody,
ApiNotFoundResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse,
Expand Down Expand Up @@ -51,7 +49,6 @@ export class TeacherController {
}

@ApiOperation({ summary: 'Get teacher by id' })
@ApiParam({ name: 'id', type: Number })
@ApiResponse({ status: 200, type: Teacher })
@ApiNotFoundResponse({ type: NotFoundException })
@HttpCode(200)
Expand All @@ -61,7 +58,6 @@ export class TeacherController {
}

@ApiOperation({ summary: 'Update teacher by id' })
@ApiParam({ name: 'id', type: Number })
@ApiBody({ type: UpdateTeacherDto })
@ApiResponse({ status: 200, type: Teacher })
@ApiNotFoundResponse({ type: NotFoundException })
Expand All @@ -72,7 +68,6 @@ export class TeacherController {
}

@ApiOperation({ summary: 'Remove teacher' })
@ApiQuery({ name: 'id', type: Number })
@ApiResponse({ status: 204 })
@HttpCode(204)
@Delete(':id')
Expand Down

0 comments on commit 156e4d8

Please sign in to comment.