-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ALCS-2325 Tag/Category entities and cruds
- Loading branch information
Showing
15 changed files
with
441 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
18 changes: 18 additions & 0 deletions
18
services/apps/alcs/src/alcs/admin/tag-category/tag-category.controller.spec.ts
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 { TagCategoryController } from './tag-category.controller'; | ||
|
||
describe('TagCategoryController', () => { | ||
let controller: TagCategoryController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [TagCategoryController], | ||
}).compile(); | ||
|
||
controller = module.get<TagCategoryController>(TagCategoryController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
services/apps/alcs/src/alcs/admin/tag-category/tag-category.controller.ts
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,55 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiOAuth2 } from '@nestjs/swagger'; | ||
import * as config from 'config'; | ||
import { RolesGuard } from '../../../common/authorization/roles-guard.service'; | ||
import { UserRoles } from '../../../common/authorization/roles.decorator'; | ||
import { AUTH_ROLE } from '../../../common/authorization/roles'; | ||
import { TagCategoryDto } from './tag-category.dto'; | ||
import { TagCategoryService } from './tag-category.service'; | ||
|
||
@Controller('tag-category') | ||
@ApiOAuth2(config.get<string[]>('KEYCLOAK.SCOPES')) | ||
@UseGuards(RolesGuard) | ||
export class TagCategoryController { | ||
|
||
constructor(private service: TagCategoryService) {} | ||
|
||
@Get('/:pageIndex/:itemsPerPage') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async fetch( | ||
@Param('pageIndex') pageIndex: number, | ||
@Param('itemsPerPage') itemsPerPage: number, | ||
@Query('search') search?: string, | ||
) { | ||
const result = await this.service.fetch(pageIndex, itemsPerPage, search); | ||
return { data: result[0], total: result[1] }; | ||
} | ||
|
||
@Post('') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async create(@Body() createDto: TagCategoryDto) { | ||
return await this.service.create(createDto); | ||
} | ||
|
||
@Put('/:uuid') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async update(@Param('uuid') uuid: string, @Body() updateDto: TagCategoryDto) { | ||
return await this.service.update(uuid, updateDto); | ||
} | ||
|
||
@Delete('/:uuid') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async delete(@Param('uuid') uuid: string) { | ||
return await this.service.delete(uuid); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
services/apps/alcs/src/alcs/admin/tag-category/tag-category.dto.ts
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,9 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class TagCategoryDto { | ||
@IsString() | ||
uuid: string; | ||
|
||
@IsString() | ||
name: string; | ||
} |
21 changes: 21 additions & 0 deletions
21
services/apps/alcs/src/alcs/admin/tag-category/tag-category.entity.ts
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,21 @@ | ||
import { AutoMap } from 'automapper-classes'; | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Base } from '../../../common/entities/base.entity'; | ||
|
||
@Entity({ comment: 'Tag category.' }) | ||
export class TagCategory extends Base { | ||
constructor(data?: Partial<TagCategory>) { | ||
super(); | ||
if (data) { | ||
Object.assign(this, data); | ||
} | ||
} | ||
|
||
@AutoMap() | ||
@PrimaryGeneratedColumn('uuid') | ||
uuid: string; | ||
|
||
@AutoMap() | ||
@Column() | ||
name: string; | ||
} |
18 changes: 18 additions & 0 deletions
18
services/apps/alcs/src/alcs/admin/tag-category/tag-category.service.spec.ts
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 { TagCategoryService } from './tag-category.service'; | ||
|
||
describe('TagCategoryService', () => { | ||
let service: TagCategoryService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [TagCategoryService], | ||
}).compile(); | ||
|
||
service = module.get<TagCategoryService>(TagCategoryService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
services/apps/alcs/src/alcs/admin/tag-category/tag-category.service.ts
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,56 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { TagCategory } from './tag-category.entity'; | ||
import { FindOptionsWhere, Like, Repository } from 'typeorm'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { TagCategoryDto } from './tag-category.dto'; | ||
|
||
@Injectable() | ||
export class TagCategoryService { | ||
constructor( | ||
@InjectRepository(TagCategory) | ||
private repository: Repository<TagCategory>, | ||
) {} | ||
|
||
async fetch(pageIndex: number, itemsPerPage: number, search?: string) { | ||
let searchExpression: FindOptionsWhere<TagCategory> | undefined = undefined; | ||
|
||
if (search) { | ||
searchExpression = { | ||
name: Like(`%${search}%`), | ||
}; | ||
} | ||
|
||
return ( | ||
(await this.repository.findAndCount({ | ||
where: searchExpression, | ||
order: { name: 'DESC' }, | ||
take: itemsPerPage, | ||
skip: pageIndex * itemsPerPage, | ||
})) || [[], 0] | ||
); | ||
} | ||
|
||
async create(dto: TagCategoryDto) { | ||
const newTagCategory = new TagCategory(); | ||
newTagCategory.name = dto.name; | ||
return this.repository.save(newTagCategory); | ||
} | ||
|
||
async getOneOrFail(uuid: string) { | ||
return await this.repository.findOneOrFail({ | ||
where: { uuid }, | ||
}); | ||
} | ||
|
||
async update(uuid: string, updateDto: TagCategoryDto) { | ||
const tagCategory = await this.getOneOrFail(uuid); | ||
tagCategory.name = updateDto.name; | ||
return await this.repository.save(tagCategory); | ||
} | ||
|
||
async delete(uuid: string) { | ||
const tagCategory = await this.getOneOrFail(uuid); | ||
|
||
return await this.repository.remove(tagCategory); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
services/apps/alcs/src/alcs/admin/tag/tag.controller.spec.ts
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 { TagController } from './tag.controller'; | ||
|
||
describe('TagController', () => { | ||
let controller: TagController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [TagController], | ||
}).compile(); | ||
|
||
controller = module.get<TagController>(TagController); | ||
}); | ||
|
||
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,54 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiOAuth2 } from '@nestjs/swagger'; | ||
import * as config from 'config'; | ||
import { RolesGuard } from '../../../common/authorization/roles-guard.service'; | ||
import { UserRoles } from '../../../common/authorization/roles.decorator'; | ||
import { TagService } from './tag.service'; | ||
import { AUTH_ROLE } from 'apps/alcs/src/common/authorization/roles'; | ||
import { TagDto } from './tag.dto'; | ||
|
||
@Controller('tag') | ||
@ApiOAuth2(config.get<string[]>('KEYCLOAK.SCOPES')) | ||
@UseGuards(RolesGuard) | ||
export class TagController { | ||
constructor(private service: TagService) {} | ||
|
||
@Get('/:pageIndex/:itemsPerPage') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async fetch( | ||
@Param('pageIndex') pageIndex: number, | ||
@Param('itemsPerPage') itemsPerPage: number, | ||
@Query('search') search?: string, | ||
) { | ||
const result = await this.service.fetch(pageIndex, itemsPerPage, search); | ||
return { data: result[0], total: result[1] }; | ||
} | ||
|
||
@Post('') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async create(@Body() createDto: TagDto) { | ||
return await this.service.create(createDto); | ||
} | ||
|
||
@Put('/:uuid') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async update(@Param('uuid') uuid: string, @Body() updateDto: TagDto) { | ||
return await this.service.update(uuid, updateDto); | ||
} | ||
|
||
@Delete('/:uuid') | ||
@UserRoles(AUTH_ROLE.ADMIN) | ||
async delete(@Param('uuid') uuid: string) { | ||
return await this.service.delete(uuid); | ||
} | ||
} |
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 { IsBoolean, IsObject, IsString } from 'class-validator'; | ||
import { TagCategoryDto } from '../tag-category/tag-category.dto'; | ||
|
||
export class TagDto { | ||
@IsString() | ||
name: string; | ||
|
||
@IsBoolean() | ||
isActive: boolean; | ||
|
||
@IsObject() | ||
category: TagCategoryDto; | ||
} |
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,29 @@ | ||
import { AutoMap } from 'automapper-classes'; | ||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Base } from '../../../common/entities/base.entity'; | ||
import { TagCategory } from '../tag-category/tag-category.entity'; | ||
|
||
@Entity({ comment: 'Tag.' }) | ||
export class Tag extends Base { | ||
constructor(data?: Partial<Tag>) { | ||
super(); | ||
if (data) { | ||
Object.assign(this, data); | ||
} | ||
} | ||
|
||
@AutoMap() | ||
@PrimaryGeneratedColumn('uuid') | ||
uuid: string; | ||
|
||
@AutoMap() | ||
@Column() | ||
name: string; | ||
|
||
@AutoMap() | ||
@Column({ default: true }) | ||
isActive: boolean; | ||
|
||
@ManyToOne(() => TagCategory) | ||
category: TagCategory; | ||
} |
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 { TagService } from './tag.service'; | ||
|
||
describe('TagCategoryService', () => { | ||
let service: TagService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [TagService], | ||
}).compile(); | ||
|
||
service = module.get<TagService>(TagService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.