diff --git a/src/common/enums/messages.enum.ts b/src/common/enums/messages.enum.ts index ad48f34..bd7efa1 100644 --- a/src/common/enums/messages.enum.ts +++ b/src/common/enums/messages.enum.ts @@ -43,3 +43,7 @@ export const enum UploaderMessage { UPLOAD_FILE_TOO_LARGE = '[MAX_FILE_SIZE] فایل بزرگتر از حد مجاز است', UPLOAD_FILE_TYPE_NOT_SUPPORTED = '[MIME_TYPES] نوع فایل مجاز نیست', } + +export const enum Notificationmessage { + NOTIFICATION_NOT_FOUND = 'اعلان مورد نظر پیدا نشد!' +} \ No newline at end of file diff --git a/src/modules/notifications/notification.controller.ts b/src/modules/notifications/notification.controller.ts index 7d38a03..ac8e659 100644 --- a/src/modules/notifications/notification.controller.ts +++ b/src/modules/notifications/notification.controller.ts @@ -1,4 +1,49 @@ -import { Controller } from "@nestjs/common"; - -@Controller('notification') -export class NotificationController {} \ No newline at end of file +import { + Body, + Controller, + Delete, + Get, + Param, + Patch, + Post, + } from '@nestjs/common'; + import { NotificationService } from './providers/notification.service'; + import { CreateNotificationDto } from './DTO/create-notification.dto'; + import { UpdateNotificationDto } from './DTO/update-notification.dto'; + import { AppNotification } from './entities/notification.entity'; + + @Controller('notification') + export class NotificationController { + constructor( + private readonly notificationService: NotificationService, + ) {} + + @Post() + async create( + @Body() dto: CreateNotificationDto, + ): Promise { + return this.notificationService.create(dto); + } + + @Get(':id') + async findOne( + @Param('id') id: string, + ): Promise { + return this.notificationService.findOneOrFail(id); + } + + @Patch(':id') + async update( + @Param('id') id: string, + @Body() dto: UpdateNotificationDto, + ): Promise { + return this.notificationService.update(id, dto); + } + + @Delete(':id') + async remove( + @Param('id') id: string, + ): Promise { + return this.notificationService.remove(id); + } + } \ No newline at end of file diff --git a/src/modules/notifications/providers/notification.service.ts b/src/modules/notifications/providers/notification.service.ts index 5f95fd6..60e6f24 100644 --- a/src/modules/notifications/providers/notification.service.ts +++ b/src/modules/notifications/providers/notification.service.ts @@ -1,9 +1,36 @@ -import { Injectable } from "@nestjs/common"; -import { NotificationRepository } from "../repositories/notification.repository"; +import { Injectable, NotFoundException } from '@nestjs/common'; +import { NotificationRepository } from '../repositories/notification.repository'; +import { AppNotification } from '../entities/notification.entity'; +import { CreateNotificationDto } from '../DTO/create-notification.dto'; +import { Notificationmessage } from 'src/common/enums/messages.enum'; +import { UpdateNotificationDto } from '../DTO/update-notification.dto'; @Injectable() export class NotificationService { - constructor( - private readonly notifRepository: NotificationRepository - ) {} -} \ No newline at end of file + constructor(private readonly notifRepository: NotificationRepository) {} + + async create(dto: CreateNotificationDto): Promise { + const notif = this.notifRepository.create(dto); + await this.notifRepository.save(notif); + return notif; + } + + async update(id: string, dto: UpdateNotificationDto): Promise { + const notif = await this.findOneOrFail(id); + Object.assign(notif, dto); + return await this.notifRepository.save(notif); + } + + async findOneOrFail(id: string): Promise { + const notif = await this.notifRepository.findOne({ where: { id } }); + if (!notif) + throw new NotFoundException(Notificationmessage.NOTIFICATION_NOT_FOUND); + return notif; + } + + async remove(id: string): Promise { + const notif = await this.findOneOrFail(id); + await this.notifRepository.remove(notif); + return notif; + } +}