From a0f4a5f393a9df576eced33f7948127d19ef33b7 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 29 Apr 2020 14:33:03 +0200 Subject: [PATCH] Ajout dees points API #3 et #2 Signed-off-by: Avior --- .gitignore | 2 ++ pages/api/coins.js | 41 +++++++++++++++++++++++-- pages/api/photos.js | 66 +++++++++++++++++++++++++++++++++++++++++ public/uploads/.gitkeep | 0 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 pages/api/photos.js create mode 100644 public/uploads/.gitkeep diff --git a/.gitignore b/.gitignore index 3dc2cb2..cb761ee 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ yarn-debug.log* yarn-error.log* .storage +/public/uploads/* +!/public/uploads/.gitkeep diff --git a/pages/api/coins.js b/pages/api/coins.js index 23d2886..cc749f2 100644 --- a/pages/api/coins.js +++ b/pages/api/coins.js @@ -3,7 +3,44 @@ import Coin from '../../models/Coin' export default async (req, res) => { await connexion.sync() - res.statusCode = 200 res.setHeader('Content-Type', 'application/json') - res.end(JSON.stringify(await Coin.findAll())) + const distanceVal = toInt(req.query.distance) + const coordx = toFloat(req.query.coordx) + const coordy = toFloat(req.query.coordy) + const coins = await Coin.findAll() + if (distanceVal && coordx && coordy) { + const result = [] + for (const coin of coins) { + const tmp = coin.toJSON() + console.log(tmp) + console.log(distance(tmp.coordx, tmp.coordy, coordx, coordy)) + if ((distance(tmp.coordx, tmp.coordy, coordx, coordy) * 1000) <= distanceVal) { + result.push(tmp) + } + } + return res.status(200).end(JSON.stringify(result)) + } + + res.status(200).end(JSON.stringify(coins)) +} + +function toFloat(number) { + const res = parseFloat(number) + return !isNaN(res) ? res : undefined +} + +function toInt(number) { + const res = parseInt(number) + return !isNaN(res) ? res : undefined +} + +// return distance in km +function distance(lat1, lon1, lat2, lon2) { + var p = 0.017453292519943295; + var c = Math.cos; + var a = 0.5 - c((lat2 - lat1) * p)/2 + + c(lat1 * p) * c(lat2 * p) * + (1 - c((lon2 - lon1) * p))/2; + + return 12742 * Math.asin(Math.sqrt(a)) } diff --git a/pages/api/photos.js b/pages/api/photos.js new file mode 100644 index 0000000..f0f7928 --- /dev/null +++ b/pages/api/photos.js @@ -0,0 +1,66 @@ +import { connexion } from '../../libs/util' +import Photo from '../../models/Photo' +import { promises } from 'fs' + +export default async (req, res) => { + await connexion.sync() + res.setHeader('Content-Type', 'application/json') + const pseudo = req.query.pseudo + const picName = req.query.filename + const location = req.query.location + + if (location && isNaN(parseInt(location))) { + return res.status(400).end() + } + + if (req.method === 'POST') { + if (!pseudo || !picName || !location) { + return res.status(400).end() + } + // post de la photo et upload du body + const path = `/uploads/${pseudo}-${Math.random() * 100000000000000000}-${picName}` + const backendPath = `./public/${path}` + await promises.writeFile(backendPath, await streamToBuffer(req)) + try { + const photo = await Photo.create({ + pseudo: pseudo, + path, + coinId: parseInt(location) + }) + res.statusCode = 200 + return res.end(JSON.stringify(photo)) + } catch (e) { + res.statusCode = 400 + await promises.unlink(backendPath) + console.log(e) + return res.end() + } + } else { + res.statusCode = 200 + let builder = {} + if (pseudo) { + builder.pseudo = pseudo + } + if (location) { + builder.location = parseInt(location) + + } + res.end(JSON.stringify(await Photo.findAll(Object.keys(builder).length > 0 ? {where: builder} : undefined))) + } +} + +// Stream.Readable +function streamToBuffer(str){ + return new Promise((resolve, reject) => { + const chunks = [] + str.once('end', () => resolve(Buffer.concat(chunks))) + str.once('error', err => reject(err)) + str.on('data', chunk => chunks.push(chunk)) + }) +} + +export const config = { + api: { + bodyParser: false + } +} diff --git a/public/uploads/.gitkeep b/public/uploads/.gitkeep new file mode 100644 index 0000000..e69de29