From 55b66da588f00761858092295adb7035112ede87 Mon Sep 17 00:00:00 2001 From: Kiril Knysh Date: Sun, 6 Feb 2022 12:09:52 +0100 Subject: [PATCH] Implement puzzles API; --- src/api/handlers/puzzle-session.js | 6 ++++ src/api/v1.js | 19 +++++++++++ src/services/puzzle-session-service.js | 46 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/api/handlers/puzzle-session.js create mode 100644 src/services/puzzle-session-service.js diff --git a/src/api/handlers/puzzle-session.js b/src/api/handlers/puzzle-session.js new file mode 100644 index 0000000..0c78000 --- /dev/null +++ b/src/api/handlers/puzzle-session.js @@ -0,0 +1,6 @@ +const puzzleSessionService = require('../../services/puzzle-session-service'); + +exports.createPuzzleSession = async function createPuzzleSession(options) { + return puzzleSessionService.createPuzzleSession(options.puzzleSetId, options.name, options.alias, options.date); + // TODO: map db session to session +}; diff --git a/src/api/v1.js b/src/api/v1.js index b00f0cf..caebb05 100644 --- a/src/api/v1.js +++ b/src/api/v1.js @@ -2,6 +2,7 @@ const express = require('express'); const userHandlers = require('./handlers/user'); const puzzleHandlers = require('./handlers/puzzle'); const puzzleSetHandlers = require('./handlers/puzzle-set'); +const puzzleSessionHandlers = require('./handlers/puzzle-session'); const apiV1Router = express.Router(); @@ -139,4 +140,22 @@ apiV1Router.get('/listOwnSets', async (req, res) => { } }); +apiV1Router.post('/createPuzzleSession', async (req, res) => { + const required = ['name', 'puzzleSetId', 'alias', 'date']; + + const missing = required.filter((prop) => { + return !req.body[prop]; + }); + if (missing.length > 0) { + return res.status(400).json({ message: 'Request body params are missing', required, missing }); + } + + try { + const puzzleSession = await puzzleSessionHandlers.createPuzzleSession(req.body); + return res.json({ puzzleSession }); + } catch (error) { + return res.status(error.status || 500).json({ message: 'Failed to create puzzle set', error: error.message }); + } +}); + exports.router = apiV1Router; diff --git a/src/services/puzzle-session-service.js b/src/services/puzzle-session-service.js new file mode 100644 index 0000000..0071fb6 --- /dev/null +++ b/src/services/puzzle-session-service.js @@ -0,0 +1,46 @@ +const fetch = require('node-fetch'); + +const logger = require('../loggers')(); +const config = require('../config'); + +const puzzleSetService = require('./puzzle-set-service'); + +const DEFAULT_HEADERS = { 'Content-Type': 'application/json', Prefer: 'return=representation', Accept: 'application/vnd.pgrst.object+json' }; + +function handleResponse(response) { + if (response.ok) { + return response.json(); + } + + return response.json().then((body) => { + return Promise.reject({ body, status: response.status, statusText: response.statusText }); + }); +} + +module.exports = { + async createPuzzleSession(setId, name, alias, date) { + const set = await puzzleSetService.getFullPuzzleSet(setId); + + if (!set) { + const error = new Error('Cannot find puzzle set'); + error.status = 404; + return Promise.reject(error); + } + + const url = new URL(`${config.get('DB:API:ORIGIN')}/sessions`); + + return fetch(url.toString(), { + method: 'post', + body: JSON.stringify({ + puzzle_set: setId, name, url_alias: alias, date, + }), + headers: DEFAULT_HEADERS, + }).then(handleResponse).catch((error) => { + logger.error('Failed to createPuzzleSet', error); + + const clientError = new Error('Failed to create puzzle set'); + clientError.status = 500; + return Promise.reject(clientError); + }); + }, +};