Skip to content

Commit

Permalink
storedURLをstoredIdに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 29, 2024
1 parent 9f8e65d commit e6b6b30
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions Documents/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ API is provided by Firebase Cloud Functions. Database is provided by Firestore.
"text": "hoge fuga",
"post": {
"userId": "Vlx6GCtq90ag3lxgh0pcCKGp5ba0",
"storedURL": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"storedId": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"text": "数学の勉強したよ^^",
"submittedAt": {
"_seconds": 1735603199,
Expand Down Expand Up @@ -151,14 +151,14 @@ API is provided by Firebase Cloud Functions. Database is provided by Firestore.
- Body (form-data)
- goalId: string
- text: string
- storedURL: string (画像のストレージパス、/post/{storedURL}/image)
- storedId: string (画像のストレージパス、/post/{storedId}/image)
- submittedAt: Date
- Example
```json
{
"goalId": "RXlHJiv3GtpzSDHhfljS",
"text": "今日は勉強をがんばった",
"storedURL": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"storedId": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"submittedAt": "2024-12-31T23:59:59.000Z"
}
```
Expand All @@ -182,7 +182,7 @@ API is provided by Firebase Cloud Functions. Database is provided by Firestore.
"goalId": "9fgWJA6wMN54EkxIC2WD",
"userId": "IK0Zc2hoUYaYjXoqzmCl",
"text": "今日は勉強をがんばった",
"storedURL": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"storedId": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"goalId": "RXlHJiv3GtpzSDHhfljS",
"submittedAt": "2024-12-31T23:59:59.000Z"
}
Expand Down Expand Up @@ -215,7 +215,7 @@ Use Create Post API to update post.
"text": "Duolingoやる",
"post": {
"text": "フランス語したよ",
"storedURL": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"storedId": "0f9a84ed-8ae8-44b0-a6f5-5ac5ca517948",
"submittedAt": "2024-12-28T09:45:10.718Z"
},
"userData": {
Expand Down
22 changes: 11 additions & 11 deletions functions/src/routers/postRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.get("/", async (req: Request, res: Response) => {
goalId: goalDoc.id,
userId: goalData.userId,
text: goalData.post.text,
storedURL: goalData.post.storedURL,
storedId: goalData.post.storedId,
submittedAt: goalData.post.submittedAt.toDate(),
});
}
Expand Down Expand Up @@ -69,7 +69,7 @@ router.get("/:userId", async (req: Request, res: Response) => {
goalId: goalDoc.id,
userId: goalData.userId,
text: goalData.post.text,
storedURL: goalData.post.storedURL,
storedId: goalData.post.storedId,
submittedAt: goalData.post.submittedAt.toDate(),
});
}
Expand All @@ -86,19 +86,19 @@ router.get("/:userId", async (req: Request, res: Response) => {
router.post("/", async (req: Request, res: Response) => {
let goalId: PostWithGoalId["goalId"];
let text: PostWithGoalId["text"];
let storedURL: PostWithGoalId["storedURL"];
let storedId: PostWithGoalId["storedId"];
let submittedAt: PostWithGoalId["submittedAt"];

try {
({ goalId, text = "", storedURL, submittedAt } = req.body);
({ goalId, text = "", storedId, submittedAt } = req.body);
} catch (error) {
logger.error(error);
return res.status(400).json({ message: "Invalid request body" });
}

if (!goalId || !storedURL || !submittedAt) {
if (!goalId || !storedId || !submittedAt) {
return res.status(400).json({
message: "userId, storedURL, goalId, and submittedAt are required",
message: "userId, storedId, goalId, and submittedAt are required",
});
}

Expand All @@ -119,7 +119,7 @@ router.post("/", async (req: Request, res: Response) => {
await goalRef.update({
post: {
text,
storedURL,
storedId,
submittedAt: new Date(submittedAt),
},
});
Expand Down Expand Up @@ -150,13 +150,13 @@ router.delete("/:goalId", async (req: Request, res: Response) => {
}

// Storageから画像を削除
const storedURL = goalDoc.data()?.post?.storedURL;
if (storedURL) {
const storedId = goalDoc.data()?.post?.storedId;
if (storedId) {
try {
const bucket = admin.storage().bucket();
const file = bucket.file(`post/${storedURL}`);
const file = bucket.file(`post/${storedId}`);
await file.delete();
logger.info("Image deleted successfully:", storedURL);
logger.info("Image deleted successfully:", storedId);
} catch (error) {
logger.error("Error deleting image:", error);
return res.status(500).json({ message: "Error deleting image" });
Expand Down
2 changes: 1 addition & 1 deletion functions/src/routers/resultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const getResults = async (
text: data.text,
post: post && {
text: post.text,
storedURL: post.storedURL,
storedId: post.storedId,
submittedAt: post.submittedAt.toDate(),
},
};
Expand Down
2 changes: 1 addition & 1 deletion functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface GoalWithIdAndUserData extends Goal {

export interface Post {
userId?: string;
storedURL: string;
storedId: string;
text: string;
submittedAt: Date;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/PostModal/PostModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function PostModal({
async (url, id) => {
const postData: PostWithGoalId = {
userId: user?.userId as string,
storedURL: id,
storedId: id,
text: text,
goalId: goalId,
submittedAt: new Date(),
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Progress/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const SuccessStep = ({
}

const storage = getStorage();
const imageRef = ref(storage, `post/${post.storedURL}`);
const imageRef = ref(storage, `post/${post.storedId}`);

getDownloadURL(imageRef)
.then((url) => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface GoalWithIdAndUserData extends Goal {

export interface Post {
userId: string;
storedURL: string;
storedId: string;
text: string;
submittedAt: Date | string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/API/User/fetchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const fetchUserById = async (userId: string): Promise<User> => {
* @return {*}
*/
export const handleFetchUserError = (error: unknown) => {
let snackBarMessage = "ユーザー情報の取得に失敗しました";
let snackBarMessage = "初回ログインかユーザーデータが見つかりません";

if (error instanceof Error) {
console.error("Fetch error:", error.message);
Expand Down

0 comments on commit e6b6b30

Please sign in to comment.