-
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.
- Loading branch information
1 parent
7cc0c1c
commit 46535a7
Showing
9 changed files
with
294 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as moment from 'moment'; | ||
import * as momentTz from "moment-timezone"; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello(): string { | ||
return `Hello POPO! (popo-${process.env.POPO_VERSION})`; | ||
return `Hello POPO! (popo-${process.env.POPO_VERSION}) (server now: ${moment().format('YYYY-MM-DD HH:mm:ss')}, KST now: ${momentTz().tz("Asia/Seoul").format('YYYY-MM-DD HH:mm:ss')})`; | ||
} | ||
} |
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,86 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Patch, | ||
Post, | ||
Put, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiBody, ApiTags } from '@nestjs/swagger'; | ||
import * as moment from 'moment'; | ||
|
||
import { NoticeService } from './notice.service'; | ||
import { NoticeDto, NoticeImageDto } from './notice.dto'; | ||
import { UserType } from '../user/user.meta'; | ||
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | ||
import { Roles } from '../../auth/authroization/roles.decorator'; | ||
import { RolesGuard } from '../../auth/authroization/roles.guard'; | ||
import { FileService } from '../../file/file.service'; | ||
import { FileBody } from '../../file/file-body.decorator'; | ||
|
||
@ApiTags('Notice') | ||
@Controller('notice') | ||
export class NoticeController { | ||
constructor( | ||
private readonly noticeService: NoticeService, | ||
private readonly fileService: FileService, | ||
) {} | ||
|
||
@Post() | ||
@Roles(UserType.admin, UserType.association) | ||
@UseGuards(JwtAuthGuard, RolesGuard) | ||
@ApiBody({ type: NoticeDto }) | ||
async create(@Body() dto: NoticeDto) { | ||
return this.noticeService.save(dto); | ||
} | ||
|
||
@Post('image/:id') | ||
@Roles(UserType.admin, UserType.association) | ||
@UseGuards(JwtAuthGuard, RolesGuard) | ||
@FileBody('image') | ||
async uploadImage(@Param('id') id: number, @Body() dto: NoticeImageDto) { | ||
const image_url = await this.fileService.uploadFile( | ||
`notice/${id}/${moment().format('YYYY-MM-DD/HH:mm:ss')}`, | ||
dto.image, | ||
); | ||
await this.noticeService.updateImageUrl(id, image_url); | ||
return image_url; | ||
} | ||
|
||
@Get() | ||
getAll() { | ||
return this.noticeService.find(); | ||
} | ||
|
||
@Get('active') | ||
getAllActive() { | ||
return this.noticeService.findActive(); | ||
} | ||
|
||
@Get(':id') | ||
async getOne(@Param('id') id: number) { | ||
return this.noticeService.findOneById(id); | ||
} | ||
|
||
@Patch('click/:id') | ||
increaseClickCount(@Param('id') id: number) { | ||
return this.noticeService.increaseClickCount(id); | ||
} | ||
|
||
@Put(':id') | ||
@Roles(UserType.admin, UserType.association, UserType.staff) | ||
@UseGuards(JwtAuthGuard, RolesGuard) | ||
async put(@Param('id') id: number, @Body() updateNoticeDto: NoticeDto) { | ||
return this.noticeService.update(id, updateNoticeDto); | ||
} | ||
|
||
@Delete(':id') | ||
@Roles(UserType.admin, UserType.association) | ||
@UseGuards(JwtAuthGuard, RolesGuard) | ||
async delete(@Param('id') id: number) { | ||
this.noticeService.remove(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,15 @@ | ||
import { IsFile, MaxFileSize, MemoryStoredFile } from 'nestjs-form-data'; | ||
|
||
export class NoticeDto { | ||
readonly title: string; | ||
readonly content: string | null; | ||
readonly start_datetime: string | null; // YYYY-MM-DD HH:mm:ss (KST) | ||
readonly end_datetime: string | null; // YYYY-MM-DD HH:mm:ss (KST) | ||
readonly link: string | null; | ||
} | ||
|
||
export class NoticeImageDto { | ||
@IsFile() | ||
@MaxFileSize(10 * 1024 * 1024) // 10MB | ||
readonly image: MemoryStoredFile; | ||
} |
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,41 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity() | ||
export class Notice extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ nullable: false }) | ||
title: string; | ||
|
||
@Column('text', { nullable: true }) | ||
content: string; | ||
|
||
@Column({ nullable: true }) | ||
image_url: string; | ||
|
||
@Column({ nullable: true }) | ||
link: string; | ||
|
||
@Column({ nullable: false }) | ||
start_datetime: string; // YYYY-MM-DD HH:mm:ss (KST) | ||
|
||
@Column({ nullable: false }) | ||
end_datetime: string; // YYYY-MM-DD HH:mm:ss (KST) | ||
|
||
@Column({ default: 0}) | ||
click_count: number; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updateAt: Date; | ||
} |
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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { NestjsFormDataModule } from 'nestjs-form-data'; | ||
|
||
import { NoticeController } from './notice.controller'; | ||
import { NoticeService } from './notice.service'; | ||
import { Notice } from './notice.entity'; | ||
import { FileModule } from '../../file/file.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Notice]), | ||
NestjsFormDataModule, | ||
FileModule, | ||
], | ||
controllers: [NoticeController], | ||
providers: [NoticeService], | ||
exports: [NoticeService], | ||
}) | ||
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,81 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { LessThan, MoreThanOrEqual, Repository } from 'typeorm'; | ||
import * as moment from "moment-timezone"; | ||
|
||
import { Notice } from './notice.entity'; | ||
import { NoticeDto } from './notice.dto'; | ||
|
||
const Message = { | ||
NOT_EXISTING_REGION: "There's no such region.", | ||
NOT_EXISTING_USER: "There's no such user.", | ||
NOT_EXISTING_PLACE: "There's no such notice.", | ||
INVALID_OWNER: 'Only Association can have a notice.', | ||
INVALID_STAFF: 'Only Staff and ADMIN can be a manager.', | ||
}; | ||
|
||
@Injectable() | ||
export class NoticeService { | ||
constructor( | ||
@InjectRepository(Notice) | ||
private readonly noticeRepo: Repository<Notice>, | ||
) {} | ||
|
||
save(dto: NoticeDto) { | ||
return this.noticeRepo.save(dto); | ||
} | ||
|
||
updateImageUrl(id: number, image_url: string) { | ||
return this.noticeRepo.update({ id: id }, { image_url: image_url }); | ||
} | ||
|
||
find() { | ||
return this.noticeRepo.find({ order: { updateAt: 'DESC' } }); | ||
} | ||
|
||
findActive() { | ||
const now = moment().tz("Asia/Seoul").format('YYYY-MM-DD HH:mm:ss'); | ||
return this.noticeRepo.find({ | ||
where: { start_datetime: LessThan(now), end_datetime: MoreThanOrEqual(now) } | ||
}); | ||
} | ||
|
||
findOneById(id: number) { | ||
return this.noticeRepo.findOneBy({ id: id }); | ||
} | ||
|
||
findOneByIdOrFail(id: number) { | ||
const notice = this.findOneById(id); | ||
if (!notice) { | ||
throw new BadRequestException(Message.NOT_EXISTING_PLACE); | ||
} | ||
return notice; | ||
} | ||
|
||
async update(id: number, dto: NoticeDto) { | ||
const existNotice = await this.findOneById(id); | ||
if (!existNotice) { | ||
throw new BadRequestException(Message.NOT_EXISTING_PLACE); | ||
} | ||
|
||
return this.noticeRepo.update({ id: id }, dto); | ||
} | ||
|
||
async increaseClickCount(id: number) { | ||
const notice = await this.noticeRepo.findOneByOrFail({ id: id }); | ||
return this.noticeRepo.update( | ||
{ id: id }, | ||
{ click_count: notice.click_count + 1 }, | ||
); | ||
} | ||
|
||
async remove(id: number) { | ||
const existNotice = await this.findOneById(id); | ||
|
||
if (!existNotice) { | ||
throw new BadRequestException(Message.NOT_EXISTING_PLACE); | ||
} | ||
|
||
return this.noticeRepo.delete({ id: 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