-
Notifications
You must be signed in to change notification settings - Fork 1
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 #12 from wang-bam-bbang/building
feat: add building module and apply to post module
- Loading branch information
Showing
20 changed files
with
589 additions
and
30 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
prisma/migrations/20241208162457_add_building_and_location_detail/migration.sql
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,21 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `location` on the `post` table. All the data in the column will be lost. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "post" DROP COLUMN "location"; | ||
|
||
-- CreateTable | ||
CREATE TABLE "building" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(255) NOT NULL, | ||
"enName" VARCHAR(255) NOT NULL, | ||
"gps" VARCHAR(255) NOT NULL, | ||
"code" VARCHAR(50) NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "building_pkey" PRIMARY KEY ("id") | ||
); |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20241208170035_add_buildingname_unique_property/migration.sql
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,8 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[name]` on the table `building` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- CreateIndex | ||
CREATE UNIQUE INDEX "building_name_key" ON "building"("name"); |
21 changes: 21 additions & 0 deletions
21
prisma/migrations/20241208173953_connect_post_building/migration.sql
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,21 @@ | ||
/* | ||
Warnings: | ||
- You are about to alter the column `name` on the `building` table. The data in that column could be lost. The data in that column will be cast from `VarChar(255)` to `VarChar(20)`. | ||
- You are about to alter the column `enName` on the `building` table. The data in that column could be lost. The data in that column will be cast from `VarChar(255)` to `VarChar(20)`. | ||
- You are about to alter the column `gps` on the `building` table. The data in that column could be lost. The data in that column will be cast from `VarChar(255)` to `VarChar(30)`. | ||
- You are about to alter the column `code` on the `building` table. The data in that column could be lost. The data in that column will be cast from `VarChar(50)` to `VarChar(4)`. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "building" ALTER COLUMN "name" SET DATA TYPE VARCHAR(20), | ||
ALTER COLUMN "enName" SET DATA TYPE VARCHAR(20), | ||
ALTER COLUMN "gps" SET DATA TYPE VARCHAR(30), | ||
ALTER COLUMN "code" SET DATA TYPE VARCHAR(4); | ||
|
||
-- AlterTable | ||
ALTER TABLE "post" ADD COLUMN "buildingId" INTEGER NOT NULL DEFAULT 2, | ||
ADD COLUMN "locationDetail" TEXT; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "post" ADD CONSTRAINT "post_buildingId_fkey" FOREIGN KEY ("buildingId") REFERENCES "building"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
Post, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiInternalServerErrorResponse, | ||
ApiNoContentResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { BuildingService } from './building.service'; | ||
import { CreateBuildingDto } from './dto/req/createBuilding.dto'; | ||
import { BuildingResponseDto } from './dto/res/buildingRes.dto'; | ||
import { UpdateBuildingDto } from './dto/req/updateBuilding.dto'; | ||
|
||
@ApiTags('Building') | ||
@Controller('building') | ||
export class BuildingController { | ||
constructor(private buildingService: BuildingService) {} | ||
|
||
@ApiOperation({ | ||
summary: 'create building', | ||
description: 'register building information', | ||
}) | ||
@ApiOkResponse({ | ||
type: BuildingResponseDto, | ||
description: 'Return Created Building', | ||
}) | ||
@ApiInternalServerErrorResponse({ | ||
description: 'Internal Server Error', | ||
}) | ||
@Post() | ||
async createBuilding( | ||
@Body() createBuildingDto: CreateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
return this.buildingService.createBuilding(createBuildingDto); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: 'get all buildings', | ||
description: 'get all buildings information', | ||
}) | ||
@ApiOkResponse({ | ||
type: [BuildingResponseDto], | ||
description: 'Return All Buildings', | ||
}) | ||
@ApiInternalServerErrorResponse({ | ||
description: 'Internal Server Error', | ||
}) | ||
@Get() | ||
async getAllBuildings(): Promise<BuildingResponseDto[]> { | ||
return this.buildingService.getAllBuildings(); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: 'update building', | ||
description: 'update building', | ||
}) | ||
@ApiOkResponse({ | ||
type: BuildingResponseDto, | ||
description: 'Return updated building', | ||
}) | ||
@Patch(':id') | ||
async updateBuilding( | ||
@Param('id', ParseIntPipe) id: number, | ||
@Body() updateBuildingDto: UpdateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
return this.buildingService.updateBuilding(id, updateBuildingDto); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: 'delete building', | ||
description: 'delete building', | ||
}) | ||
@ApiNoContentResponse({ description: 'No content returned' }) | ||
@ApiInternalServerErrorResponse({ | ||
description: 'Internal Server Error', | ||
}) | ||
@Delete(':id') | ||
async deleteBuilding(@Param('id', ParseIntPipe) id: number): Promise<void> { | ||
return this.buildingService.deleteBuilding(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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BuildingController } from './building.controller'; | ||
import { PrismaModule } from 'src/prisma/prisma.module'; | ||
import { BuildingService } from './building.service'; | ||
import { BuildingRepository } from './building.repository'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
providers: [BuildingService, BuildingRepository], | ||
controllers: [BuildingController], | ||
exports: [BuildingService], | ||
}) | ||
export class BuildingModule {} |
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,56 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { CreateBuildingDto } from './dto/req/createBuilding.dto'; | ||
import { UpdateBuildingDto } from './dto/req/updateBuilding.dto'; | ||
import { BuildingResponseDto } from './dto/res/buildingRes.dto'; | ||
|
||
@Injectable() | ||
export class BuildingRepository { | ||
constructor(private prismaService: PrismaService) {} | ||
|
||
async createBuilding( | ||
createBuildingDto: CreateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
return this.prismaService.building.create({ | ||
data: { | ||
...createBuildingDto, | ||
}, | ||
}); | ||
} | ||
|
||
async getAllBuildings(): Promise<BuildingResponseDto[]> { | ||
return this.prismaService.building.findMany({ | ||
where: {}, | ||
}); | ||
} | ||
|
||
async getBuildingByName(name: string): Promise<BuildingResponseDto> { | ||
return this.prismaService.building.findUnique({ | ||
where: { name }, | ||
}); | ||
} | ||
|
||
async getBuildingById(id: number): Promise<BuildingResponseDto> { | ||
return this.prismaService.building.findUnique({ | ||
where: { id }, | ||
}); | ||
} | ||
|
||
async updateBuilding( | ||
id: number, | ||
updateBuildingDto: UpdateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
const updatedBuilding = await this.prismaService.building.update({ | ||
where: { id }, | ||
data: updateBuildingDto, | ||
}); | ||
|
||
return updatedBuilding; | ||
} | ||
|
||
async deleteBuilding(id: number): Promise<void> { | ||
await this.prismaService.building.delete({ | ||
where: { 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,57 @@ | ||
import { | ||
ConflictException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { CreateBuildingDto } from './dto/req/createBuilding.dto'; | ||
import { BuildingRepository } from './building.repository'; | ||
import { UpdateBuildingDto } from './dto/req/updateBuilding.dto'; | ||
import { BuildingResponseDto } from './dto/res/buildingRes.dto'; | ||
|
||
@Injectable() | ||
export class BuildingService { | ||
constructor(private buildingRepository: BuildingRepository) {} | ||
|
||
async createBuilding( | ||
createBuildingDto: CreateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
const building = await this.buildingRepository.getBuildingByName( | ||
createBuildingDto.name, | ||
); | ||
|
||
if (building) { | ||
throw new ConflictException('Building name already exists.'); | ||
} | ||
|
||
return this.buildingRepository.createBuilding(createBuildingDto); | ||
} | ||
|
||
async getAllBuildings(): Promise<BuildingResponseDto[]> { | ||
return this.buildingRepository.getAllBuildings(); | ||
} | ||
|
||
async updateBuilding( | ||
id: number, | ||
updateBuildingDto: UpdateBuildingDto, | ||
): Promise<BuildingResponseDto> { | ||
const building = await this.buildingRepository.getBuildingById(id); | ||
if (!building) { | ||
throw new NotFoundException('Building not found.'); | ||
} | ||
|
||
return this.buildingRepository.updateBuilding(id, updateBuildingDto); | ||
} | ||
|
||
async deleteBuilding(id: number): Promise<void> { | ||
const building = await this.buildingRepository.getBuildingById(id); | ||
if (!building) { | ||
throw new NotFoundException('Building not found.'); | ||
} | ||
|
||
return this.buildingRepository.deleteBuilding(id); | ||
} | ||
|
||
async getBuildingById(id: number): Promise<BuildingResponseDto> { | ||
return this.buildingRepository.getBuildingById(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,43 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString, Matches } from 'class-validator'; | ||
|
||
export class CreateBuildingDto { | ||
@ApiProperty({ | ||
type: String, | ||
description: 'Building Name', | ||
example: '대학 A동', | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@ApiProperty({ | ||
type: String, | ||
description: 'Building Name in english', | ||
example: 'College A', | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
enName: string; | ||
|
||
@ApiProperty({ | ||
type: String, | ||
description: 'GPS info (lat, lon)', | ||
example: '(35.229695, 126.844536)', | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
@Matches(/^\((-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)\)$/, { | ||
message: 'GPS must be in format (latitude, longitude)', | ||
}) | ||
gps: string; | ||
|
||
@ApiProperty({ | ||
type: String, | ||
description: 'Building Code', | ||
example: 'N4', | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
code: string; | ||
} |
Oops, something went wrong.