From 64268a1f3c3fe5bcbd7285bb2d23411c8dad766c Mon Sep 17 00:00:00 2001 From: roschaefer Date: Tue, 3 Sep 2019 20:42:01 +0200 Subject: [PATCH] Add locations to seeds, fix `shoutedByCurrentUser` --- backend/src/models/Location.js | 21 ++++++ backend/src/models/User.js | 6 ++ backend/src/models/index.js | 1 + backend/src/schema/resolvers/posts.js | 2 +- backend/src/schema/types/Location.gql | 17 +++++ backend/src/schema/types/schema.gql | 17 ----- backend/src/schema/types/type/Post.gql | 5 +- backend/src/seed/factories/index.js | 2 + backend/src/seed/factories/locations.js | 24 +++++++ backend/src/seed/seed-db.js | 87 +++++++++++++++++++++++++ 10 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 backend/src/models/Location.js create mode 100644 backend/src/schema/types/Location.gql create mode 100644 backend/src/seed/factories/locations.js diff --git a/backend/src/models/Location.js b/backend/src/models/Location.js new file mode 100644 index 000000000..bd6e0b5d9 --- /dev/null +++ b/backend/src/models/Location.js @@ -0,0 +1,21 @@ +module.exports = { + id: { type: 'string', primary: true }, + lat: { type: 'number' }, + lng: { type: 'number' }, + type: { type: 'string' }, + name: { type: 'string' }, + nameES: { type: 'string' }, + nameFR: { type: 'string' }, + nameIT: { type: 'string' }, + nameEN: { type: 'string' }, + namePT: { type: 'string' }, + nameDE: { type: 'string' }, + nameNL: { type: 'string' }, + namePL: { type: 'string' }, + isIn: { + type: 'relationship', + relationship: 'IS_IN', + target: 'Location', + direction: 'out', + }, +} diff --git a/backend/src/models/User.js b/backend/src/models/User.js index b10144315..28ab46d3c 100644 --- a/backend/src/models/User.js +++ b/backend/src/models/User.js @@ -89,4 +89,10 @@ module.exports = { target: 'Post', direction: 'out', }, + isIn: { + type: 'relationship', + relationship: 'IS_IN', + target: 'Location', + direction: 'out', + }, } diff --git a/backend/src/models/index.js b/backend/src/models/index.js index a5323678b..a7d3c8252 100644 --- a/backend/src/models/index.js +++ b/backend/src/models/index.js @@ -10,4 +10,5 @@ export default { Comment: require('./Comment.js'), Category: require('./Category.js'), Tag: require('./Tag.js'), + Location: require('./Location.js'), } diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index ca3812a37..5cab8c601 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -225,7 +225,7 @@ export default { }, boolean: { shoutedByCurrentUser: - '<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1', + 'MATCH(this)<-[:SHOUTED]-(related:User {id: $cypherParams.currentUserId}) RETURN COUNT(related) >= 1', }, }), relatedContributions: async (parent, params, context, resolveInfo) => { diff --git a/backend/src/schema/types/Location.gql b/backend/src/schema/types/Location.gql new file mode 100644 index 000000000..e7053e345 --- /dev/null +++ b/backend/src/schema/types/Location.gql @@ -0,0 +1,17 @@ +type Location { + id: ID! + name: String! + nameEN: String + nameDE: String + nameFR: String + nameNL: String + nameIT: String + nameES: String + namePT: String + namePL: String + type: String! + lat: Float + lng: Float + parent: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l") +} + diff --git a/backend/src/schema/types/schema.gql b/backend/src/schema/types/schema.gql index 62f84d677..dc2cec8f5 100644 --- a/backend/src/schema/types/schema.gql +++ b/backend/src/schema/types/schema.gql @@ -51,23 +51,6 @@ type Statistics { countShouts: Int! } -type Location { - id: ID! - name: String! - nameEN: String - nameDE: String - nameFR: String - nameNL: String - nameIT: String - nameES: String - namePT: String - namePL: String - type: String! - lat: Float - lng: Float - parent: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l") -} - type Report { id: ID! submitter: User @relation(name: "REPORTED", direction: "IN") diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index 5f209ea19..eeaae0279 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -43,10 +43,7 @@ type Post { # Has the currently logged in user shouted that post? shoutedByCurrentUser: Boolean! @cypher( - statement: """ - MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId}) - RETURN COUNT(u) >= 1 - """ + statement: "MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId}) RETURN COUNT(u) >= 1" ) emotions: [EMOTED] diff --git a/backend/src/seed/factories/index.js b/backend/src/seed/factories/index.js index 620a53183..913e6efa1 100644 --- a/backend/src/seed/factories/index.js +++ b/backend/src/seed/factories/index.js @@ -7,6 +7,7 @@ import createComment from './comments.js' import createCategory from './categories.js' import createTag from './tags.js' import createSocialMedia from './socialMedia.js' +import createLocation from './locations.js' export const seedServerHost = 'http://127.0.0.1:4001' @@ -28,6 +29,7 @@ const factories = { Category: createCategory, Tag: createTag, SocialMedia: createSocialMedia, + Location: createLocation, } export const cleanDatabase = async (options = {}) => { diff --git a/backend/src/seed/factories/locations.js b/backend/src/seed/factories/locations.js new file mode 100644 index 000000000..99b666de8 --- /dev/null +++ b/backend/src/seed/factories/locations.js @@ -0,0 +1,24 @@ +export default function create() { + return { + factory: async ({ args, neodeInstance }) => { + const defaults = { + name: 'Germany', + namePT: 'Alemanha', + nameDE: 'Deutschland', + nameES: 'Alemania', + nameNL: 'Duitsland', + namePL: 'Niemcy', + nameFR: 'Allemagne', + nameIT: 'Germania', + nameEN: 'Germany', + id: 'country.10743216036480410', + type: 'country', + } + args = { + ...defaults, + ...args, + } + return neodeInstance.create('Location', args) + }, + } +} diff --git a/backend/src/seed/seed-db.js b/backend/src/seed/seed-db.js index 153b65805..48a739529 100644 --- a/backend/src/seed/seed-db.js +++ b/backend/src/seed/seed-db.js @@ -25,6 +25,86 @@ import { gql } from '../jest/helpers' const { mutate } = createTestClient(server) const f = Factory() + + const [Hamburg, Berlin, Germany, Paris, France] = await Promise.all([ + f.create('Location', { + id: 'region.5127278006398860', + name: 'Hamburg', + type: 'region', + lat: 10.0, + lng: 53.55, + nameES: 'Hamburgo', + nameFR: 'Hambourg', + nameIT: 'Amburgo', + nameEN: 'Hamburg', + namePT: 'Hamburgo', + nameDE: 'Hamburg', + nameNL: 'Hamburg', + namePL: 'Hamburg', + }), + f.create('Location', { + id: 'region.14880313158564380', + type: 'region', + name: 'Berlin', + lat: 13.38333, + lng: 52.51667, + nameES: 'Berlín', + nameFR: 'Berlin', + nameIT: 'Berlino', + nameEN: 'Berlin', + namePT: 'Berlim', + nameDE: 'Berlin', + nameNL: 'Berlijn', + namePL: 'Berlin', + }), + f.create('Location', { + id: 'country.10743216036480410', + name: 'Germany', + type: 'country', + namePT: 'Alemanha', + nameDE: 'Deutschland', + nameES: 'Alemania', + nameNL: 'Duitsland', + namePL: 'Niemcy', + nameFR: 'Allemagne', + nameIT: 'Germania', + nameEN: 'Germany', + }), + f.create('Location', { + id: 'region.9397217726497330', + name: 'Paris', + type: 'region', + lat: 2.35183, + lng: 48.85658, + nameES: 'París', + nameFR: 'Paris', + nameIT: 'Parigi', + nameEN: 'Paris', + namePT: 'Paris', + nameDE: 'Paris', + nameNL: 'Parijs', + namePL: 'Paryż', + }), + f.create('Location', { + id: 'country.9759535382641660', + name: 'France', + type: 'country', + namePT: 'França', + nameDE: 'Frankreich', + nameES: 'Francia', + nameNL: 'Frankrijk', + namePL: 'Francja', + nameFR: 'France', + nameIT: 'Francia', + nameEN: 'France', + }), + ]) + await Promise.all([ + Berlin.relateTo(Germany, 'isIn'), + Hamburg.relateTo(Germany, 'isIn'), + Paris.relateTo(France, 'isIn'), + ]) + const [racoon, rabbit, wolf, bear, turtle, rhino] = await Promise.all([ f.create('Badge', { id: 'indiegogo_en_racoon', @@ -112,6 +192,13 @@ import { gql } from '../jest/helpers' }), ]) + await Promise.all([ + peterLustig.relateTo(Berlin, 'isIn'), + bobDerBaumeister.relateTo(Hamburg, 'isIn'), + jennyRostock.relateTo(Paris, 'isIn'), + huey.relateTo(Paris, 'isIn'), + ]) + await Promise.all([ peterLustig.relateTo(racoon, 'rewarded'), peterLustig.relateTo(rhino, 'rewarded'),