feat: added notification service and controller

This commit is contained in:
2026-08-01 00:32:18 +03:30
parent 366ab5092e
commit 1ead663ddf
3 changed files with 86 additions and 10 deletions
+4
View File
@@ -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 = 'اعلان مورد نظر پیدا نشد!'
}
@@ -1,4 +1,49 @@
import { Controller } from "@nestjs/common";
@Controller('notification')
export class NotificationController {}
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<AppNotification> {
return this.notificationService.create(dto);
}
@Get(':id')
async findOne(
@Param('id') id: string,
): Promise<AppNotification> {
return this.notificationService.findOneOrFail(id);
}
@Patch(':id')
async update(
@Param('id') id: string,
@Body() dto: UpdateNotificationDto,
): Promise<AppNotification> {
return this.notificationService.update(id, dto);
}
@Delete(':id')
async remove(
@Param('id') id: string,
): Promise<AppNotification> {
return this.notificationService.remove(id);
}
}
@@ -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
) {}
}
constructor(private readonly notifRepository: NotificationRepository) {}
async create(dto: CreateNotificationDto): Promise<AppNotification> {
const notif = this.notifRepository.create(dto);
await this.notifRepository.save(notif);
return notif;
}
async update(id: string, dto: UpdateNotificationDto): Promise<AppNotification> {
const notif = await this.findOneOrFail(id);
Object.assign(notif, dto);
return await this.notifRepository.save(notif);
}
async findOneOrFail(id: string): Promise<AppNotification> {
const notif = await this.notifRepository.findOne({ where: { id } });
if (!notif)
throw new NotFoundException(Notificationmessage.NOTIFICATION_NOT_FOUND);
return notif;
}
async remove(id: string): Promise<AppNotification> {
const notif = await this.findOneOrFail(id);
await this.notifRepository.remove(notif);
return notif;
}
}