-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxim Smirnov
committed
Sep 17, 2023
1 parent
4d19bd0
commit 156e4d8
Showing
11 changed files
with
176 additions
and
16 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
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
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,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; | ||
} |
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,4 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
import { CreateLessonDto } from '@/lesson/dto/create-lesson.dto'; | ||
|
||
export class UpdateLessonDto extends PartialType(CreateLessonDto) {} |
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,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[]; | ||
} |
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,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); | ||
} | ||
} |
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 { 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 {} |
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,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); | ||
} | ||
} |
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