Skip to content

Commit 38209be

Browse files
committed
Add user contoller, user module, user service, question controller, question module, and questions service
1 parent ff7a75d commit 38209be

File tree

10 files changed

+244
-47
lines changed

10 files changed

+244
-47
lines changed

backend/question-service/src/app.controller.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

backend/question-service/src/app.controller.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Module } from '@nestjs/common';
2-
import { AppController } from './app.controller';
3-
import { AppService } from './app.service';
2+
import { MongooseModule } from '@nestjs/mongoose';
3+
import { QuestionsModule } from './questions/questions.module';
44

55
@Module({
6-
imports: [],
7-
controllers: [AppController],
8-
providers: [AppService],
6+
imports: [
7+
MongooseModule.forRoot('mongodb://127.0.0.1/PeerPrep'),
8+
QuestionsModule,
9+
],
910
})
1011
export class AppModule {}

backend/question-service/src/app.service.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
IsBoolean,
3+
IsNotEmpty,
4+
IsOptional,
5+
IsEnum,
6+
IsString,
7+
ValidateNested,
8+
} from 'class-validator';
9+
import { Type } from 'class-transformer';
10+
11+
enum Category {
12+
Algorithm = 'Algorithm',
13+
DynamicProgramming = 'DynamicProgramming',
14+
Array = 'Array',
15+
SQL = 'SQL',
16+
Heap = 'Heap',
17+
Recursion = 'Recursion',
18+
Graph = 'Graph',
19+
Sorting = 'Sorting',
20+
}
21+
22+
enum Difficulty {
23+
Easy = 'easy',
24+
Medium = 'medium',
25+
Hard = 'hard',
26+
}
27+
28+
export class CreateQuestionDto {
29+
@IsNotEmpty()
30+
@IsString()
31+
title: string;
32+
33+
@IsNotEmpty()
34+
@IsString()
35+
description: string;
36+
'';
37+
@IsNotEmpty()
38+
@IsEnum(Category, {
39+
message:
40+
'Category must be Algorithm, DynamicProgramming, Array, SQL, Heap, Recursion, Graph, Sorting',
41+
})
42+
category: Category;
43+
44+
@IsNotEmpty()
45+
@IsEnum(Difficulty, {
46+
message: 'Difficulty must be easy, medium, hard',
47+
})
48+
difficulty: Difficulty;
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
IsBoolean,
3+
IsNotEmpty,
4+
IsOptional,
5+
IsEnum,
6+
IsString,
7+
ValidateNested,
8+
} from 'class-validator';
9+
import { Type } from 'class-transformer';
10+
11+
enum Category {
12+
Algorithm = 'Algoritm',
13+
DynamicProgramming = 'DynamicProgramming',
14+
Array = 'Array',
15+
SQL = 'SQL',
16+
Heap = 'Heap',
17+
Recursion = 'Recursion',
18+
Graph = 'Graph',
19+
Sorting = 'Sorting',
20+
}
21+
22+
enum Difficulty {
23+
Easy = 'easy',
24+
Medium = 'medium',
25+
Hard = 'hard',
26+
}
27+
28+
export class UpdateQuestionDto {
29+
@IsNotEmpty()
30+
@IsString()
31+
title: string;
32+
33+
@IsNotEmpty()
34+
@IsString()
35+
description: string;
36+
37+
@IsNotEmpty()
38+
@IsEnum(Category, {
39+
message:
40+
'Category must be Algorithm, DynamicProgramming, Array, SQL, Heap, Recursion, Graph, Sorting',
41+
})
42+
category: Category;
43+
44+
@IsNotEmpty()
45+
@IsEnum(Difficulty, {
46+
message: 'Difficulty must be easy, medium, hard',
47+
})
48+
difficulty: Difficulty;
49+
}
50+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import mongoose from 'mongoose';
2+
import {
3+
Controller,
4+
Post,
5+
Body,
6+
Get,
7+
Param,
8+
HttpException,
9+
Patch,
10+
Delete,
11+
} from '@nestjs/common';
12+
import { QuestionsService } from './questions.service';
13+
import { CreateQuestionDto } from '../dto/CreateQuestion.dto';
14+
import { UpdateQuestionDto } from '../dto/UpdateQuestion.dto';
15+
16+
@Controller('questions')
17+
export class QuestionsController {
18+
constructor(private questionsService: QuestionsService) {}
19+
20+
@Post()
21+
createQuestion(@Body() CreateQuestionDto: CreateQuestionDto) {
22+
return this.questionsService.createQuestion(CreateQuestionDto);
23+
}
24+
25+
@Get()
26+
getQuestions() {
27+
return this.questionsService.getQuestions();
28+
}
29+
30+
@Get(':id')
31+
async getQuestionsById(@Param('id') id: string) {
32+
const isValid = mongoose.Types.ObjectId.isValid(id);
33+
if (!isValid) throw new HttpException('Question not found', 404);
34+
const findQuestion = await this.questionsService.getQuestionsById(id);
35+
if (!findQuestion) throw new HttpException('Question not found', 404);
36+
return findQuestion;
37+
}
38+
39+
@Patch(':id')
40+
async updateQuestion(
41+
@Param('id') id: string,
42+
@Body() UpdateQuestionDto: UpdateQuestionDto,
43+
) {
44+
const isValid = mongoose.Types.ObjectId.isValid(id);
45+
if (!isValid) throw new HttpException('Invalid ID', 400);
46+
const updateQuestion = await this.questionsService.updateQuestion(
47+
id,
48+
UpdateQuestionDto,
49+
);
50+
if (!updateQuestion) throw new HttpException('Question Not Found', 404);
51+
return updateQuestion;
52+
}
53+
54+
@Delete(':id')
55+
async deleteQuestion(@Param('id') id: string) {
56+
const isValid = mongoose.Types.ObjectId.isValid(id);
57+
if (!isValid) throw new HttpException('Invalid ID', 400);
58+
const deletedUser = await this.questionsService.deleteQuestion(id);
59+
if (!deletedUser) throw new HttpException('Question not Found', 404);
60+
return;
61+
}
62+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Module } from '@nestjs/common';
2+
import { MongooseModule } from '@nestjs/mongoose';
3+
import { Question, QuestionSchema } from '../schemas/Question.schema';
4+
import { QuestionsService } from './questions.service';
5+
import { QuestionsController } from './questions.controller';
6+
7+
@Module({
8+
imports: [
9+
MongooseModule.forFeature([
10+
{
11+
name: Question.name,
12+
schema: QuestionSchema,
13+
},
14+
]),
15+
],
16+
providers: [QuestionsService],
17+
controllers: [QuestionsController],
18+
})
19+
export class QuestionsModule {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CreateQuestionDto } from '../dto/CreateQuestion.dto';
3+
import { Question } from '../schemas/Question.schema';
4+
import { InjectModel } from '@nestjs/mongoose';
5+
import { Model } from 'mongoose';
6+
import { UpdateQuestionDto } from '../dto/UpdateQuestion.dto';
7+
8+
@Injectable()
9+
export class QuestionsService {
10+
constructor(
11+
@InjectModel(Question.name) private questionModel: Model<Question>,
12+
) {}
13+
14+
async createQuestion(createQuestionDto: CreateQuestionDto) {
15+
const newQuestion = new this.questionModel(createQuestionDto);
16+
return newQuestion.save();
17+
}
18+
19+
getQuestions() {
20+
return this.questionModel.find();
21+
}
22+
23+
getQuestionsById(id: string) {
24+
return this.questionModel.findById(id);
25+
}
26+
27+
updateQuestion(id: string, updateUserDto: UpdateQuestionDto) {
28+
return this.questionModel.findByIdAndUpdate(id, updateUserDto, {
29+
new: true,
30+
});
31+
}
32+
33+
deleteQuestion(id: string) {
34+
return this.questionModel.findByIdAndDelete(id);
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
2+
import mongoose from 'mongoose';
3+
4+
@Schema()
5+
export class Question {
6+
@Prop({ unique: true })
7+
id: string;
8+
9+
@Prop({ unique: true, required: true })
10+
title: string;
11+
12+
@Prop({ required: true })
13+
description: string;
14+
15+
@Prop({ required: true })
16+
category: string;
17+
18+
@Prop({ required: true })
19+
difficulty: string;
20+
}
21+
22+
export const QuestionSchema = SchemaFactory.createForClass(Question)

0 commit comments

Comments
 (0)