forked from coredumped7893/excali-api
-
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.
Merge pull request #10 from Excali-Studio/EXD-63-crud-for-tags
Exd 63 crud for tags
- Loading branch information
Showing
7 changed files
with
274 additions
and
7 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,66 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { CanvasTagService } from './canvas-tag.service'; | ||
import { CanvasTagCreateOrUpdateDTO, CanvasTagDTO } from './canvas.interface'; | ||
import { AuthenticatedGuard } from '../auth/guard/authenticated.guard'; | ||
import { Uuid } from '../common/common.interface'; | ||
import {ListFilter, PagedResult} from '../common/pageable.utils'; | ||
|
||
@Controller('/canvas-tag') | ||
export class CanvasTagController { | ||
constructor(private readonly canvasTagService: CanvasTagService) {} | ||
|
||
@Post() | ||
@UseGuards(AuthenticatedGuard) | ||
public async create( | ||
@Body() dto: CanvasTagCreateOrUpdateDTO, | ||
): Promise<CanvasTagDTO> { | ||
const tag = await this.canvasTagService.create(dto); | ||
return { | ||
id: tag.id, | ||
name: tag.name, | ||
color: tag.color, | ||
}; | ||
} | ||
|
||
@Get('/:id') | ||
@UseGuards(AuthenticatedGuard) | ||
public async readById(@Param('id') id: Uuid): Promise<CanvasTagDTO> { | ||
return await this.canvasTagService.readById(id); | ||
} | ||
|
||
@Get('/') | ||
@UseGuards(AuthenticatedGuard) | ||
public async readAll( | ||
@Query() filter: ListFilter, | ||
): Promise<PagedResult<CanvasTagDTO>> { | ||
return await this.canvasTagService.readAll(filter); | ||
} | ||
|
||
@Put('/:id') | ||
@UseGuards(AuthenticatedGuard) | ||
public async update( | ||
@Param('id') id: Uuid, | ||
@Body() dto: CanvasTagCreateOrUpdateDTO, | ||
): Promise<CanvasTagDTO> { | ||
return await this.canvasTagService.update({ | ||
...dto, | ||
id, | ||
}); | ||
} | ||
|
||
@Delete('/:id') | ||
@UseGuards(AuthenticatedGuard) | ||
public async delete(@Param('id') id: Uuid) { | ||
await this.canvasTagService.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,84 @@ | ||
import { ConflictException, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { CanvasTagEntity } from './entity/canvas-tag.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { | ||
CanvasTagCreateCommand, | ||
CanvasTagDeleteCommand, | ||
CanvasTagUpdateCommand, | ||
} from './canvas.interface'; | ||
import { | ||
ListFilter, | ||
PageableUtils, | ||
PagedResult, | ||
} from '../common/pageable.utils'; | ||
import { Uuid } from '../common/common.interface'; | ||
|
||
@Injectable() | ||
export class CanvasTagService { | ||
constructor( | ||
@InjectRepository(CanvasTagEntity) | ||
private readonly canvasTagRepository: Repository<CanvasTagEntity>, | ||
) {} | ||
|
||
public async create( | ||
command: CanvasTagCreateCommand, | ||
): Promise<CanvasTagEntity> { | ||
const tagName = command.name?.trim().toUpperCase(); | ||
const existingTag = await this.canvasTagRepository.findOne({ | ||
where: { name: tagName }, | ||
}); | ||
if (existingTag) { | ||
throw new ConflictException(); | ||
} | ||
const tag = new CanvasTagEntity(); | ||
tag.name = tagName; | ||
tag.color = command.color; | ||
await this.canvasTagRepository.save(tag); | ||
return tag; | ||
} | ||
|
||
public async readById(id: Uuid): Promise<CanvasTagEntity> { | ||
return this.canvasTagRepository.findOne({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
} | ||
|
||
public async readAll( | ||
filter: ListFilter, | ||
): Promise<PagedResult<CanvasTagEntity>> { | ||
return PageableUtils.producePagedResult( | ||
filter, | ||
await this.canvasTagRepository.findAndCount({ order: { name: 'asc' } }), | ||
); | ||
} | ||
|
||
public async update( | ||
command: CanvasTagUpdateCommand, | ||
): Promise<CanvasTagEntity> { | ||
const tagName = command.name?.trim().toUpperCase(); | ||
const tagToBeUpdated = await this.canvasTagRepository.findOne({ | ||
where: { id: command.id }, | ||
}); | ||
const existingTag = await this.canvasTagRepository.findOne({ | ||
where: { name: tagName }, | ||
}); | ||
if (existingTag && existingTag.id != command.id) { | ||
throw new ConflictException(); | ||
} | ||
tagToBeUpdated.name = tagName; | ||
tagToBeUpdated.color = command.color; | ||
await this.canvasTagRepository.save(tagToBeUpdated); | ||
return tagToBeUpdated; | ||
} | ||
|
||
public async delete(command: CanvasTagDeleteCommand) { | ||
try { | ||
await this.canvasTagRepository.delete(command.id); | ||
} catch (e) { | ||
throw new ConflictException(); | ||
} | ||
} | ||
} |
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