refactor(core): Move tag collection into repository (no-changelog) (#6860)

* refactor(core): Move tag collection into repository

* Fix tests

* Address feedback

* Fix missing spot
This commit is contained in:
Iván Ovejero
2023-08-08 14:08:56 +02:00
committed by GitHub
parent 8de28fe4d0
commit 11440bfd3c
15 changed files with 59 additions and 61 deletions

View File

@@ -1,35 +1,25 @@
import { Request, Response, NextFunction } from 'express';
import type { Config } from '@/config';
import config from '@/config';
import { Authorized, Delete, Get, Middleware, Patch, Post, RestController } from '@/decorators';
import type { IDatabaseCollections, IExternalHooksClass, ITagWithCountDb } from '@/Interfaces';
import { TagEntity } from '@db/entities/TagEntity';
import type { TagRepository } from '@db/repositories';
import { type ITagWithCountDb } from '@/Interfaces';
import type { TagEntity } from '@db/entities/TagEntity';
import { TagRepository } from '@db/repositories';
import { validateEntity } from '@/GenericHelpers';
import { BadRequestError } from '@/ResponseHelper';
import { TagsRequest } from '@/requests';
import { Service } from 'typedi';
import { ExternalHooks } from '@/ExternalHooks';
@Authorized()
@RestController('/tags')
@Service()
export class TagsController {
private config: Config;
private config = config;
private externalHooks: IExternalHooksClass;
private tagsRepository: TagRepository;
constructor({
config,
externalHooks,
repositories,
}: {
config: Config;
externalHooks: IExternalHooksClass;
repositories: Pick<IDatabaseCollections, 'Tag'>;
}) {
this.config = config;
this.externalHooks = externalHooks;
this.tagsRepository = repositories.Tag;
}
constructor(
private tagsRepository: TagRepository,
private externalHooks: ExternalHooks,
) {}
// TODO: move this into a new decorator `@IfEnabled('workflowTagsDisabled')`
@Middleware()
@@ -63,8 +53,7 @@ export class TagsController {
// Creates a tag
@Post('/')
async createTag(req: TagsRequest.Create): Promise<TagEntity> {
const newTag = new TagEntity();
newTag.name = req.body.name.trim();
const newTag = this.tagsRepository.create({ name: req.body.name.trim() });
await this.externalHooks.run('tag.beforeCreate', [newTag]);
await validateEntity(newTag);
@@ -77,12 +66,7 @@ export class TagsController {
// Updates a tag
@Patch('/:id(\\w+)')
async updateTag(req: TagsRequest.Update): Promise<TagEntity> {
const { name } = req.body;
const { id } = req.params;
const newTag = new TagEntity();
newTag.id = id;
newTag.name = name.trim();
const newTag = this.tagsRepository.create({ id: req.params.id, name: req.body.name.trim() });
await this.externalHooks.run('tag.beforeUpdate', [newTag]);
await validateEntity(newTag);