-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
published / remove functions working
- Loading branch information
1 parent
0243d6e
commit 72d0e6d
Showing
16 changed files
with
1,036 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {insertOne} from '../utils'; | ||
function prepareQuery(string: any) { | ||
const parsed = JSON.parse(string); | ||
const expectedArgs = [ | ||
'templateAuthor', | ||
'templateName', | ||
'templateMap', | ||
'templateInstance', | ||
'dataset', | ||
'instanceCreator', | ||
'thumbnail', | ||
]; | ||
if (typeof parsed !== 'object') { | ||
throw new Error('bad inputs'); | ||
} | ||
const noUnexpectedKeys = Object.keys(parsed).every((key) => expectedArgs.includes(key)); | ||
const allExpectedKeys = expectedArgs.every((key) => Object.keys(parsed).includes(key)); | ||
if (!noUnexpectedKeys || !allExpectedKeys) { | ||
throw new Error('bad inputs'); | ||
} | ||
const query = { | ||
template_creator: parsed.templateAuthor, | ||
template_name: parsed.templateName, | ||
name: parsed.templateInstance, | ||
template_instance: parsed.templateMap, | ||
instance_creator: parsed.instanceCreator, | ||
dataset: parsed.dataset, | ||
thumbnail: parsed.thumbnail, | ||
}; | ||
return query; | ||
} | ||
|
||
export const handler = insertOne('template-instances', prepareQuery); | ||
|
||
// export const handler: Handler = (event, context, callback) => { | ||
// let inputs: Record<string, any>; | ||
// try { | ||
// inputs = parseInputs(event.body); | ||
// } catch (e) { | ||
// errorResponse(callback, 'Bad submit'); | ||
// return; | ||
// } | ||
|
||
// const query = { | ||
// template_creator: inputs.templateAuthor, | ||
// template_name: inputs.templateName, | ||
// name: inputs.templateInstance, | ||
// template_instance: inputs.templateMap, | ||
// instance_creator: inputs.instanceCreator, | ||
// dataset: inputs.dataset, | ||
// thumbnail: inputs.thumbnail, | ||
// }; | ||
|
||
// MongoClient.connect(`${DB_URL}/${DB_NAME}`) | ||
// .then((connection) => { | ||
// const db = connection.db(DB_NAME); | ||
// const collection = db.collection('template-instances'); | ||
// return collection.insertOne(query).then((result) => { | ||
// console.log(result); | ||
// callback!(null, {statusCode: 200}); | ||
// connection.close(); | ||
// }); | ||
// }) | ||
// .catch((err) => { | ||
// return errorResponse(callback, err); | ||
// }); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {insertOne} from '../utils'; | ||
|
||
function prepareQuery(body: any) { | ||
const parsed = JSON.parse(body); | ||
const {template} = parsed; | ||
if (!template || typeof template !== 'object') { | ||
throw new Error('bad inputs'); | ||
} | ||
const {templateAuthor, templateName} = template; | ||
if (!templateAuthor || !templateName) { | ||
throw new Error('bad inputs'); | ||
} | ||
|
||
return { | ||
template: JSON.stringify(template), | ||
creator: templateAuthor, | ||
name: templateName, | ||
}; | ||
} | ||
|
||
export const handler = insertOne('templates', prepareQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {errorResponse} from '../utils'; | ||
import {Handler} from '@netlify/functions'; | ||
import {MongoClient} from 'mongodb'; | ||
|
||
const DB_URL = process.env.DB_URL || 'mongodb://localhost:27017'; | ||
const DB_NAME = 'ivy-be'; | ||
|
||
export const handler: Handler = (event, context, callback) => { | ||
let parsedArgs: Record<string, any>; | ||
try { | ||
parsedArgs = JSON.parse(event.body!); | ||
} catch (e) { | ||
errorResponse(callback, 'Bad submit'); | ||
return; | ||
} | ||
|
||
MongoClient.connect(`${DB_URL}/${DB_NAME}`) | ||
.then(async (connection) => { | ||
const db = connection.db(DB_NAME); | ||
|
||
await db.collection('template-instances').deleteMany({ | ||
template_name: parsedArgs.templateName, | ||
template_creator: parsedArgs.templateAuthor, | ||
name: parsedArgs.instanceName, | ||
instance_creator: parsedArgs.userName, | ||
}); | ||
|
||
callback!(null, {statusCode: 200}); | ||
connection.close(); | ||
}) | ||
.catch((err) => errorResponse(callback, err)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {errorResponse} from '../utils'; | ||
import {Handler} from '@netlify/functions'; | ||
import {MongoClient} from 'mongodb'; | ||
|
||
const DB_URL = process.env.DB_URL || 'mongodb://localhost:27017'; | ||
const DB_NAME = 'ivy-be'; | ||
|
||
export const handler: Handler = (event, context, callback) => { | ||
let parsedArgs: Record<string, any>; | ||
try { | ||
parsedArgs = JSON.parse(event.body!); | ||
} catch (e) { | ||
errorResponse(callback, 'Bad submit'); | ||
return; | ||
} | ||
|
||
MongoClient.connect(`${DB_URL}/${DB_NAME}`) | ||
.then(async (connection) => { | ||
const db = connection.db(DB_NAME); | ||
|
||
await db.collection('templates').deleteMany({ | ||
name: parsedArgs.templateName, | ||
creator: parsedArgs.templateAuthor, | ||
}); | ||
|
||
await db.collection('template-instances').deleteMany({ | ||
template_name: parsedArgs.templateName, | ||
template_creator: parsedArgs.templateAuthor, | ||
}); | ||
|
||
callback!(null, {statusCode: 200}); | ||
connection.close(); | ||
}) | ||
.catch((err) => errorResponse(callback, err)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import {simpleHandler} from '../utils'; | ||
import {getMany} from '../utils'; | ||
|
||
export const handler = simpleHandler('template-instances'); | ||
export const handler = getMany('template-instances'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import {simpleHandler} from '../utils'; | ||
import {getMany} from '../utils'; | ||
|
||
export const handler = simpleHandler('templates'); | ||
export const handler = getMany('templates'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.