From 2d473e5ea63d8d6a441393bbbbc7bbfced9c60f6 Mon Sep 17 00:00:00 2001 From: EungGu Yun Date: Sun, 8 Dec 2019 19:07:35 +0900 Subject: [PATCH] Add category to item --- src/backend/.gitignore | 1 + src/backend/handlers/item.js | 15 +++++++++------ src/backend/responses/item.js | 12 +++++++----- src/backend/tests/item_registration.json | 14 +++++++------- src/backend/utils/builder.js | 18 +++++++++++++++++- src/backend/utils/database.js | 9 +++++---- src/backend/utils/parser.js | 12 +++++++++++- 7 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/backend/.gitignore b/src/backend/.gitignore index e444b0c..8a239a3 100644 --- a/src/backend/.gitignore +++ b/src/backend/.gitignore @@ -7,6 +7,7 @@ jspm_packages .build serverless_sdk s_* +_warmup # Credentials utils/credential.js diff --git a/src/backend/handlers/item.js b/src/backend/handlers/item.js index b946754..1f24a9b 100644 --- a/src/backend/handlers/item.js +++ b/src/backend/handlers/item.js @@ -2,20 +2,23 @@ const parser = require("../utils/parser"); const database = require("../utils/database"); -const authenticator = require("../utils/authenticator"); const responseTemplate = require("../responses/template"); const registration = async (event) => { const userId = parser.getUserId(event); const parameters = parser.getParameters(event); + const item_category = parameters["item_category"].value; const item_name = parameters["item_name"].origin; - const item_price_raw = parameters["item_price"].value; + const item_price = parser.getPrice(parameters["item_price"].value); const item_detail = parameters["item_detail"].origin; - const item_image = parameters["item_image"].origin; + const item_image = parser.getImageUrls(parameters["item_image"].origin); - // Todo: Regist item to database - - return responseTemplate.itemRegistrationSuccess(item_name); + const result = await database.registNewItem(userId, item_category, item_name, item_price, item_detail, item_image); + if (result.success == true) { + return responseTemplate.itemRegistrationSuccess(item_name, item_detail, item_price, item_image[0]); + } else { + return responseTemplate.itemRegistrationFail(""); + } } module.exports = { diff --git a/src/backend/responses/item.js b/src/backend/responses/item.js index ca4e12a..cf4aa86 100644 --- a/src/backend/responses/item.js +++ b/src/backend/responses/item.js @@ -3,13 +3,15 @@ const builder = require("../utils/builder"); const resource = require("../utils/resource"); -module.exports.itemRegistrationSuccess = (itemName) => { +module.exports.itemRegistrationSuccess = (itemName, itemDetail, itemPrice, imageUrl, nickname) => { // Construct registration guide - const resultThumbnail = builder.getThumbnail(resource.itemRegistrationSuccessThumbnailUrl); - const resultTitle = "상품 등록 성공"; - const resultDescription = itemName + " 등록에 성공하였습니다."; + const resultThumbnail = builder.getThumbnail(imageUrl); + const resultTitle = itemName; + const resultDescription = itemDetail; + const resultPrice = itemPrice; + const resultNickname = nickname; const resultMainMenuButton = builder.getButton("처음으로", "block", "처음으로", resource.welcomeBlockId); - const resultCard = builder.getBasicCard(resultTitle, resultDescription, resultThumbnail, [resultMainMenuButton]); + const resultCard = builder.getCommerceCard(resultTitle, resultDescription, resultPrice, resultThumbnail, result, [resultMainMenuButton]); // Build response return builder.buildResponse([resultCard]); diff --git a/src/backend/tests/item_registration.json b/src/backend/tests/item_registration.json index 9c12f21..0314756 100644 --- a/src/backend/tests/item_registration.json +++ b/src/backend/tests/item_registration.json @@ -48,8 +48,8 @@ "id": "5dda19bf8192ac00011a2918", "detailParams": { "item_name": { - "origin": "어어", - "value": "어어", + "origin": "상품의 이름", + "value": "상품의 이름", "groupName": "" }, "item_price": { @@ -58,13 +58,13 @@ "groupName": "" }, "item_detail": { - "origin": "처음으로", - "value": "처음으로", + "origin": "상세한 설명", + "value": "상세한 설명", "groupName": "" }, - "$item_image": { - "origin": "List(http://secure.kakaocdn.net/dna/biS7Z3/K6avpFEudv/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=gAuRfga0QSVLQTu%2Fto7KhmgKyGI%3D, http://secure.kakaocdn.net/dna/cjvCoE/K6aAZNxD00/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=JZ61bx1qvBaR6RHGo352yak7fHA%3D, http://secure.kakaocdn.net/dna/bPthHV/K6aA1q3sS8/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=9rz9GHZDcgjnucALZpUjFe71QAY%3D)", - "value": "{\"privacyAgreement\":\"Y\",\"imageQuantity\":\"3\",\"secureUrls\":\"List(http://secure.kakaocdn.net/dna/biS7Z3/K6avpFEudv/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=gAuRfga0QSVLQTu%2Fto7KhmgKyGI%3D, http://secure.kakaocdn.net/dna/cjvCoE/K6aAZNxD00/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=JZ61bx1qvBaR6RHGo352yak7fHA%3D, http://secure.kakaocdn.net/dna/bPthHV/K6aA1q3sS8/XXX/img_org.jpg?credential=Kq0eSbCrZgKIq51jh41Uf1jLsUh7VWcz&expires=1575476312&allow_ip=&allow_referer=&signature=9rz9GHZDcgjnucALZpUjFe71QAY%3D)\",\"expire\":\"1970-01-19T14:37:56+0900\"}", + "item_image": { + "origin": "List(http://k.kakaocdn.net/dn/xsBdT/btqqIzbK4Hc/F39JI8XNVDMP9jPvoVdxl1/2x1.jpg, https://i.ibb.co/0f5zh6N/Kakao-Talk-Photo-2019-12-06-16-36-17.jpg)", + "value": "{\"privacyAgreement\":\"Y\",\"imageQuantity\":\"3\",\"secureUrls\":\"List(http://k.kakaocdn.net/dn/xsBdT/btqqIzbK4Hc/F39JI8XNVDMP9jPvoVdxl1/2x1.jpg, https://i.ibb.co/0f5zh6N/Kakao-Talk-Photo-2019-12-06-16-36-17.jpg)\"}", "groupName": "" } } diff --git a/src/backend/utils/builder.js b/src/backend/utils/builder.js index 8a480d0..76479ef 100644 --- a/src/backend/utils/builder.js +++ b/src/backend/utils/builder.js @@ -118,6 +118,21 @@ const getCarousel = (type, cards) => { } } +const getCommerceCard = (title, description, price, thumbnail, nickname, buttons=[]) => { + return { + commerceCard: { + description: title + "\n" + description, + price: price, + currency: "won", + thumbnails: [ thumbnail ], + profile: { + nickname: nickname + }, + buttons: buttons + } + }; +} + module.exports = { buildResponse, buildAWSResponse, @@ -129,5 +144,6 @@ module.exports = { getBasicCardBody, getBasicCard, getListCard, - getCarousel + getCarousel, + getCommerceCard } diff --git a/src/backend/utils/database.js b/src/backend/utils/database.js index 4ccdacb..14667d7 100644 --- a/src/backend/utils/database.js +++ b/src/backend/utils/database.js @@ -30,7 +30,6 @@ const query = async (sql, params) => { }); }; - const uploadImages = async (directory, locations) => { const results = []; for (var key in locations) { @@ -206,7 +205,8 @@ const registNewItem = async (userId, item_name, item_price, item_detail, item_im const itemId = await generateToken(); const imageurls = JSON.stringify(await uploadImages(itemId + '/', item_image)); var sql = 'INSERT INTO item(userId, itemId, item_name, item_price, item_detail, item_image, item_date)VALUES(?,?,?,?,?,?,?)'; - var params = [userId, 'itemid', item_name, item_price, item_detail, imageurls, NOW()]; + var params = [userId, itemId, item_name, item_price, item_detail, imageurls, new Date().toISOString().slice(0, 19).replace('T', ' ')]; + console.log(Date.now()); // userId: string, item_name: string, item_price: number, item_detail: string, item_image: JSON array // Todo: Create new item with scheme { userId: string, // itemId: number (auto increment), @@ -223,6 +223,7 @@ const registNewItem = async (userId, item_name, item_price, item_detail, item_im message: "" }; }catch(err){ + console.log(err); return{ success: false, message:"fail" @@ -257,7 +258,7 @@ const getItem = async (itemId) => { } }; -const getuserItem = async (userId) => { +const getUserItem = async (userId) => { const results = []; var sql = 'SELECT * FROM item WHERE userId = ?'; try{ @@ -325,5 +326,5 @@ module.exports = { getUser, registNewItem, getItem, - getuserItem + getUserItem } diff --git a/src/backend/utils/parser.js b/src/backend/utils/parser.js index 314d472..c002260 100644 --- a/src/backend/utils/parser.js +++ b/src/backend/utils/parser.js @@ -24,11 +24,21 @@ const getKeys = (event) => { return event.queryStringParameters; } +const getPrice = (price_string) => { + return JSON.parse(price_string).amount +}; + +const getImageUrls = (image_list) => { + return image_list.substring(5, image_list.length - 1).split(", "); +}; + module.exports = { getBody, getUserId, getMessage, getParameters, getBlockId, - getKeys + getKeys, + getPrice, + getImageUrls }