From edf6e1e90a7aa88b748e34b8094e3101d3258ed7 Mon Sep 17 00:00:00 2001 From: Gerbera3090 Date: Mon, 25 Nov 2024 04:34:54 +0900 Subject: [PATCH] feat: post for organiazation that apiOrg002 --- .../controller/organization.controller.ts | 15 ++++++- .../repository/organization.repository.ts | 40 +++++++++++++++++++ .../service/organization.service.ts | 27 ++++++++++++- .../interface/src/api/organization/index.ts | 3 ++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/packages/api/src/feature/organization/controller/organization.controller.ts b/packages/api/src/feature/organization/controller/organization.controller.ts index d28af44..c403d4f 100644 --- a/packages/api/src/feature/organization/controller/organization.controller.ts +++ b/packages/api/src/feature/organization/controller/organization.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Param, UsePipes } from "@nestjs/common"; +import { Body, Controller, Get, Param, Post, UsePipes } from "@nestjs/common"; import { ZodPipe } from "@sparcs-students/api/common/pipes/zod-pipe"; import { @@ -6,6 +6,10 @@ import { ApiOrg001RequestUrl, apiOrg001, ApiOrg001RequestParam, + ApiOrg002RequestUrl, + apiOrg002, + ApiOrg002RequestBody, + ApiOrg002ResponseOK, } from "@sparcs-students/interface/api/organization/index"; import { OrganizationService } from "../service/organization.service"; @@ -21,4 +25,13 @@ export class OrganizationController { ): Promise { return this.organizationService.getOrganizationsBySemesterId(param); } + + @Post(ApiOrg002RequestUrl) + @UsePipes(new ZodPipe(apiOrg002)) + async postOrganization( + @Body() body: ApiOrg002RequestBody, + ): Promise { + const res = await this.organizationService.postOrganization(body); + return res; + } } diff --git a/packages/api/src/feature/organization/repository/organization.repository.ts b/packages/api/src/feature/organization/repository/organization.repository.ts index 410a62e..14c67cf 100644 --- a/packages/api/src/feature/organization/repository/organization.repository.ts +++ b/packages/api/src/feature/organization/repository/organization.repository.ts @@ -1,4 +1,5 @@ import { Injectable, Inject } from "@nestjs/common"; +import { ApiOrg002RequestBody } from "@sparcs-students/interface/api/organization/index"; import { and, or, lte, gte, eq, isNull } from "drizzle-orm"; import { MySql2Database } from "drizzle-orm/mysql2"; import { DrizzleAsyncProvider } from "src/drizzle/drizzle.provider"; @@ -106,4 +107,43 @@ export class OrganizationRepository { organizationTypeEnum: row.organization_type_enum, })); } + + async ckOrganizationBeforeCreate( + body: ApiOrg002RequestBody, + ): Promise { + const select = await this.db + .select() + .from(Organization) + .where( + and( + eq(Organization.name, body.name), + eq(Organization.nameEng, body.nameEng), + eq(Organization.organizationTypeEnumId, body.organizationTypeId), + eq(Organization.foundingYear, body.foundingYear), + eq(Organization.startTerm, body.startTerm), + ), + ) + .limit(1); + if (select.length === 0) { + return 0; + } + return select[0].id; + } + + async createOrganization(body: ApiOrg002RequestBody): Promise { + await this.db + .insert(Organization) + .values({ + name: body.name, + nameEng: body.nameEng, + organizationTypeEnumId: body.organizationTypeId, + foundingYear: body.foundingYear, + startTerm: body.startTerm, + endTerm: body.endTerm ? body.endTerm : null, + }) + .execute(); + + const res = await this.ckOrganizationBeforeCreate(body); + return res; + } } diff --git a/packages/api/src/feature/organization/service/organization.service.ts b/packages/api/src/feature/organization/service/organization.service.ts index dcc6b05..3ba4397 100644 --- a/packages/api/src/feature/organization/service/organization.service.ts +++ b/packages/api/src/feature/organization/service/organization.service.ts @@ -1,8 +1,10 @@ -import { Injectable } from "@nestjs/common"; +import { HttpException, HttpStatus, Injectable } from "@nestjs/common"; import { ApiOrg001RequestParam, ApiOrg001ResponseOK, + ApiOrg002RequestBody, + ApiOrg002ResponseOK, } from "@sparcs-students/interface/api/organization/index"; import { SemesterPublicService } from "src/feature/semester/semester.public.service"; @@ -56,4 +58,27 @@ export class OrganizationService { return { organizationTypes }; } + + async postOrganization( + body: ApiOrg002RequestBody, + ): Promise { + const ck = + await this.organizationRepository.ckOrganizationBeforeCreate(body); + if (ck > 0) { + throw new HttpException( + "Organization already exists", + HttpStatus.BAD_REQUEST, + ); + } + const organizationId = + await this.organizationRepository.createOrganization(body); + if (organizationId < 1) { + throw new HttpException( + "Failed to create organization", + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + + return { organizationId }; + } } diff --git a/packages/interface/src/api/organization/index.ts b/packages/interface/src/api/organization/index.ts index 0201a28..2bbb282 100644 --- a/packages/interface/src/api/organization/index.ts +++ b/packages/interface/src/api/organization/index.ts @@ -1,2 +1,5 @@ export * from "./endpoint/apiOrg001"; export { default as apiOrg001 } from "./endpoint/apiOrg001"; // default export 추가 + +export * from "./endpoint/apiOrg002"; +export { default as apiOrg002 } from "./endpoint/apiOrg002"; // default export 추가