Skip to content

Commit fbee0c5

Browse files
committed
タグ追加
1 parent 3ea877e commit fbee0c5

File tree

5 files changed

+238
-144
lines changed

5 files changed

+238
-144
lines changed

server/src/database/interest.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ export async function get(id: number): Promise<InterestSubject | null> {
99
return await prisma.interestSubject.findUnique({ where: { id } });
1010
}
1111

12+
export async function create(name: string): Promise<InterestSubject> {
13+
const existingTag = await prisma.interestSubject.findMany({
14+
where: { name },
15+
});
16+
if (existingTag.length > 0) {
17+
throw new Error("同名のタグがすでに存在します");
18+
}
19+
return await prisma.interestSubject.create({
20+
data: {
21+
name,
22+
group: "", // TODO: 運用次第
23+
},
24+
});
25+
}
26+
1227
export async function of(userId: UserID): Promise<InterestSubject[]> {
1328
return await prisma.interest
1429
.findMany({
@@ -57,13 +72,3 @@ export async function updateMultipleWithTransaction(
5772
});
5873
});
5974
}
60-
61-
export async function search(query: string): Promise<InterestSubject[]> {
62-
return await prisma.interestSubject.findMany({
63-
where: {
64-
name: {
65-
contains: query,
66-
},
67-
},
68-
});
69-
}

server/src/router/subjects.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ router.get("/mine", async (req: Request, res: Response) => {
3030
}
3131
});
3232

33+
router.post("/", async (req: Request, res: Response) => {
34+
const { name } = req.body;
35+
if (typeof name !== "string") {
36+
return res.status(400).json({ error: "name must be a string" });
37+
}
38+
try {
39+
const newSubject = await interest.create(name);
40+
res.status(201).json(newSubject);
41+
} catch (error) {
42+
console.error("Error creating subject:", error);
43+
res.status(500).json({ error: "Failed to create subject" });
44+
}
45+
});
46+
3347
router.patch("/mine", async (req: Request, res: Response) => {
3448
const userId = await safeGetUserId(req);
3549
if (!userId.ok) return res.status(401).send("auth error");
@@ -104,18 +118,13 @@ router.put("/mine", async (req: Request, res: Response) => {
104118
}
105119
});
106120

107-
router.get("/search/:query", async (req: Request, res: Response) => {
108-
// TODO: token との兼ね合いで、クエリパラメータでなく一旦パスパラメータとしている
109-
const q = req.params.query;
110-
if (typeof q !== "string") {
111-
return res.status(400).json({ error: "Invalid query" });
112-
}
121+
router.get("/all", async (req: Request, res: Response) => {
113122
try {
114-
const subjects = await interest.search(q);
123+
const subjects = await interest.all();
115124
res.status(200).json(subjects);
116125
} catch (error) {
117-
console.error("Error searching subjects:", error);
118-
res.status(500).json({ error: "Failed to search subjects" });
126+
console.error("Error fetching subjects:", error);
127+
res.status(500).json({ error: "Failed to fetch subjects" });
119128
}
120129
});
121130

web/api/internal/endpoints.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ export const coursesDayPeriod = (day: Day, period: number) => {
236236
return `${API_ENDPOINT}/courses/day-period?day=${day}&period=${period}`;
237237
};
238238

239+
/**
240+
* [v] 実装済み
241+
* POST -> create a new subject.
242+
* - statuses:
243+
* - 200: ok.
244+
* - body: InterestSubject
245+
* - 401: unauthorized.
246+
* - 500: internal error.
247+
*/
248+
export const subjects = `${API_ENDPOINT}/subjects`;
249+
239250
/**
240251
* [v] 実装済み
241252
* GET -> get subjects the user is interested in.
@@ -282,18 +293,15 @@ export const subjectsUserId = (userId: UserID) => {
282293
export const subjectsMine = `${API_ENDPOINT}/subjects/mine`;
283294

284295
/**
285-
* [] 実装済み
286-
* GET -> search subjects.
296+
* [v] 実装済み
297+
* GET -> get all subjects.
287298
* - statuses:
288-
* - 200: ok.
289-
* - body: InterestSubject[]
290-
* - 401: unauthorized.
291-
* - 500: internal error.
292-
**/
293-
export const subjectsSearch = (query: string) => {
294-
// TODO: token との兼ね合いで、クエリパラメータでなく一旦パスパラメータとしている
295-
return `${API_ENDPOINT}/subjects/search/${query}`;
296-
};
299+
* - 200: ok.
300+
* - body: InterestSubject[]
301+
* - 401: unauthorized.
302+
* - 500: internal error.
303+
*/
304+
export const subjectsAll = `${API_ENDPOINT}/subjects/all`;
297305

298306
/**
299307
* [v] 実装済み
@@ -459,9 +467,10 @@ export default {
459467
cancelRequest,
460468
coursesUserId,
461469
coursesDayPeriod,
470+
subjects,
462471
subjectsUserId,
463472
subjectsMine,
464-
subjectsSearch,
473+
subjectsAll,
465474
roomOverview,
466475
dmTo,
467476
dmWith,

web/api/subject.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import endpoints from "./internal/endpoints.ts";
1111

1212
const InterestSubjectListSchema = z.array(InterestSubjectSchema);
1313

14+
// 興味分野を作成する
15+
export async function create(name: string) {
16+
return await credFetch("POST", endpoints.subjects, { name });
17+
}
18+
1419
// 自身の興味分野を取得する
1520
export function useMyInterests(): Hook<InterestSubject[]> {
1621
return useCustomizedSWR(
@@ -26,11 +31,9 @@ async function getMySubjects(): Promise<InterestSubject[]> {
2631
}
2732

2833
// 自身の興味分野を更新する
29-
export async function update(
30-
newSubjectIds: InterestSubjectID[],
31-
): Promise<void> {
34+
export async function update(newSubjectIds: InterestSubjectID[]) {
3235
const url = endpoints.subjectsMine;
33-
await credFetch("PUT", url, { subjectIds: newSubjectIds });
36+
return await credFetch("PUT", url, { subjectIds: newSubjectIds });
3437
}
3538

3639
// 指定した userId のユーザの興味分野を取得
@@ -39,8 +42,8 @@ export async function get(id: UserID): Promise<InterestSubject[] | null> {
3942
return await res.json();
4043
}
4144

42-
// 興味分野を検索
43-
export async function search(q: string): Promise<InterestSubject[]> {
44-
const res = await credFetch("GET", endpoints.subjectsSearch(q));
45+
// すべての興味分野を取得
46+
export async function getAll(): Promise<InterestSubject[]> {
47+
const res = await credFetch("GET", endpoints.subjectsAll);
4548
return await res.json();
4649
}

0 commit comments

Comments
 (0)