From 55a5680d84948f7f87b113e380ebec27fc614a44 Mon Sep 17 00:00:00 2001 From: yeon Date: Wed, 27 Nov 2019 16:17:12 +0900 Subject: [PATCH] =?UTF-8?q?[Add]=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20fe,=20api=20toggle=20#33=20#27?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit why 게시글 좋아요 toggle 시 서버에 값이 반영되어야 함 이전의 좋아요, 좋아요취소가 따로 있어서 복잡 how 댓글의 좋아요 좋아요 취소를 하나로 합치고 서버쪽에서 구분해서 쿼리하도록 분리 --- client/src/composition/Feed/FeedFooter.tsx | 16 +++++------ server/src/api/feed/feed.graphql | 2 +- server/src/api/feed/feed.resolvers.ts | 31 +++++++++++++--------- server/src/schema/feed/query.ts | 17 +++--------- 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/client/src/composition/Feed/FeedFooter.tsx b/client/src/composition/Feed/FeedFooter.tsx index 0e6ecfd3..ba7954bf 100644 --- a/client/src/composition/Feed/FeedFooter.tsx +++ b/client/src/composition/Feed/FeedFooter.tsx @@ -70,8 +70,8 @@ interface Iprops { } const SEND_LIKE = gql` - mutation updateLike($feedId: Int) { - updateLike(feedId: $feedId) + mutation updateLike($feedId: Int, $count: Int) { + updateLike(feedId: $feedId, count: $count) } `; @@ -86,22 +86,18 @@ const FeedFooter = ({ const ToggleLike = () => { setLikeCnt((props: number) => { - setHasLiked((props: boolean) => !props); if (!hasLiked) { - sendLike(1); return props + 1; } else { - // cancleLike(-1); return props - 1; } }); + setHasLiked((props: boolean) => { + updateLike({ variables: { feedId, count: Number(!props) } }); + return !props; + }); }; - const sendLike = (count: number) => { - // send count - updateLike({ variables: { feedId } }); - console.log(count); - }; return ( <> diff --git a/server/src/api/feed/feed.graphql b/server/src/api/feed/feed.graphql index 536ee170..b0495263 100644 --- a/server/src/api/feed/feed.graphql +++ b/server/src/api/feed/feed.graphql @@ -61,5 +61,5 @@ type Query { } type Mutation { - updateLike(feedId: Int): Boolean + updateLike(feedId: Int, count: Int): Boolean } diff --git a/server/src/api/feed/feed.resolvers.ts b/server/src/api/feed/feed.resolvers.ts index 048a4103..a2732366 100644 --- a/server/src/api/feed/feed.resolvers.ts +++ b/server/src/api/feed/feed.resolvers.ts @@ -2,10 +2,12 @@ import db from '../../db'; import { MATCH_NEW_FEEDS, MATCH_FEEDS, - UPDATE_LIKE + UPDATE_LIKE, + DELETE_LIKE } from '../../schema/feed/query'; import { IKey } from '../../schema/commonTypes'; import neo4j from 'neo4j-driver'; +import console = require('console'); const session = db.session(); @@ -39,8 +41,6 @@ const Datetransform = object => { if (object.hasOwnProperty(property)) { const propertyValue = object[property]; if (neo4j.isInt(propertyValue)) { - // console.log(neo4j.integer.toNumber(propertyValue)); - // console.log('tet', propertyValue); returnobj[property] = String(propertyValue); } else if (toString.call(propertyValue) === '[object String]') { let temp = {}; @@ -71,7 +71,6 @@ const ParseResultRecords = records => { arr = { ...arr, ...temp }; } else if (node instanceof neo4j.types.Integer) { const temp = {}; - temp[nodeKey] = String(node); arr = { ...arr, ...temp }; } else if (toString.call(node) === '[object Array]') { @@ -112,21 +111,27 @@ export default { }, Mutation: { - updateLike: async (_, { feedId }, { req }) => { - let userEmail1 = 'dasom@naver.com'; + updateLike: async (_, { feedId, count }, { req }) => { + let userEmail1 = ''; if (!req.user) { console.log('사용자 정보가 없습니다 다시 로그인해 주세요'); return false; } userEmail1 = req.user.email; - console.log(req.user); - - const result = await session.run(UPDATE_LIKE, { - useremail: userEmail1, - feedId - }); + let result; + if (count > 0) { + result = await session.run(UPDATE_LIKE, { + useremail: userEmail1, + feedId + }); + } else { + result = await session.run(DELETE_LIKE, { + useremail: userEmail1, + feedId + }); + } - console.log('true', result); + console.log('result: ', result); return true; } } diff --git a/server/src/schema/feed/query.ts b/server/src/schema/feed/query.ts index 081bc6ca..58eed68b 100644 --- a/server/src/schema/feed/query.ts +++ b/server/src/schema/feed/query.ts @@ -15,10 +15,6 @@ order by feed.createdAt desc LIMIT {first} `; -const MATCH_NEW_FEEDS2 = `MATCH (user:User { email: 'abc@naver.com' })-[:AUTHOR]->(feed:Feed) -where feed.createdAt < datetime('9999-12-31T09:29:26.050Z') -RETURN feed`; - const UPDATE_LIKE = ` MATCH (u:User),(f:Feed) WHERE u.email = {useremail} AND ID(f) = {feedId} @@ -26,15 +22,8 @@ MERGE (u)-[r:LIKE]->(f) RETURN type(r)`; const DELETE_LIKE = ` -MATCH (u:User),(f:Feed) +MATCH (u:User)-[r:LIKE]->(f:Feed) WHERE u.email = {useremail} AND ID(f) = {feedId} -MERGE (u)-[r:LIKE]->(f) -RETURN type(r)`; +delete r`; -export { - MATCH_NEW_FEEDS, - MATCH_NEW_FEEDS2, - MATCH_FEEDS, - UPDATE_LIKE, - DELETE_LIKE -}; +export { MATCH_NEW_FEEDS, MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE };