From 23272488d9edd966081c92b84bdfc7fc21f8b532 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 22 Nov 2022 15:11:07 +0900 Subject: [PATCH] Remove ProjectWriteItem --- frontend/domain/models/project/project.ts | 60 +++++++++++-------- .../models/project/projectRepository.ts | 6 +- .../project/apiProjectRepository.ts | 21 ++++--- .../project/projectApplicationService.ts | 14 ++--- 4 files changed, 56 insertions(+), 45 deletions(-) diff --git a/frontend/domain/models/project/project.ts b/frontend/domain/models/project/project.ts index 300640c4..db2a2c25 100644 --- a/frontend/domain/models/project/project.ts +++ b/frontend/domain/models/project/project.ts @@ -25,22 +25,51 @@ export class ProjectReadItem { readonly name: string, readonly description: string, readonly guideline: string, - readonly users: number[], - readonly tags: Object[], readonly projectType: ProjectType, - readonly createdAt: string, - readonly updatedAt: string, - readonly author: string, readonly randomOrder: boolean, readonly collaborativeAnnotation: boolean, readonly exclusiveCategories: boolean, - readonly resourceType: string, readonly allowOverlapping: boolean, readonly graphemeMode: boolean, readonly useRelation: boolean, - readonly isTextProject: boolean + readonly tags: Object[], + readonly users: number[] = [], + readonly createdAt: string = '', + readonly updatedAt: string = '', + readonly author: string = '', + readonly isTextProject: boolean = false ) {} + static create( + id: number, + name: string, + description: string, + guideline: string, + projectType: ProjectType, + randomOrder: boolean, + collaborativeAnnotation: boolean, + exclusiveCategories: boolean, + allowOverlapping: boolean, + graphemeMode: boolean, + useRelation: boolean, + tags: Object[] + ) { + return new ProjectReadItem( + id, + name, + description, + guideline, + projectType, + randomOrder, + collaborativeAnnotation, + exclusiveCategories, + allowOverlapping, + graphemeMode, + useRelation, + tags + ) + } + get canDefineLabel(): boolean { return this.canDefineCategory || this.canDefineSpan } @@ -72,23 +101,6 @@ export class ProjectReadItem { } return [this.projectType] } -} - -export class ProjectWriteItem { - constructor( - public id: number, - public name: string, - public description: string, - public guideline: string, - public projectType: ProjectType, - public randomOrder: boolean, - public collaborativeAnnotation: boolean, - public exclusiveCategories: boolean, - public allowOverlapping: boolean, - public graphemeMode: boolean, - public useRelation: boolean, - public tags: string[] - ) {} get resourceType(): string { const mapping = { diff --git a/frontend/domain/models/project/projectRepository.ts b/frontend/domain/models/project/projectRepository.ts index 301ad357..def492e5 100644 --- a/frontend/domain/models/project/projectRepository.ts +++ b/frontend/domain/models/project/projectRepository.ts @@ -1,5 +1,5 @@ import { Page } from '~/domain/models/page' -import { ProjectReadItem, ProjectWriteItem } from '~/domain/models/project/project' +import { ProjectReadItem } from '~/domain/models/project/project' const sortableFieldList = ['name', 'projectType', 'createdAt', 'author'] as const type SortableFields = typeof sortableFieldList[number] @@ -27,9 +27,9 @@ export interface ProjectRepository { findById(id: string): Promise - create(item: ProjectWriteItem): Promise + create(item: ProjectReadItem): Promise - update(item: ProjectWriteItem): Promise + update(item: ProjectReadItem): Promise bulkDelete(projectIds: number[]): Promise } diff --git a/frontend/repositories/project/apiProjectRepository.ts b/frontend/repositories/project/apiProjectRepository.ts index ab069933..a60bc0cb 100644 --- a/frontend/repositories/project/apiProjectRepository.ts +++ b/frontend/repositories/project/apiProjectRepository.ts @@ -1,5 +1,5 @@ import { Page } from '@/domain/models/page' -import { ProjectReadItem, ProjectWriteItem } from '@/domain/models/project/project' +import { ProjectReadItem } from '@/domain/models/project/project' import { ProjectRepository, SearchQuery } from '@/domain/models/project/projectRepository' import ApiService from '@/services/api.service' @@ -9,24 +9,23 @@ function toModel(item: { [key: string]: any }): ProjectReadItem { item.name, item.description, item.guideline, - item.users, - item.tags, item.project_type, - item.created_at, - item.updated_at, - item.author, item.random_order, item.collaborative_annotation, item.single_class_classification, - item.resourcetype, item.allow_overlapping, item.grapheme_mode, item.use_relation, + item.tags, + item.users, + item.created_at, + item.updated_at, + item.author, item.is_text_project ) } -function toPayload(item: ProjectWriteItem): { [key: string]: any } { +function toPayload(item: ProjectReadItem): { [key: string]: any } { return { id: item.id, name: item.name, @@ -39,7 +38,7 @@ function toPayload(item: ProjectWriteItem): { [key: string]: any } { allow_overlapping: item.allowOverlapping, grapheme_mode: item.graphemeMode, use_relation: item.useRelation, - tags: item.tags.map((tag) => ({ text: tag })), + tags: item.tags, resourcetype: item.resourceType } } @@ -72,14 +71,14 @@ export class APIProjectRepository implements ProjectRepository { return toModel(response.data) } - async create(item: ProjectWriteItem): Promise { + async create(item: ProjectReadItem): Promise { const url = `/projects` const payload = toPayload(item) const response = await this.request.post(url, payload) return toModel(response.data) } - async update(item: ProjectWriteItem): Promise { + async update(item: ProjectReadItem): Promise { const url = `/projects/${item.id}` const payload = toPayload(item) await this.request.patch(url, payload) diff --git a/frontend/services/application/project/projectApplicationService.ts b/frontend/services/application/project/projectApplicationService.ts index 251763dd..b77e87ff 100644 --- a/frontend/services/application/project/projectApplicationService.ts +++ b/frontend/services/application/project/projectApplicationService.ts @@ -1,4 +1,4 @@ -import { ProjectWriteItem } from '~/domain/models/project/project' +import { ProjectReadItem } from '~/domain/models/project/project' import { ProjectRepository, SearchQuery } from '~/domain/models/project/projectRepository' import { ProjectDTO, @@ -27,7 +27,7 @@ export class ProjectApplicationService { public async create(item: ProjectWriteDTO): Promise { try { - const project = this.toWriteModel(item) + const project = this.toModel(item) const response = await this.repository.create(project) return new ProjectDTO(response) } catch (e: any) { @@ -37,8 +37,8 @@ export class ProjectApplicationService { public async update(item: ProjectWriteDTO): Promise { try { - const project = this.toWriteModel(item) - project.tags = [] + item.tags = [] // TODO: somewhat hacky + const project = this.toModel(item) await this.repository.update(project) } catch (e: any) { throw new Error(e.response.data.detail) @@ -50,8 +50,8 @@ export class ProjectApplicationService { return this.repository.bulkDelete(ids) } - private toWriteModel(item: ProjectWriteDTO): ProjectWriteItem { - return new ProjectWriteItem( + private toModel(item: ProjectWriteDTO): ProjectReadItem { + return ProjectReadItem.create( item.id, item.name, item.description, @@ -63,7 +63,7 @@ export class ProjectApplicationService { item.allowOverlapping, item.graphemeMode, item.useRelation, - item.tags + item.tags.map((tag) => ({ text: tag })) ) } }