-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db72a9f
commit 8ecea74
Showing
7 changed files
with
191 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import express, { Request, Response } from "express"; | ||
import admin from "firebase-admin"; | ||
import { GoalWithId, SuccessResult } from "./types"; | ||
|
||
const router = express.Router(); | ||
const db = admin.firestore(); | ||
|
||
const getResults = async (limit: number, offset: number, userId?: string) => { | ||
let goalQuery = db.collection("goal").limit(limit).offset(offset); | ||
if (userId) { | ||
goalQuery = goalQuery.where("userId", "==", userId); | ||
} | ||
const goalSnapshot = await goalQuery.get(); | ||
|
||
const goals = goalSnapshot.docs.map((doc) => { | ||
const data = doc.data(); | ||
return { | ||
goalId: doc.id, | ||
userId: data.userId, | ||
deadline: new Date(data.deadline._seconds * 1000), | ||
text: data.text, | ||
}; | ||
}) as GoalWithId[]; | ||
|
||
if (!goals || goals.length === 0) { | ||
return { successResults: [], failedPosts: [] }; | ||
} | ||
|
||
const postSnapshot = await db | ||
.collection("post") | ||
.where( | ||
"goalId", | ||
"in", | ||
goals.map((goal) => goal.goalId) | ||
) | ||
.get(); | ||
|
||
const successResults: SuccessResult[] = []; | ||
const failedPosts: GoalWithId[] = []; | ||
goals.forEach((goal) => { | ||
const post = postSnapshot.docs.find( | ||
(doc) => doc.data().goalId === goal.goalId | ||
); | ||
if (post) { | ||
const postData = post.data(); | ||
const submittedAt = postData.submittedAt.toDate(); | ||
if (submittedAt < goal.deadline) { | ||
failedPosts.push(goal); | ||
} else { | ||
successResults.push({ | ||
userId: goal.userId, | ||
goalId: goal.goalId, | ||
postId: post.id, | ||
goalText: goal.text, | ||
postText: postData.text, | ||
storedId: postData.storedId, | ||
deadline: goal.deadline, | ||
submittedAt: submittedAt, | ||
}); | ||
} | ||
} else { | ||
failedPosts.push(goal); | ||
} | ||
}); | ||
|
||
return { successResults, failedPosts }; | ||
}; | ||
|
||
// GET: 全ての目標に対する結果を取得 | ||
router.get("/", async (req: Request, res: Response) => { | ||
try { | ||
const limit = req.query.limit ? Number(req.query.limit) : 10; // 取得数 | ||
const offset = req.query.offset ? Number(req.query.offset) : 0; // 開始位置 | ||
if (offset < 0) { | ||
res.status(400).json({ message: "Offset must be a positive number" }); | ||
} | ||
if (limit < 1) { | ||
res.status(400).json({ message: "Limit must be more than zero" }); | ||
} | ||
|
||
const results = await getResults(limit, offset); | ||
res.json(results); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}); | ||
|
||
// GET: userIdから結果を取得 | ||
router.get("/:userId", async (req: Request, res: Response) => { | ||
const userId = req.params.userId; | ||
|
||
if (!userId) { | ||
res.status(400).json({ message: "User ID is required" }); | ||
} | ||
|
||
try { | ||
const limit = req.query.limit ? Number(req.query.limit) : 10; // 取得数 | ||
const offset = req.query.offset ? Number(req.query.offset) : 0; // 開始位置 | ||
if (offset < 0) { | ||
res.status(400).json({ message: "Offset must be a positive number" }); | ||
} | ||
if (limit < 1) { | ||
res.status(400).json({ message: "Limit must be more than zero" }); | ||
} | ||
|
||
const results = await getResults(limit, offset, userId); | ||
res.json(results); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export interface User { | ||
name: string; | ||
streak: number; | ||
} | ||
|
||
export interface Goal { | ||
userId: string; | ||
deadline: Date; | ||
text: string; | ||
} | ||
|
||
export interface GoalWithId extends Goal { | ||
goalId: string; | ||
} | ||
|
||
export interface Post { | ||
userId: string; | ||
storedId: string; | ||
text: string; | ||
goalId: string; | ||
submittedAt: Date; | ||
} | ||
|
||
export interface SuccessResult { | ||
userId: string; | ||
goalId: string; | ||
postId: string; | ||
goalText: string; | ||
postText: string; | ||
storedId: string; | ||
deadline: Date; | ||
submittedAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters