Skip to content

Commit

Permalink
Merge pull request #74 from Wonchang0314/develop
Browse files Browse the repository at this point in the history
FIX: 링크를 통한 가게 합류 기능 개선
  • Loading branch information
iOdiO89 authored Apr 29, 2024
2 parents 100066d + 2927c9b commit 0825c13
Show file tree
Hide file tree
Showing 12 changed files with 1,314 additions and 94 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
"lint": "next lint"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.556.0",
"@aws-sdk/util-dynamodb": "^3.556.0",
"@tanstack/react-query": "^5.24.7",
"@tanstack/react-query-devtools": "^5.24.7",
"@types/node": "20.11.1",
"@types/react": "18.2.48",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.14",
"axios": "^1.5.0",
"cors": "^2.8.5",
"dayjs": "^1.11.10",
"eslint": "^8.56.0",
"eslint-config-next": "13.4.13",
Expand Down
10 changes: 10 additions & 0 deletions src/libs/createRandomId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const createRandomString = (num: number) => {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let result = "";
const charactersLength = characters.length;
for (let i = 0; i < num; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
console.log(result);
return result;
};
87 changes: 87 additions & 0 deletions src/libs/dynamoDB/getOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
DeleteItemCommand,
DeleteItemCommandInput,
GetItemCommand,
GetItemCommandInput,
PutItemCommand,
PutItemCommandInput,
QueryCommand,
QueryCommandInput,
QueryCommandOutput,
ScanCommand,
ScanCommandInput,
ScanCommandOutput,
UpdateItemCommand,
UpdateItemCommandInput,
} from '@aws-sdk/client-dynamodb';
import { getDynamoDBClient } from '../network';

async function getScanCommandOutputFromDynamoDB(params: ScanCommandInput) {
const client = getDynamoDBClient();
let data: ScanCommandOutput;

const command = new ScanCommand(params);
data = await client.send(command);

while (true) {
if (data.LastEvaluatedKey) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
} else break;
const tempcommand = new ScanCommand(params);
const tempdata = await client.send(tempcommand);
data.Items = data.Items?.concat(tempdata.Items ?? []);
data.LastEvaluatedKey = tempdata.LastEvaluatedKey;
}

return data;
}

async function getQueryCommandOutputFromDynamoDB(params: QueryCommandInput) {
const client = getDynamoDBClient();
let data: QueryCommandOutput;

const command = new QueryCommand(params);
data = await client.send(command);
return data;
}

async function getItemCommandOutputFromDynamoDB(params: GetItemCommandInput) {
const client = getDynamoDBClient();

const command = new GetItemCommand(params);
const data = await client.send(command);
return data;
}

async function putItemCommandOutputFromDynamoDB(params: PutItemCommandInput) {
const client = getDynamoDBClient();

const command = new PutItemCommand(params);
const data = await client.send(command);
return data;
}

async function patchItemCommandOutputFromDynamoDB(params: UpdateItemCommandInput) {
const client = getDynamoDBClient();

const command = new UpdateItemCommand(params);
const data = await client.send(command);
return data;
}

async function deleteItemCommandOutputFromDynamoDB(params: DeleteItemCommandInput) {
const client = getDynamoDBClient();

const command = new DeleteItemCommand(params);
const data = await client.send(command);
return data;
}

export {
deleteItemCommandOutputFromDynamoDB,
getItemCommandOutputFromDynamoDB,
getQueryCommandOutputFromDynamoDB,
getScanCommandOutputFromDynamoDB,
patchItemCommandOutputFromDynamoDB,
putItemCommandOutputFromDynamoDB,
};
14 changes: 14 additions & 0 deletions src/libs/dynamoDB/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GetItemCommandOutput, ScanCommandOutput } from '@aws-sdk/client-dynamodb';
import { unmarshall } from '@aws-sdk/util-dynamodb';

function parseRecord(data: GetItemCommandOutput) {
const record = unmarshall(data.Item!);
return record;
}

function parseRecords(data: ScanCommandOutput) {
const records = data.Items!.map((i) => unmarshall(i));
return records;
}

export { parseRecord, parseRecords };
128 changes: 128 additions & 0 deletions src/libs/dynamoDB/tableHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { QueryCommandInput, ScanCommandInput } from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import {
deleteItemCommandOutputFromDynamoDB,
getItemCommandOutputFromDynamoDB,
getQueryCommandOutputFromDynamoDB,
getScanCommandOutputFromDynamoDB,
patchItemCommandOutputFromDynamoDB,
putItemCommandOutputFromDynamoDB,
} from './getOutput';
import { parseRecord, parseRecords } from './parse';

