Skip to content

Commit f2d0d60

Browse files
authoredJul 17, 2021
Added announcements endpoints with swagger doc completed for it (#103)
* added announcements endpoints with swagger doc completed for it * removed stuff that was not needed and fixed camelCasing * add created_by * fix a small issue of required * rsolve conflicts
1 parent 61f2542 commit f2d0d60

11 files changed

+355
-1
lines changed
 

‎.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
src/config/example.service.json
1+
src/config/example.service.json
2+
src/config/service.json

‎src/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ChatModule } from './modules/chat/chat.module';
1111
import * as path from 'path';
1212
import { CourseModule } from './modules/course/course.module';
1313
import { DoubtModule } from './modules/doubt/doubt.module';
14+
import { AnnouncementModule } from 'modules/announcements/announcement.module';
1415
import { MentorModule } from './modules/mentor/mentor.module';
1516

1617
import * as dotenv from 'dotenv';
@@ -29,6 +30,7 @@ dotenv.config({ path: path.resolve(__dirname, '..', '.env') });
2930
CourseModule,
3031
ChatModule,
3132
DoubtModule,
33+
AnnouncementModule,
3234
MentorModule,
3335
],
3436
controllers: [AppController],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Put,
7+
Delete,
8+
Param,
9+
} from '@nestjs/common';
10+
import { AnnouncementService } from './announcement.service'; //eslint-disable-line
11+
import { CreateAnnouncementDTO } from './dto/create-announcement.dto'; //eslint-disable-line
12+
import {
13+
ApiCreatedResponse,
14+
ApiOkResponse,
15+
ApiParam,
16+
ApiTags,
17+
} from '@nestjs/swagger';
18+
import { UpdateAnnouncementDTO } from './dto/update-announcement.dto';
19+
import responsedoc from './docUtils/apidoc';
20+
import { announcementId } from './docUtils/announcement.paramdocs';
21+
22+
@ApiTags('Announcement')
23+
@Controller('Announcement')
24+
export class AnnouncementController {
25+
constructor(private announcementService: AnnouncementService) {}
26+
27+
// add a Announcement
28+
@Post()
29+
@ApiCreatedResponse(responsedoc.addAnnouncement)
30+
async addAnnouncement(@Body() CreateAnnouncementDTO: CreateAnnouncementDTO) {
31+
return await this.announcementService.addAnnouncement(
32+
CreateAnnouncementDTO,
33+
);
34+
}
35+
36+
// Retrieve Announcements list
37+
@Get()
38+
@ApiOkResponse(responsedoc.getAllAnnouncement)
39+
async getAllAnnouncement() {
40+
return await this.announcementService.getAllAnnouncement();
41+
}
42+
43+
// Fetch a particular Announcement using ID
44+
@Get('/:announcementId')
45+
@ApiParam(announcementId)
46+
@ApiOkResponse(responsedoc.getAnnouncement)
47+
async getAnnouncement(@Param('announcementId') announcementId: string) {
48+
return await this.announcementService.getAnnouncement(announcementId);
49+
}
50+
51+
@Put('/:announcementId')
52+
@ApiParam(announcementId)
53+
@ApiOkResponse(responsedoc.updateAnnouncement)
54+
async updateAnnouncement(
55+
@Param('announcementId') announcementId: string,
56+
@Body() updateAnnouncementDTO: UpdateAnnouncementDTO,
57+
) {
58+
return await this.announcementService.updateAnnouncement(
59+
announcementId,
60+
updateAnnouncementDTO,
61+
);
62+
}
63+
64+
// Delete a Announcement
65+
@Delete('/:announcementId')
66+
@ApiParam(announcementId)
67+
@ApiOkResponse(responsedoc.deleteAnnouncement)
68+
async deleteAnnouncement(@Param('announcementId') announcementId: string) {
69+
return await this.announcementService.deleteAnnouncement(announcementId);
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Module } from '@nestjs/common';
2+
import { AnnouncementController } from './announcement.controller';
3+
import { AnnouncementService } from './announcement.service';
4+
import { MongooseModule } from '@nestjs/mongoose';
5+
import { AnnouncementSchema } from './schema/announcement.schema';
6+
@Module({
7+
imports: [
8+
MongooseModule.forFeature([
9+
{ name: 'Announcement', schema: AnnouncementSchema },
10+
]),
11+
],
12+
controllers: [AnnouncementController],
13+
providers: [AnnouncementService],
14+
})
15+
export class AnnouncementModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ApiParamOptions } from '@nestjs/swagger';
2+
3+
export const announcementId: ApiParamOptions = {
4+
name: 'announcementId',
5+
type: String,
6+
description: 'Announcement Id in the form of MongoId',
7+
example: '60ccf3758dc53371bd4d0154',
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default class AnnouncementResponseBody {
2+
/**
3+
* The title of the announcement
4+
* @example 'New course on Web Development'
5+
*/
6+
title: string;
7+
8+
/**
9+
* The description of the announcement
10+
* @example 'The wait is finally over as teh new course on Web Development has been released. The course will take you through the basics and help you learn and make projects along the way'
11+
*/
12+
description: string;
13+
14+
/**
15+
* Whether the announcement was read or not
16+
* @example true
17+
*/
18+
read: boolean;
19+
20+
/**
21+
* Creator of the announcement
22+
* @example 'John Doe'
23+
*/
24+
created_by: string;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import AnnouncementResponseBody from './announcement.responsedoc';
2+
import { ApiResponseOptions } from '@nestjs/swagger';
3+
4+
const getAllAnnouncement: ApiResponseOptions = {
5+
description: 'Get all the courses available in the database',
6+
type: [AnnouncementResponseBody],
7+
};
8+
9+
const getAnnouncement: ApiResponseOptions = {
10+
description: 'Get course by id from the database',
11+
type: AnnouncementResponseBody,
12+
};
13+
14+
const addAnnouncement: ApiResponseOptions = {
15+
description: 'Add a course',
16+
type: AnnouncementResponseBody,
17+
};
18+
19+
const updateAnnouncement: ApiResponseOptions = {
20+
description: 'Update a course',
21+
type: AnnouncementResponseBody,
22+
};
23+
24+
const deleteAnnouncement: ApiResponseOptions = {
25+
description: 'Delete a course',
26+
type: AnnouncementResponseBody,
27+
};
28+
29+
const responses = {
30+
getAllAnnouncement,
31+
getAnnouncement,
32+
addAnnouncement,
33+
updateAnnouncement,
34+
deleteAnnouncement,
35+
};
36+
37+
export default responses;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
2+
3+
export class CreateAnnouncementDTO {
4+
/**
5+
* The title of the announcement
6+
* @example 'New course on Web Development'
7+
*/
8+
@IsNotEmpty()
9+
@IsString()
10+
title: string;
11+
12+
/**
13+
* The description of the announcement
14+
* @example 'The wait is finally over as teh new course on Web Development has been released. The course will take you through the basics and help you learn and make projects along the way'
15+
*/
16+
@IsNotEmpty()
17+
@IsString()
18+
description: string;
19+
20+
/**
21+
* Whether the announcement was read or not
22+
* @example true
23+
*/
24+
@IsNotEmpty()
25+
@IsBoolean()
26+
read: boolean;
27+
28+
/**
29+
* Creator of the announcement
30+
* @example 'John Doe'
31+
*/
32+
@IsNotEmpty()
33+
@IsString()
34+
created_by: string;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IsNotEmpty, IsString, IsBoolean } from 'class-validator';
2+
3+
export class UpdateAnnouncementDTO {
4+
/**
5+
* The title of the announcement
6+
* @example 'New course on Web Development'
7+
*/
8+
@IsNotEmpty()
9+
@IsString()
10+
title: string;
11+
12+
/**
13+
* The description of the announcement
14+
* @example 'The wait is finally over as teh new course on Web Development has been released. The course will take you through the basics and help you learn and make projects along the way'
15+
*/
16+
@IsNotEmpty()
17+
@IsString()
18+
description: string;
19+
20+
/**
21+
* Whether the announcement was read or not
22+
* @example true
23+
*/
24+
@IsNotEmpty()
25+
@IsBoolean()
26+
read: boolean;
27+
28+
/**
29+
* Creator of the announcement
30+
* @example 'John Doe'
31+
*/
32+
@IsNotEmpty()
33+
@IsString()
34+
created_by: string;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
2+
import { Document } from 'mongoose';
3+
4+
export type AnnouncementDocument = Announcement & Document;
5+
6+
@Schema({ timestamps: true })
7+
export class Announcement {
8+
@Prop({ required: true })
9+
title: string;
10+
11+
@Prop({ required: true })
12+
description: string;
13+
14+
@Prop({ default: false })
15+
read: boolean;
16+
17+
@Prop({ required: true })
18+
created_by: string;
19+
}
20+
21+
export const AnnouncementSchema = SchemaFactory.createForClass(Announcement);
22+
23+
AnnouncementSchema.methods.toJSON = function () {
24+
const announcementObject = this.toObject();
25+
announcementObject.id = announcementObject._id;
26+
27+
delete announcementObject.__v;
28+
delete announcementObject._id;
29+
30+
return announcementObject;
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.