Skip to content

Commit

Permalink
ALCS-2325 Tag/Category entities and cruds
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarreta committed Oct 24, 2024
1 parent c0b806c commit 7db21ad
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 0 deletions.
12 changes: 12 additions & 0 deletions services/apps/alcs/src/alcs/admin/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import { NoiSubtypeController } from './noi-subtype/noi-subtype.controller';
import { NoiSubtypeService } from './noi-subtype/noi-subtype.service';
import { UnarchiveCardController } from './unarchive-card/unarchive-card.controller';
import { UnarchiveCardService } from './unarchive-card/unarchive-card.service';
import { TagCategoryService } from './tag-category/tag-category.service';
import { TagCategoryController } from './tag-category/tag-category.controller';
import { TagCategory } from './tag-category/tag-category.entity';
import { Tag } from './tag/tag.entity';
import { TagController } from './tag/tag.controller';
import { TagService } from './tag/tag.service';

@Module({
imports: [
Expand All @@ -42,6 +48,8 @@ import { UnarchiveCardService } from './unarchive-card/unarchive-card.service';
NoticeOfIntentSubtype,
ApplicationDecisionConditionType,
Configuration,
TagCategory,
Tag,
]),
ApplicationModule,
NoticeOfIntentModule,
Expand All @@ -62,6 +70,8 @@ import { UnarchiveCardService } from './unarchive-card/unarchive-card.service';
UnarchiveCardController,
NoiSubtypeController,
ApplicationDecisionMakerController,
TagCategoryController,
TagController,
ApplicationDecisionConditionTypesController,
CardStatusController,
BoardManagementController,
Expand All @@ -71,6 +81,8 @@ import { UnarchiveCardService } from './unarchive-card/unarchive-card.service';
HolidayService,
ApplicationCeoCriterionService,
ApplicationDecisionMakerService,
TagCategoryService,
TagService,
UnarchiveCardService,
NoiSubtypeService,
ApplicationDecisionConditionTypesService,
Expand Down
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();
});
});
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);
}
}
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;
}
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;
}
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();
});
});
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 services/apps/alcs/src/alcs/admin/tag/tag.controller.spec.ts
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();
});
});
54 changes: 54 additions & 0 deletions services/apps/alcs/src/alcs/admin/tag/tag.controller.ts
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);
}
}
13 changes: 13 additions & 0 deletions services/apps/alcs/src/alcs/admin/tag/tag.dto.ts
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;
}
29 changes: 29 additions & 0 deletions services/apps/alcs/src/alcs/admin/tag/tag.entity.ts
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;
}
18 changes: 18 additions & 0 deletions services/apps/alcs/src/alcs/admin/tag/tag.service.spec.ts
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();
});
});
Loading

0 comments on commit 7db21ad

Please sign in to comment.