Parcourir la source

Extract add count to URL to it's own query

poeti8 il y a 6 ans
Parent
commit
9a5a7c9361
1 fichiers modifiés avec 19 ajouts et 1 suppressions
  1. 19 1
      server/db/url.js

+ 19 - 1
server/db/url.js

@@ -49,13 +49,31 @@ exports.createShortUrl = async params => {
   };
 };
 
+exports.addUrlCount = async (id, domain) => {
+  const session = driver.session();
+  const { records = [] } = await session.writeTransaction(tx =>
+    tx.run(
+      'MATCH (l:URL { id: $id }) ' +
+        `${domain ? 'MATCH (l)-[:USES]->({ name: $domain })' : ''} ` +
+        'SET l.count = l.count + 1 ' +
+        'RETURN l',
+      {
+        id,
+        domain,
+      }
+    )
+  );
+  session.close();
+  const url = records.length && records[0].get('l').properties;
+  return url;
+};
+
 exports.createVisit = async params => {
   const session = driver.session();
   const { records = [] } = await session.writeTransaction(tx =>
     tx.run(
       'MATCH (l:URL { id: $id }) ' +
         `${params.domain ? 'MATCH (l)-[:USES]->({ name: $domain })' : ''} ` +
-        'SET l.count = l.count + 1 ' +
         'CREATE (v:VISIT)' +
         'MERGE (b:BROWSER { browser: $browser })' +
         'MERGE (c:COUNTRY { country: $country })' +