Skip to content

Commit

Permalink
[Add] 게시글 좋아요 fe, api toggle #33 #27
Browse files Browse the repository at this point in the history
why
게시글 좋아요 toggle 시 서버에 값이 반영되어야 함
이전의 좋아요, 좋아요취소가 따로 있어서 복잡

how
댓글의 좋아요 좋아요 취소를 하나로 합치고 서버쪽에서 구분해서 쿼리하도록 분리
  • Loading branch information
WooYeonSeo committed Nov 27, 2019
1 parent fdb1e8e commit 55a5680
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
16 changes: 6 additions & 10 deletions client/src/composition/Feed/FeedFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
`;

Expand All @@ -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 (
<>
<FeedActionDiv>
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/feed/feed.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ type Query {
}

type Mutation {
updateLike(feedId: Int): Boolean
updateLike(feedId: Int, count: Int): Boolean
}
31 changes: 18 additions & 13 deletions server/src/api/feed/feed.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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]') {
Expand Down Expand Up @@ -112,21 +111,27 @@ export default {
},

Mutation: {
updateLike: async (_, { feedId }, { req }) => {
let userEmail1 = '[email protected]';
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;
}
}
Expand Down
17 changes: 3 additions & 14 deletions server/src/schema/feed/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@ order by feed.createdAt desc
LIMIT {first}
`;

const MATCH_NEW_FEEDS2 = `MATCH (user:User { email: '[email protected]' })-[: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}
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 };

0 comments on commit 55a5680

Please sign in to comment.