-
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.
Merge pull request #74 from Wonchang0314/develop
FIX: 링크를 통한 가게 합류 기능 개선
- Loading branch information
Showing
12 changed files
with
1,314 additions
and
94 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
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; | ||
}; |
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,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, | ||
}; |
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,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 }; |
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,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, | ||
}; |
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,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(); | ||
} |
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
Oops, something went wrong.