Skip to content

Commit

Permalink
result APIの返り値をsuccess, failed, pendingに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 2, 2024
1 parent 34103ff commit 9e4a4e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 54 deletions.
42 changes: 20 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,40 +229,38 @@ API is provided by Firebase Cloud Functions. Database is provided by Firestore.
- URL: /result/:userId
- Empty userId will return all results.
- Parameters
- limit?: number - The maximum number of results to return.(Default is 10)
- limit?: number - The maximum number of results to return.(Default is 50)
- offset?: number - The number of results to skip before starting to collect the result set.
- onlyPast?: boolean - If true, only past results will be returned.
- onlyFuture?: boolean - If true, only future results will be returned.
- Example1: /result/:userId?limit=30&offset=0&onlyPast
- Example2: /result/:userId?onlyFuture
- Method: GET
- Response
```json
{
"successResults": [
{
"userId": "FZ5KQ4sv0wZCow0MdwniPbyeBvVv",
"goalId": "9AQYNhnEA3DyjNKSCU6U",
"postId": "6bUz6SOUn62SJhrWn1Y6",
"goalText": "英語の勉強する",
"postText": "英語の勉強したよ^^",
"userId": "Vlx6GCtq90ag3lxgh0pcCKGp5ba0",
"goalId": "DESiyiEIHFDpuCjo08Si",
"postId": "5KffB5x2SykU6lY9sHGB",
"goalText": "数学の勉強する",
"postText": "数学の勉強したよ^^",
"storedId": "ointtq9NT5TPgEnKS4tb",
"deadline": "2024-12-31T23:59:59.000Z",
"submittedAt": "2024-12-28T23:59:59.000Z"
"deadline": "2025-01-31T23:59:59.000Z",
"submittedAt": "2024-12-30T23:59:59.000Z"
}
],
"failedOrPendingResults": [
"failedResults": [
{
"goalId": "0qVTLfKJ80m50XD5HAOI",
"userId": "FZ5KQ4sv0wZCow0MdwniPbyeBvVv",
"deadline": "2024-12-31T23:59:59.000Z",
"text": "国語の勉強する"
},
"goalId": "cl2wtpf5RufkCL2N8s98",
"userId": "Vlx6GCtq90ag3lxgh0pcCKGp5ba0",
"deadline": "2024-10-30T23:59:59.000Z",
"text": "ちょいと前のやつ"
}
],
"pendingResults": [
{
"goalId": "3HAGWZHFg9GOJYV2sib5",
"userId": "FZ5KQ4sv0wZCow0MdwniPbyeBvVv",
"deadline": "2024-12-31T23:59:59.000Z",
"text": "数学の勉強する"
"goalId": "2CA5Q7JygkXSHUmJZ8B7",
"userId": "Vlx6GCtq90ag3lxgh0pcCKGp5ba0",
"deadline": "2024-12-09T23:59:59.000Z",
"text": "まだまだ先だよ"
}
]
}
Expand Down
44 changes: 12 additions & 32 deletions functions/src/routers/resultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@ import { GoalWithId, SuccessResult } from "./types";
const router = express.Router();
const db = admin.firestore();

const getResults = async (
limit: number,
offset: number,
userId?: string,
onlyPast?: boolean, // 過去の結果のみ取得
onlyFuture?: boolean // 未来の結果のみ取得
) => {
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);
}
// TODO: 日本時間に合っているか確認
if (onlyPast) {
console.log("onlyPast");
goalQuery = goalQuery.where("deadline", "<", new Date());
}
if (onlyFuture) {
console.log("onlyFuture");
goalQuery = goalQuery.where("deadline", ">", new Date());
}
const goalSnapshot = await goalQuery.get();

const goals = goalSnapshot.docs.map((doc) => {
Expand All @@ -38,7 +23,7 @@ const getResults = async (
}) as GoalWithId[];

if (!goals || goals.length === 0) {
return { successResults: [], failedOrPendingResults: [] };
return { successResults: [], failedResults: [], pendingResults: [] };
}

const postSnapshot = await db
Expand All @@ -51,7 +36,8 @@ const getResults = async (
.get();

const successResults: SuccessResult[] = [];
const failedOrPendingResults: GoalWithId[] = [];
const failedResults: GoalWithId[] = [];
const pendingResults: GoalWithId[] = [];
goals.forEach((goal) => {
const post = postSnapshot.docs.find(
(doc) => doc.data().goalId === goal.goalId
Expand All @@ -60,7 +46,7 @@ const getResults = async (
const postData = post.data();
const submittedAt = postData.submittedAt.toDate();
if (submittedAt > goal.deadline) {
failedOrPendingResults.push(goal);
failedResults.push(goal);
} else {
successResults.push({
userId: goal.userId,
Expand All @@ -73,23 +59,23 @@ const getResults = async (
submittedAt: submittedAt,
});
}
} else if (goal.deadline < new Date()) {
// TODO: 日本時間に合ってるか確認
failedResults.push(goal);
} else {
failedOrPendingResults.push(goal);
pendingResults.push(goal);
}
});

return { successResults, failedOrPendingResults };
return { successResults, failedResults, pendingResults };
};

// GET: 全ての目標または特定のユーザーの目標に対する結果を取得
router.get("/:userId?", async (req: Request, res: Response) => {
const userId = req.params.userId;

const limit = req.query.limit ? Number(req.query.limit) : 10; // 取得数
const limit = req.query.limit ? Number(req.query.limit) : 50; // 取得数
const offset = req.query.offset ? Number(req.query.offset) : 0; // 開始位置
const onlyPast = req.query.onlyPast !== undefined; // 過去の結果のみ取得
const onlyFuture = req.query.onlyFuture !== undefined; // 未来の結果のみ取得
console.log(onlyPast, onlyFuture);
if (offset < 0) {
res.status(400).json({ message: "Offset must be a positive number" });
}
Expand All @@ -98,13 +84,7 @@ router.get("/:userId?", async (req: Request, res: Response) => {
}

try {
const results = await getResults(
limit,
offset,
userId,
onlyPast,
onlyFuture
);
const results = await getResults(limit, offset, userId);
res.json(results);
} catch (error) {
console.error(error);
Expand Down

0 comments on commit 9e4a4e4

Please sign in to comment.