-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add notice module * feat: Add get notices by study id api
- Loading branch information
Showing
14 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
src/database/migrations/20230822074102_add_notice/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,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "Notice" ( | ||
"id" SERIAL NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"content" TEXT NOT NULL, | ||
"studyId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "Notice_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notice" ADD CONSTRAINT "Notice_studyId_fkey" FOREIGN KEY ("studyId") REFERENCES "Study"("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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNumber, IsDate, IsString } from 'class-validator'; | ||
import { Notice } from '@prisma/client'; | ||
|
||
export class CreateNoticeResponseDto { | ||
@ApiProperty() | ||
@IsNumber() | ||
readonly id: number; | ||
|
||
@ApiProperty() | ||
@IsDate() | ||
readonly createdAt: Date | ||
|
||
@ApiProperty() | ||
@IsDate() | ||
readonly updatedAt: Date | ||
|
||
@ApiProperty() | ||
@IsString() | ||
readonly content: String | ||
|
||
@ApiProperty() | ||
@IsNumber() | ||
readonly studyId: number; | ||
|
||
constructor(id: number, createdAt: Date, updatedAt: Date, content: string, studyId: number) { | ||
this.id = id; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
this.content = content; | ||
this.studyId = studyId; | ||
} | ||
|
||
static fromNotice(notice: Notice) { | ||
return new CreateNoticeResponseDto(notice.id, notice.createdAt, notice.updatedAt, notice.content, notice.studyId); | ||
} | ||
} |
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,12 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNumber, IsString } from 'class-validator'; | ||
|
||
export class CreateNoticeDto { | ||
@ApiProperty() | ||
@IsString() | ||
readonly content: string; | ||
|
||
@ApiProperty() | ||
@IsNumber() | ||
readonly studyId: number; | ||
} |
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,37 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNumber, IsDate, IsString } from 'class-validator'; | ||
import { Notice } from '@prisma/client'; | ||
|
||
export class GetNoticeResponseDto { | ||
@ApiProperty() | ||
@IsNumber() | ||
readonly id: number; | ||
|
||
@ApiProperty() | ||
@IsDate() | ||
readonly createdAt: Date | ||
|
||
@ApiProperty() | ||
@IsDate() | ||
readonly updatedAt: Date | ||
|
||
@ApiProperty() | ||
@IsString() | ||
readonly content: String | ||
|
||
@ApiProperty() | ||
@IsNumber() | ||
readonly studyId: number; | ||
|
||
constructor(id: number, createdAt: Date, updatedAt: Date, content: string, studyId: number) { | ||
this.id = id; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
this.content = content; | ||
this.studyId = studyId; | ||
} | ||
|
||
static fromNotice(notice: Notice) { | ||
return new GetNoticeResponseDto(notice.id, notice.createdAt, notice.updatedAt, notice.content, notice.studyId); | ||
} | ||
} |
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,24 @@ | ||
import { Notice } from "@prisma/client"; | ||
import { CreateNoticeResponseDto } from "./create-notice-response.dto"; | ||
|
||
export class UpdateNoticeResponseDto extends CreateNoticeResponseDto{ | ||
constructor( | ||
id: number, | ||
createdAt: Date, | ||
updatedAt: Date, | ||
content: string, | ||
studyId: number | ||
) { | ||
super(id, createdAt, updatedAt, content, studyId); | ||
} | ||
|
||
static fromNotice(notice: Notice) { | ||
return new UpdateNoticeResponseDto( | ||
notice.id, | ||
notice.createdAt, | ||
notice.updatedAt, | ||
notice.content, | ||
notice.studyId | ||
); | ||
} | ||
} |
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,4 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
import { CreateNoticeDto } from './create-notice.dto'; | ||
|
||
export class UpdateNoticeDto extends PartialType(CreateNoticeDto) {} |
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,29 @@ | ||
import { Controller, Get, Post, Patch, Delete, Param, Body } from '@nestjs/common'; | ||
import { NoticeService } from './notice.service'; | ||
import { CreateNoticeDto } from './dto/create-notice.dto'; | ||
import { UpdateNoticeDto } from './dto/update-notice.dto'; | ||
|
||
@Controller() | ||
export class NoticeController { | ||
constructor(private readonly noticeService: NoticeService) {} | ||
|
||
@Get() | ||
findAll() { | ||
return this.noticeService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: string) { | ||
return this.noticeService.findOne(+id); | ||
} | ||
|
||
@Post() | ||
create(@Body() createNoticeDto: CreateNoticeDto) { | ||
return this.noticeService.create(createNoticeDto); | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id') id: string, @Body() updateNoticeDto: UpdateNoticeDto) { | ||
return this.noticeService.update(+id, updateNoticeDto); | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { NoticeController } from './notice.controller'; | ||
import { NoticeService } from './notice.service'; | ||
import { PrismaModule } from 'src/database/prisma.module'; | ||
|
||
@Module({ | ||
controllers: [NoticeController], | ||
providers: [NoticeService], | ||
imports : [PrismaModule], | ||
}) | ||
export class NoticeModule {} |
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,67 @@ | ||
import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common'; | ||
import { PrismaService } from 'src/database/prisma.service'; | ||
import { GetNoticeResponseDto } from './dto/get-notice-response.dto'; | ||
import { CreateNoticeResponseDto } from './dto/create-notice-response.dto'; | ||
import { CreateNoticeDto } from './dto/create-notice.dto'; | ||
import { UpdateNoticeDto } from './dto/update-notice.dto'; | ||
import { UpdateNoticeResponseDto } from './dto/update-notice-response.dto'; | ||
import { StudiumException } from 'src/common/studium-exception'; | ||
|
||
@Injectable() | ||
export class NoticeService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async findAll(): Promise<GetNoticeResponseDto[]> { | ||
const notices = await this.prisma.notice.findMany(); | ||
return notices.map((notice) => GetNoticeResponseDto.fromNotice(notice)); | ||
} | ||
|
||
async findOne(id: number): Promise<GetNoticeResponseDto> { | ||
if (isNaN(id)) { | ||
throw new BadRequestException(StudiumException.idFormatError); | ||
} | ||
const notice = await this.prisma.notice.findUnique({ where: { id } }) | ||
|
||
if (!notice) { | ||
throw new InternalServerErrorException(StudiumException.dataNotFound); | ||
} | ||
|
||
return GetNoticeResponseDto.fromNotice(notice); | ||
} | ||
|
||
async create(createNoticeDto: CreateNoticeDto): Promise<CreateNoticeResponseDto> { | ||
const {studyId, ...data} = createNoticeDto; | ||
const notice = await this.prisma.notice.create({ | ||
data: { | ||
study: { connect: {id: studyId } }, | ||
...data | ||
}, | ||
include: { | ||
study: true, | ||
} | ||
}); | ||
return CreateNoticeResponseDto.fromNotice(notice); | ||
} | ||
|
||
async update(id: number, updateNoticeDto: UpdateNoticeDto): Promise<UpdateNoticeResponseDto> { | ||
const { studyId, ...data } = updateNoticeDto; | ||
|
||
const notice = await this.prisma.notice.update({ | ||
where: { id }, | ||
data: { | ||
...data | ||
} | ||
}); | ||
|
||
return UpdateNoticeResponseDto.fromNotice(notice); | ||
} | ||
|
||
async remove(id: number) { | ||
const noticeToDelete = await this.prisma.notice.findUnique({ | ||
where: { id }, | ||
include: { study: true }, | ||
}); | ||
|
||
return await this.prisma.notice.delete({ where: { id: noticeToDelete.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
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