async function getListFromDynamoDB(table: string) {
const params: ScanCommandInput = {
TableName: table,
};
while (true) {
try {
const data = await getScanCommandOutputFromDynamoDB(params);
const records = parseRecords(data);
return records;
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

async function getQueryFromDynamoDB(table: string, queryKeys: any) {
let keyExpression = '';
Object.keys(queryKeys).forEach((key) => {
if (keyExpression !== '') keyExpression += ' AND ';
keyExpression += key + ' = :' + key;
});
let expressionAttributeValues: { [key: string]: any } = {};
Object.keys(queryKeys).forEach((key) => {
expressionAttributeValues[':' + key] = queryKeys[key];
});
const params: QueryCommandInput = {
TableName: table,
KeyConditionExpression: keyExpression,
ExpressionAttributeValues: expressionAttributeValues,
ConsistentRead: true,
};
while (true) {
try {
const data = await getQueryCommandOutputFromDynamoDB(params);
const records = parseRecords(data);
return records;
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

async function getFromDynamoDB(table: string, key: any) {
const params = {
TableName: table,
Key: key,
};

while (true) {
try {
const data = await getItemCommandOutputFromDynamoDB(params);
const record = parseRecord(data);
return record;
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

async function putFromDynamoDB(table: string, item: any) {
const params = {
TableName: table,
Item: marshall(item),
};
while (true) {
try {
return await putItemCommandOutputFromDynamoDB(params);
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

async function patchFromDynamoDB(table: string, key: any, updates: any) {
const params = {
TableName: table,
Key: key,
AttributeUpdates: updates,
};
while (true) {
try {
return await patchItemCommandOutputFromDynamoDB(params);
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

async function deleteFromDynamoDB(table: string, key: any) {
const params = {
TableName: table,
Key: key,
};
while (true) {
try {
return await deleteItemCommandOutputFromDynamoDB(params);
} catch (error: any) {
if (error?.name !== 'ProvisionedThroughputExceededException') throw error;
else console.log(error?.name);
}
}
}

export {
deleteFromDynamoDB,
getFromDynamoDB,
getListFromDynamoDB,
getQueryFromDynamoDB,
patchFromDynamoDB,
putFromDynamoDB,
};
66 changes: 66 additions & 0 deletions src/pages/api/dynamoDB/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NextApiRequest, NextApiResponse } from "next";
import {
DynamoDBClient,
GetItemCommand,
PutItemCommand,
PutItemCommandInput,
QueryCommand,
ScanCommand,
ScanCommandInput,
} from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";

const client = new DynamoDBClient({
region: process.env.REGION,
credentials: {
accessKeyId: process.env.DYNAMODB_ACCESS_KEY!,
secretAccessKey: process.env.DYNAMODB_SECRET_KEY!,
},
});
const tableName = "eolluga-dynamodb";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
switch (req.method) {
case "GET":
try {
const id = req.query.id as string;
const params = {
TableName: tableName,
Key: {
id: { S: id },
},
};
const data = await client.send(new GetItemCommand(params));
const records = unmarshall(data.Item!);
res.status(200).json(records);
} catch (error: any) {
console.log(error);
res.status(400).json({
message: error.message || "An error occurred during fetching data",
});
}
break;
case "POST":
try {
const item = req.body;
const params: PutItemCommandInput = {
TableName: tableName,
Item: marshall(item),
};
const data = await client.send(new PutItemCommand(params));
res.status(200);
} catch (error: any) {
console.log(error);
res.status(400).json({
message: error.message || "An error occurred during posting data",
});
}
break;
default:
res.status(405).json({ message: "Method Not Allowed" });
}
res.end();
}
6 changes: 3 additions & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default function Home() {
const { query } = useRouter();

useEffect(() => {
if (typeof query.inviteData === "string") {
const inviteData = query.inviteData as string;
localStorage.setItem("inviteData", inviteData);
if (typeof query.id === "string") {
const inviteDataId = query.id as string;
localStorage.setItem("inviteDataId", inviteDataId);
}
}, [query]);

Expand Down
10 changes: 10 additions & 0 deletions src/pages/manage/confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TopTitle from "@modules/layout/TopTitle";
import TextButton from "@modules/components/button/TextButton";
import ConfirmEmployee from "@/screen/manage/ConfirmEmployee";
import ShareLink from "@/screen/manage/ShareLink";
import { InviteSchedule, inviteScheduleInit } from "@/data/inviteSchedule";

type View = "confirm" | "share";

Expand All @@ -17,6 +18,8 @@ export default function Home() {
};

const handleShiftButton = () => {
setSelectedPostion("");
setInviteSchedule(inviteScheduleInit);
router.push("/manage");
};

Expand Down Expand Up @@ -45,3 +48,10 @@ export default function Home() {
</FlexBox>
);
}
function setSelectedPostion(arg0: string) {
throw new Error("Function not implemented.");
}

function setInviteSchedule(inviteScheduleInit: InviteSchedule) {
throw new Error("Function not implemented.");
}
Loading

0 comments on commit 0825c13

Please sign in to comment.