Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 impl post apis for filling dbs #57

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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 {
ApiOrg001ResponseOK,
ApiOrg001RequestUrl,
apiOrg001,
ApiOrg001RequestParam,
ApiOrg002RequestUrl,
apiOrg002,
ApiOrg002RequestBody,
ApiOrg002ResponseOK,
} from "@sparcs-students/interface/api/organization/index";

import { OrganizationService } from "../service/organization.service";
Expand All @@ -21,4 +25,13 @@ export class OrganizationController {
): Promise<ApiOrg001ResponseOK> {
return this.organizationService.getOrganizationsBySemesterId(param);
}

@Post(ApiOrg002RequestUrl)
@UsePipes(new ZodPipe(apiOrg002))
async postOrganization(
@Body() body: ApiOrg002RequestBody,
): Promise<ApiOrg002ResponseOK> {
const res = await this.organizationService.postOrganization(body);
return res;
}
}
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -106,4 +107,43 @@ export class OrganizationRepository {
organizationTypeEnum: row.organization_type_enum,
}));
}

async ckOrganizationBeforeCreate(
body: ApiOrg002RequestBody,
): Promise<number> {
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<number> {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -56,4 +58,27 @@ export class OrganizationService {

return { organizationTypes };
}

async postOrganization(
body: ApiOrg002RequestBody,
): Promise<ApiOrg002ResponseOK> {
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 };
}
}
64 changes: 64 additions & 0 deletions packages/interface/src/api/organization/endpoint/apiOrg002.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { HttpStatusCode } from "axios";
import { z } from "zod";

import {
zOrgName,
zOrgNameEng,
} from "@sparcs-students/interface/common/stringLength";
import { OrganizationTypeE } from "@sparcs-students/interface/common/enum";
import { zId } from "@sparcs-students/interface/common/type/ids";

/**
* @version v0.1
* @description 총학생회장 권한으로 새로운 단체를 생성합니다.
*/

const url = () => `/uapresident/organizations/organization`;
const method = "POST";

const requestParam = z.object({});

const requestQuery = z.object({});

const requestBody = z.object({
name: zOrgName,
nameEng: zOrgNameEng,
organizationTypeId: z.nativeEnum(OrganizationTypeE),
foundingYear: z.coerce.number().int(),
startTerm: z.coerce.date(),
endTerm: z.coerce.date().optional(),
});

const responseBodyMap = {
[HttpStatusCode.Ok]: z.object({
organizationId: zId,
}),
};

const responseErrorMap = {};

const apiOrg002 = {
url,
method,
requestParam,
requestQuery,
requestBody,
responseBodyMap,
responseErrorMap,
};

type ApiOrg002RequestParam = z.infer<typeof apiOrg002.requestParam>;
type ApiOrg002RequestQuery = z.infer<typeof apiOrg002.requestQuery>;
type ApiOrg002RequestBody = z.infer<typeof apiOrg002.requestBody>;
type ApiOrg002ResponseOK = z.infer<(typeof apiOrg002.responseBodyMap)[200]>;

export default apiOrg002;

export const ApiOrg002RequestUrl = "/uapresident/organizations/organization";

export type {
ApiOrg002RequestParam,
ApiOrg002RequestQuery,
ApiOrg002RequestBody,
ApiOrg002ResponseOK,
};
3 changes: 3 additions & 0 deletions packages/interface/src/api/organization/index.ts
Original file line number Diff line number Diff line change
@@ -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 추가
1 change: 1 addition & 0 deletions packages/interface/src/common/enum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./organization.enum";
63 changes: 63 additions & 0 deletions packages/interface/src/common/enum/organization.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 조직 유형 E
export enum OrganizationTypeE {
Autonomous = 1, // 자치기구
Standing, // 상설위원회
Specialized, // 전문기구
Special, // 특별기구
Emergency, // 비상대책위원회
UA, // 학부 총학생회
StudentCouncil, // 학과학생회
Preparatory, // 준비위원회
Affairs, // 특임위원회
Inspect, // 조사위원회
}

// 조직 대표 유형 E
export enum OrganizationPresidentTypeE {
Chief = 1, // 정
Vice, // 부
}

// OrganizationTypeE
export const getDisplayNameOrganizationTypeE = (
type: OrganizationTypeE | undefined,
) => {
switch (type) {
case OrganizationTypeE.Autonomous:
return "자치기구";
case OrganizationTypeE.Standing:
return "상설위원회";
case OrganizationTypeE.Specialized:
return "전문기구";
case OrganizationTypeE.Special:
return "특별기구";
case OrganizationTypeE.Emergency:
return "비상대책위원회";
case OrganizationTypeE.UA:
return "학부 총학생회";
case OrganizationTypeE.StudentCouncil:
return "학과학생회";
case OrganizationTypeE.Preparatory:
return "준비위원회";
case OrganizationTypeE.Affairs:
return "특임위원회";
case OrganizationTypeE.Inspect:
return "조사위원회";
default:
return "";
}
};

// OrganizationPresidentTypeE
export const getDisplayNameOrganizationPresidentTypeE = (
type: OrganizationPresidentTypeE | undefined,
) => {
switch (type) {
case OrganizationPresidentTypeE.Chief:
return "정";
case OrganizationPresidentTypeE.Vice:
return "부";
default:
return "";
}
};
3 changes: 3 additions & 0 deletions packages/interface/src/common/type/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import z from "zod";

export const zId = z.coerce.number().int().min(1);
Loading