Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement puzzles API; #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api/handlers/puzzle-session.js
Original file line number Diff line number Diff line change
@@ -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
};
19 changes: 19 additions & 0 deletions src/api/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
46 changes: 46 additions & 0 deletions src/services/puzzle-session-service.js
Original file line number Diff line number Diff line change
@@ -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);
});
},
};