Skip to content

Commit 625e190

Browse files
authored
Merge pull request #251 from boostcampwm-2024/dev
[Deploy] 5주차 스프린트 배포 - 2
2 parents ba85b80 + 8e068a7 commit 625e190

File tree

89 files changed

+1642
-814
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+1642
-814
lines changed

backend/src/question-list/question-list.controller.ts

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import {
2-
Body,
3-
Controller,
4-
Get,
5-
Post,
6-
Req,
7-
Res,
8-
UseGuards,
9-
} from "@nestjs/common";
1+
import { Body, Controller, Delete, Get, Param, Post, Req, Res, UseGuards } from "@nestjs/common";
102
import { QuestionListService } from "./question-list.service";
113
import { CreateQuestionListDto } from "./dto/create-question-list.dto";
12-
import { CreateQuestionDto } from "./dto/create-question.dto";
134
import { GetAllQuestionListDto } from "./dto/get-all-question-list.dto";
145
import { QuestionListContentsDto } from "./dto/question-list-contents.dto";
156
import { AuthGuard } from "@nestjs/passport";
16-
import { JwtPayload } from "../auth/jwt/jwt.decorator";
17-
import { IJwtPayload } from "../auth/jwt/jwt.model";
7+
import { JwtPayload } from "@/auth/jwt/jwt.decorator";
8+
import { IJwtPayload } from "@/auth/jwt/jwt.model";
189
import { MyQuestionListDto } from "./dto/my-question-list.dto";
1910

