Skip to content

Commit

Permalink
already added method to create a question with an array of topics
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanEspindola committed Feb 7, 2023
1 parent 52ffcd5 commit a7d1f82
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 32 deletions.
7 changes: 0 additions & 7 deletions server/src/models/Question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ export class Question {
@ManyToMany(() => Topic)
@JoinTable({
name: "topics_questions",
// joinColumn: {
// name: "topic_id",
// },
// inverseJoinColumn: {
// name: "question_id",
// },
// })
})
topics: Topic[];
}
7 changes: 0 additions & 7 deletions server/src/models/Topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ export class Topic {
@ManyToMany(() => Question)
@JoinTable({
name: "topics_questions",
// joinColumn: {
// name: "topic_id",
// },
// inverseJoinColumn: {
// name: "question_id",
// },
// })
})
questions: Question[];
}
4 changes: 2 additions & 2 deletions server/src/question/dtos/create-question.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export class CreateQuestionDto {
@IsNotEmpty({ message: "question name is required" })
name: string;

@IsNotEmpty({ message: "name of category is required" })
topic: string;
@IsNotEmpty({ message: "name of topics is required" })
topics: string[];
}
4 changes: 2 additions & 2 deletions server/src/question/question.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Body, Controller, Get, Post, ValidationPipe } from "@nestjs/common";
import { HttpStatus } from "@nestjs/common/enums";
import { HttpException } from "@nestjs/common/exceptions";
import { CreateQuestionDto } from "./dtos/create-question.dto";
import { QuestionService } from "./question.service";

Expand All @@ -17,8 +19,6 @@ export class QuestionController {
async createQuestion(
@Body(ValidationPipe) createQuestion: CreateQuestionDto,
) {
console.log(createQuestion);

const question = await this.questionService.createQuestion(createQuestion);
return question;
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/question/question.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Question, Topic } from "src/models";
import { TopicService } from "src/topic/topic.service";
import { QuestionController } from "./question.controller";
import { QuestionService } from "./question.service";

@Module({
imports: [TypeOrmModule.forFeature([Question, Topic])],
controllers: [QuestionController],
providers: [QuestionService],
providers: [QuestionService, TopicService],
})
export class QuestionModule {}
23 changes: 11 additions & 12 deletions server/src/question/question.service.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Question, Topic } from "src/models";
import { Repository } from "typeorm";
import { Question } from "src/models";
import { CreateQuestionDto } from "./dtos/create-question.dto";
import { IQuestion } from "./question";
import { TopicService } from "src/topic/topic.service";

@Injectable()
export class QuestionService implements IQuestion {
constructor(
@InjectRepository(Question)
private questionRespository: Repository<Question>,
@InjectRepository(Topic)
private topicRepository: Repository<Topic>,
private topicService: TopicService,
) {}

async findAllQuestions(): Promise<Array<Question>> {
async findAllQuestions(): Promise<Question[]> {
const questions = await this.questionRespository.find({
relations: { topics: true },
});

return questions;
}

async createQuestion(questionDto: CreateQuestionDto): Promise<Question> {
const topic = await this.topicRepository.findOneBy({
name: questionDto.topic,
});

if (!topic) throw new NotFoundException("Topic not found");
const topics = await this.topicService.findTopicsByNames(
questionDto.topics,
);

const question = await this.questionRespository.create({
const question = this.questionRespository.create({
questionName: questionDto.name,
topics: [topic],
topics: topics,
});
return await this.questionRespository.save(question);
}
Expand Down
14 changes: 13 additions & 1 deletion server/src/topic/topic.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Topic } from "src/models";
Expand All @@ -15,6 +15,18 @@ export class TopicService implements ITopic {
const topics = await this.topicRepostory.find({
relations: { questions: true },
});

return topics;
}

async findTopicsByNames(names: string[]): Promise<Topic[]> {
const topics = await this.topicRepostory
.createQueryBuilder("topic")
.where("topic.name in (:...names)", { names })
.getMany();

if (topics.length === 0) throw new NotFoundException("Topics not found");

return topics;
}

Expand Down
1 change: 1 addition & 0 deletions server/src/topic/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { CreateTopicDto } from "./dtos/create-topic.dto";

export interface ITopic {
findAllTopics: () => Promise<Topic[]>;
findTopicsByNames: (names: string[]) => Promise<Topic[]>;
createTopic: (craeteTopicDto: CreateTopicDto) => Promise<Topic>;
}

0 comments on commit a7d1f82

Please sign in to comment.