Skip to content

Commit

Permalink
Merge pull request #251 from boostcampwm-2024/dev
Browse files Browse the repository at this point in the history
[Deploy] 5주차 스프린트 배포 - 2
  • Loading branch information
moozeh authored Nov 27, 2024
2 parents ba85b80 + 8e068a7 commit 625e190
Show file tree
Hide file tree
Showing 89 changed files with 1,642 additions and 814 deletions.
115 changes: 94 additions & 21 deletions backend/src/question-list/question-list.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import {
Body,
Controller,
Get,
Post,
Req,
Res,
UseGuards,
} from "@nestjs/common";
import { Body, Controller, Delete, Get, Param, Post, Req, Res, UseGuards } from "@nestjs/common";
import { QuestionListService } from "./question-list.service";
import { CreateQuestionListDto } from "./dto/create-question-list.dto";
import { CreateQuestionDto } from "./dto/create-question.dto";
import { GetAllQuestionListDto } from "./dto/get-all-question-list.dto";
import { QuestionListContentsDto } from "./dto/question-list-contents.dto";
import { AuthGuard } from "@nestjs/passport";
import { JwtPayload } from "../auth/jwt/jwt.decorator";
import { IJwtPayload } from "../auth/jwt/jwt.model";
import { JwtPayload } from "@/auth/jwt/jwt.decorator";
import { IJwtPayload } from "@/auth/jwt/jwt.model";
import { MyQuestionListDto } from "./dto/my-question-list.dto";

@Controller("question-list")
Expand Down Expand Up @@ -70,9 +61,7 @@ export class QuestionListController {

// 질문지 생성
const { createdQuestionList, createdQuestions } =
await this.questionListService.createQuestionList(
createQuestionListDto
);
await this.questionListService.createQuestionList(createQuestionListDto);

return res.send({
success: true,
Expand Down Expand Up @@ -102,9 +91,7 @@ export class QuestionListController {
try {
const { categoryName } = body;
const allQuestionLists: GetAllQuestionListDto[] =
await this.questionListService.getAllQuestionListsByCategoryName(
categoryName
);
await this.questionListService.getAllQuestionListsByCategoryName(categoryName);
return res.send({
success: true,
message: "All question lists received successfully.",
Expand Down Expand Up @@ -132,9 +119,7 @@ export class QuestionListController {
try {
const { questionListId } = body;
const questionListContents: QuestionListContentsDto =
await this.questionListService.getQuestionListContents(
questionListId
);
await this.questionListService.getQuestionListContents(questionListId);
return res.send({
success: true,
message: "Question list contents received successfully.",
Expand Down Expand Up @@ -173,4 +158,92 @@ export class QuestionListController {
});
}
}

@Get("scrap")
@UseGuards(AuthGuard("jwt"))
async getScrappedQuestionLists(@Res() res, @JwtPayload() token: IJwtPayload) {
try {
const userId = token.userId;
const scrappedQuestionLists =
await this.questionListService.getScrappedQuestionLists(userId);
return res.send({
success: true,
message: "Scrapped question lists received successfully.",
data: {
scrappedQuestionLists,
},
});
} catch (error) {
return res.send({
success: false,
message: "Failed to get scrapped question lists.",
error: error.message,
});
}
}

@Post("scrap")
@UseGuards(AuthGuard("jwt"))
async scrapQuestionList(
@Res() res,
@JwtPayload() token: IJwtPayload,
@Body() body: { questionListId: number }
) {
try {
const userId = token.userId;
const { questionListId } = body;
const scrappedQuestionList = await this.questionListService.scrapQuestionList(
questionListId,
userId
);

return res.send({
success: true,
message: "Question list is scrapped successfully.",
data: {
scrappedQuestionList,
},
});
} catch (error) {
return res.send({
success: false,
message: "Failed to scrap question list.",
error: error.message,
});
}
}

@Delete("scrap/:questionListId")
@UseGuards(AuthGuard("jwt"))
async unscrapQuestionList(
@Res() res,
@JwtPayload() token: IJwtPayload,
@Param("questionListId") questionListId: number
) {
try {
const userId = token.userId;
const unscrappedQuestionList = await this.questionListService.unscrapQuestionList(
questionListId,
userId
);

if (unscrappedQuestionList.affected) {
return res.send({
success: true,
message: "Question list unscrapped successfully.",
});
} else {
return res.send({
success: false,
message: "Failed to unscrap question list.",
});
}
} catch (error) {
return res.send({
success: false,
message: "Failed to unscrap question list.",
error: error.message,
});
}
}
}
16 changes: 12 additions & 4 deletions backend/src/question-list/question-list.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ManyToMany,
JoinTable,
} from "typeorm";
import { User } from "../user/user.entity";
import { User } from "@/user/user.entity";
import { Question } from "./question.entity";
import { Category } from "./category.entity";

Expand All @@ -30,21 +30,29 @@ export class QuestionList {
@Column()
userId: number;

@ManyToOne(() => User, (user) => user.questionLists)
@ManyToOne(() => User, (user) => user.questionLists, {
onDelete: "CASCADE",
})
user: User;

@OneToMany(() => Question, (question) => question.questionList)
@OneToMany(() => Question, (question) => question.questionList, {
cascade: true,
})
questions: Question[];

@ManyToMany(() => Category, (category) => category.questionLists, {
cascade: true,
onDelete: "CASCADE",
})
@JoinTable({
name: "question_list_category",
joinColumn: {
name: "questionListId",
name: "question_list_id",
referencedColumnName: "id",
},
})
categories: Category[];

@ManyToMany(() => User, (user) => user.scrappedQuestionLists)
scrappedByUsers: User[];
}
3 changes: 2 additions & 1 deletion backend/src/question-list/question-list.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from "@nestjs/common";
import { QuestionListController } from "./question-list.controller";
import { QuestionListService } from "./question-list.service";
import { QuestionListRepository } from "./question-list.repository";
import { UserRepository } from "@/user/user.repository";

@Module({
controllers: [QuestionListController],
providers: [QuestionListService, QuestionListRepository],
providers: [QuestionListService, QuestionListRepository, UserRepository],
exports: [QuestionListRepository],
})
export class QuestionListModule {}
39 changes: 38 additions & 1 deletion backend/src/question-list/question-list.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { DataSource, In } from "typeorm";
import { QuestionList } from "./question-list.entity";
import { Question } from "./question.entity";
import { Category } from "./category.entity";
import { User } from "../user/user.entity";
import { User } from "@/user/user.entity";

@Injectable()
export class QuestionListRepository {
constructor(private dataSource: DataSource) {}

createQuestionList(questionList: QuestionList) {
return this.dataSource.getRepository(QuestionList).save(questionList);
}

async createQuestions(questions: Question[]) {
return this.dataSource.getRepository(Question).save(questions);
}

findPublicQuestionLists() {
return this.dataSource.getRepository(QuestionList).find({
where: { isPublic: true },
Expand Down Expand Up @@ -86,4 +94,33 @@ export class QuestionListRepository {
})
.getCount();
}

scrapQuestionList(questionListId: number, userId: number) {
return this.dataSource
.createQueryBuilder()
.insert()
.into("user_question_list")
.values({
user_id: userId,
question_list_id: questionListId,
})
.orIgnore()
.execute();
}

getScrappedQuestionListsByUser(user: User) {
return this.dataSource.getRepository(QuestionList).find({
where: { scrappedByUsers: user },
});
}

unscrapQuestionList(questionListId: number, userId: number) {
return this.dataSource
.createQueryBuilder()
.delete()
.from("user_question_list")
.where("user_id = :userId", { userId })
.andWhere("question_list_id = :questionListId", { questionListId })
.execute();
}
}
Loading

0 comments on commit 625e190

Please sign in to comment.