-
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.
add the last module, departments module
- Loading branch information
1 parent
dca1cdf
commit 3a9166b
Showing
8 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DepartmentsController } from './departments.controller'; | ||
import { DepartmentsService } from './departments.service'; | ||
|
||
describe('DepartmentsController', () => { | ||
let controller: DepartmentsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [DepartmentsController], | ||
providers: [DepartmentsService], | ||
}).compile(); | ||
|
||
controller = module.get<DepartmentsController>(DepartmentsController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,34 @@ | ||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; | ||
import { DepartmentsService } from './departments.service'; | ||
import { CreateDepartmentDto } from './dto/create-department.dto'; | ||
import { UpdateDepartmentDto } from './dto/update-department.dto'; | ||
|
||
@Controller('departments') | ||
export class DepartmentsController { | ||
constructor(private readonly departmentsService: DepartmentsService) {} | ||
|
||
@Post() | ||
create(@Body() createDepartmentDto: CreateDepartmentDto) { | ||
return this.departmentsService.create(createDepartmentDto); | ||
} | ||
|
||
@Get() | ||
findAll() { | ||
return this.departmentsService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: string) { | ||
return this.departmentsService.findOne(+id); | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id') id: string, @Body() updateDepartmentDto: UpdateDepartmentDto) { | ||
return this.departmentsService.update(+id, updateDepartmentDto); | ||
} | ||
|
||
@Delete(':id') | ||
remove(@Param('id') id: string) { | ||
return this.departmentsService.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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DepartmentsService } from './departments.service'; | ||
import { DepartmentsController } from './departments.controller'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Department } from './entities/department.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Department])], | ||
controllers: [DepartmentsController], | ||
providers: [DepartmentsService], | ||
}) | ||
export class DepartmentsModule {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DepartmentsService } from './departments.service'; | ||
|
||
describe('DepartmentsService', () => { | ||
let service: DepartmentsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [DepartmentsService], | ||
}).compile(); | ||
|
||
service = module.get<DepartmentsService>(DepartmentsService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateDepartmentDto } from './dto/create-department.dto'; | ||
import { UpdateDepartmentDto } from './dto/update-department.dto'; | ||
import { Repository } from 'typeorm'; | ||
import { Department } from './entities/department.entity'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
@Injectable() | ||
export class DepartmentsService { | ||
constructor( | ||
@InjectRepository(Department) | ||
private readonly departmentRepository: Repository<Department>, | ||
) {} | ||
|
||
async create(createDepartmentDto: CreateDepartmentDto) { | ||
const department = this.departmentRepository.create(createDepartmentDto); | ||
return this.departmentRepository.save(department); | ||
} | ||
|
||
async findAll() { | ||
return this.departmentRepository.find(); | ||
} | ||
|
||
async findOne(id: number) { | ||
return this.departmentRepository.findOne({ where: { id } }); | ||
} | ||
|
||
async update(id: number, updateDepartmentDto: UpdateDepartmentDto) { | ||
const department = this.departmentRepository.create(updateDepartmentDto); | ||
await this.departmentRepository.update(id, department); | ||
return this.findOne(id); | ||
} | ||
|
||
async remove(id: number) { | ||
return this.departmentRepository.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IsNotEmpty, IsNumber, IsString } from 'class-validator'; | ||
|
||
export interface IDepartment { | ||
name: string; | ||
employer_id: number; | ||
} | ||
|
||
export class CreateDepartmentDto implements IDepartment { | ||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@IsNumber() | ||
@IsNotEmpty() | ||
employer_id: number; | ||
} |
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/mapped-types'; | ||
import { CreateDepartmentDto } from './create-department.dto'; | ||
|
||
export class UpdateDepartmentDto extends PartialType(CreateDepartmentDto) {} |
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,25 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity({ name: 'departments' }) | ||
export class Department { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
employer_id: number; | ||
|
||
@CreateDateColumn() | ||
created_at: Date; | ||
|
||
@UpdateDateColumn() | ||
updated_at: Date; | ||
} |