2011
@Controller("question-list")
@@ -70,9 +61,7 @@ export class QuestionListController {
7061

7162
// 질문지 생성
7263
const { createdQuestionList, createdQuestions } =
73-
await this.questionListService.createQuestionList(
74-
createQuestionListDto
75-
);
64+
await this.questionListService.createQuestionList(createQuestionListDto);
7665

7766
return res.send({
7867
success: true,
@@ -102,9 +91,7 @@ export class QuestionListController {
10291
try {
10392
const { categoryName } = body;
10493
const allQuestionLists: GetAllQuestionListDto[] =
105-
await this.questionListService.getAllQuestionListsByCategoryName(
106-
categoryName
107-
);
94+
await this.questionListService.getAllQuestionListsByCategoryName(categoryName);
10895
return res.send({
10996
success: true,
11097
message: "All question lists received successfully.",
@@ -132,9 +119,7 @@ export class QuestionListController {
132119
try {
133120
const { questionListId } = body;
134121
const questionListContents: QuestionListContentsDto =
135-
await this.questionListService.getQuestionListContents(
136-
questionListId
137-
);
122+
await this.questionListService.getQuestionListContents(questionListId);
138123
return res.send({
139124
success: true,
140125
message: "Question list contents received successfully.",
@@ -173,4 +158,92 @@ export class QuestionListController {
173158
});
174159
}
175160
}
161+
162+
@Get("scrap")
163+
@UseGuards(AuthGuard("jwt"))
164+
async getScrappedQuestionLists(@Res() res, @JwtPayload() token: IJwtPayload) {
165+
try {
166+
const userId = token.userId;
167+
const scrappedQuestionLists =
168+
await this.questionListService.getScrappedQuestionLists(userId);
169+
return res.send({
170+
success: true,
171+
message: "Scrapped question lists received successfully.",
172+
data: {
173+
scrappedQuestionLists,
174+
},
175+
});
176+
} catch (error) {
177+
return res.send({
178+
success: false,
179+
message: "Failed to get scrapped question lists.",
180+
error: error.message,
181+
});
182+
}
183+
}
184+
185+
@Post("scrap")
186+
@UseGuards(AuthGuard("jwt"))
187+
async scrapQuestionList(
188+
@Res() res,
189+
@JwtPayload() token: IJwtPayload,
190+
@Body() body: { questionListId: number }
191+
) {
192+
try {
193+
const userId = token.userId;
194+
const { questionListId } = body;
195+
const scrappedQuestionList = await this.questionListService.scrapQuestionList(
196+
questionListId,
197+
userId
198+
);
199+
200+
return res.send({
201+
success: true,
202+
message: "Question list is scrapped successfully.",
203+
data: {
204+
scrappedQuestionList,
205+
},
206+
});
207+
} catch (error) {
208+
return res.send({
209+
success: false,
210+
message: "Failed to scrap question list.",
211+
error: error.message,
212+
});
213+
}
214+
}
215+
216+
@Delete("scrap/:questionListId")
217+
@UseGuards(AuthGuard("jwt"))
218+
async unscrapQuestionList(
219+
@Res() res,
220+
@JwtPayload() token: IJwtPayload,
221+
@Param("questionListId") questionListId: number
222+
) {
223+
try {
224+
const userId = token.userId;
225+
const unscrappedQuestionList = await this.questionListService.unscrapQuestionList(
226+
questionListId,
227+
userId
228+
);
229+
230+
if (unscrappedQuestionList.affected) {
231+
return res.send({
232+
success: true,
233+
message: "Question list unscrapped successfully.",
234+
});
235+
} else {
236+
return res.send({
237+
success: false,
238+
message: "Failed to unscrap question list.",
239+
});
240+
}
241+
} catch (error) {
242+
return res.send({
243+
success: false,
244+
message: "Failed to unscrap question list.",
245+
error: error.message,
246+
});
247+
}
248+
}
176249
}

backend/src/question-list/question-list.entity.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ManyToMany,
88
JoinTable,
99
} from "typeorm";
10-
import { User } from "../user/user.entity";
10+
import { User } from "@/user/user.entity";
1111
import { Question } from "./question.entity";
1212
import { Category } from "./category.entity";
1313

@@ -30,21 +30,29 @@ export class QuestionList {
3030
@Column()
3131
userId: number;
3232

33-
@ManyToOne(() => User, (user) => user.questionLists)
33+
@ManyToOne(() => User, (user) => user.questionLists, {
34+
onDelete: "CASCADE",
35+
})
3436
user: User;
3537

36-
@OneToMany(() => Question, (question) => question.questionList)
38+
@OneToMany(() => Question, (question) => question.questionList, {
39+
cascade: true,
40+
})
3741
questions: Question[];
3842

3943
@ManyToMany(() => Category, (category) => category.questionLists, {
4044
cascade: true,
45+
onDelete: "CASCADE",
4146
})
4247
@JoinTable({
4348
name: "question_list_category",
4449
joinColumn: {
45-
name: "questionListId",
50+
name: "question_list_id",
4651
referencedColumnName: "id",
4752
},
4853
})
4954
categories: Category[];
55+
56+
@ManyToMany(() => User, (user) => user.scrappedQuestionLists)
57+
scrappedByUsers: User[];
5058
}

backend/src/question-list/question-list.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { Module } from "@nestjs/common";
22
import { QuestionListController } from "./question-list.controller";
33
import { QuestionListService } from "./question-list.service";
44
import { QuestionListRepository } from "./question-list.repository";
5+
import { UserRepository } from "@/user/user.repository";
56

67
@Module({
78
controllers: [QuestionListController],
8-
providers: [QuestionListService, QuestionListRepository],
9+
providers: [QuestionListService, QuestionListRepository, UserRepository],
910
exports: [QuestionListRepository],
1011
})
1112
export class QuestionListModule {}

backend/src/question-list/question-list.repository.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import { DataSource, In } from "typeorm";
33
import { QuestionList } from "./question-list.entity";
44
import { Question } from "./question.entity";
55
import { Category } from "./category.entity";
6-
import { User } from "../user/user.entity";
6+
import { User } from "@/user/user.entity";
77

88
@Injectable()
99
export class QuestionListRepository {
1010
constructor(private dataSource: DataSource) {}
1111

12+
createQuestionList(questionList: QuestionList) {
13+
return this.dataSource.getRepository(QuestionList).save(questionList);
14+
}
15+
16+
async createQuestions(questions: Question[]) {
17+
return this.dataSource.getRepository(Question).save(questions);
18+
}
19+
1220
findPublicQuestionLists() {
1321
return this.dataSource.getRepository(QuestionList).find({
1422
where: { isPublic: true },
@@ -86,4 +94,33 @@ export class QuestionListRepository {
8694
})
8795
.getCount();
8896
}
97+
98+
scrapQuestionList(questionListId: number, userId: number) {
99+
return this.dataSource
100+
.createQueryBuilder()
101+
.insert()
102+
.into("user_question_list")
103+
.values({
104+
user_id: userId,
105+
question_list_id: questionListId,
106+
})
107+
.orIgnore()
108+
.execute();
109+
}
110+
111+
getScrappedQuestionListsByUser(user: User) {
112+
return this.dataSource.getRepository(QuestionList).find({
113+
where: { scrappedByUsers: user },
114+
});
115+
}
116+
117+
unscrapQuestionList(questionListId: number, userId: number) {
118+
return this.dataSource
119+
.createQueryBuilder()
120+
.delete()
121+
.from("user_question_list")
122+
.where("user_id = :userId", { userId })
123+
.andWhere("question_list_id = :questionListId", { questionListId })
124+
.execute();
125+
}
89126
}

0 commit comments

Comments
 (0)