|
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + InternalServerErrorException, |
| 4 | + NotFoundException, |
| 5 | +} from '@nestjs/common'; |
| 6 | +import { Model } from 'mongoose'; |
| 7 | +import { InjectModel } from '@nestjs/mongoose'; |
| 8 | +import { AnnouncementDocument as Announcement } from './schema/announcement.schema'; |
| 9 | +import { CreateAnnouncementDTO } from './dto/create-announcement.dto'; |
| 10 | +import { UpdateAnnouncementDTO } from './dto/update-announcement.dto'; |
| 11 | + |
| 12 | +@Injectable() |
| 13 | +export class AnnouncementService { |
| 14 | + constructor( |
| 15 | + @InjectModel('Announcement') |
| 16 | + private readonly AnnouncementModel: Model<Announcement>, |
| 17 | + ) {} |
| 18 | + |
| 19 | + // fetch all Announcements |
| 20 | + async getAllAnnouncement(): Promise<Announcement[]> { |
| 21 | + try { |
| 22 | + const Announcements = await this.AnnouncementModel.find().exec(); |
| 23 | + return Announcements; |
| 24 | + } catch (e) { |
| 25 | + throw new InternalServerErrorException(e); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Get a single Announcement |
| 30 | + async getAnnouncement(announcementId): Promise<Announcement> { |
| 31 | + try { |
| 32 | + const Announcement = await this.AnnouncementModel.findById( |
| 33 | + announcementId, |
| 34 | + ); |
| 35 | + if (Announcement) { |
| 36 | + return Announcement; |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + throw new InternalServerErrorException(e); |
| 40 | + } |
| 41 | + |
| 42 | + throw new NotFoundException('Announcement not found'); |
| 43 | + } |
| 44 | + |
| 45 | + // post a single Announcement |
| 46 | + async addAnnouncement( |
| 47 | + CreateAnnouncementDTO: CreateAnnouncementDTO, |
| 48 | + ): Promise<Announcement> { |
| 49 | + try { |
| 50 | + const newAnnouncement = await new this.AnnouncementModel( |
| 51 | + CreateAnnouncementDTO, |
| 52 | + ); |
| 53 | + await newAnnouncement.save(); |
| 54 | + return newAnnouncement; |
| 55 | + } catch (e) { |
| 56 | + throw new InternalServerErrorException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Edit Announcement details |
| 61 | + async updateAnnouncement( |
| 62 | + announcementId, |
| 63 | + updateAnnouncementDTO: UpdateAnnouncementDTO, |
| 64 | + ): Promise<Announcement> { |
| 65 | + try { |
| 66 | + const updatedAnnouncement = |
| 67 | + await this.AnnouncementModel.findByIdAndUpdate( |
| 68 | + announcementId, |
| 69 | + updateAnnouncementDTO, |
| 70 | + { new: true }, |
| 71 | + ); |
| 72 | + if (updatedAnnouncement) { |
| 73 | + return updatedAnnouncement; |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + throw new InternalServerErrorException(e); |
| 77 | + } |
| 78 | + throw new NotFoundException('Announcement not found!'); |
| 79 | + } |
| 80 | + |
| 81 | + // Delete a Announcement |
| 82 | + async deleteAnnouncement(announcementId): Promise<any> { |
| 83 | + try { |
| 84 | + const deletedAnnouncement = |
| 85 | + await this.AnnouncementModel.findByIdAndRemove(announcementId); |
| 86 | + if (deletedAnnouncement) { |
| 87 | + return deletedAnnouncement; |
| 88 | + } |
| 89 | + throw new NotFoundException('Announcement not found!'); |
| 90 | + } catch (e) { |
| 91 | + throw new InternalServerErrorException(e); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments