|
| 1 | +import { Response, SimpleHandler } from '@/http/RequestHandler' |
| 2 | +import { GlueBoardDoc } from '@@/migrate/schemas/glue-board' |
| 3 | +import GlueBoard from '@@/migrate/models/glue-board' |
| 4 | +import { FragmentDoc } from '@@/migrate/schemas/fragment' |
| 5 | + |
| 6 | +interface GetHashResponseBody { |
| 7 | + hash: string |
| 8 | +} |
| 9 | + |
| 10 | +interface GetResponseBody { |
| 11 | + category: { |
| 12 | + name: string |
| 13 | + color: string |
| 14 | + } |
| 15 | + fragments: Array<{ |
| 16 | + url: string |
| 17 | + selector: string |
| 18 | + xPos: number |
| 19 | + yPos: number |
| 20 | + scale: number |
| 21 | + }> |
| 22 | +} |
| 23 | + |
| 24 | +export default class SharingController { |
| 25 | + /** |
| 26 | + * Get the url hash of the shared GlueBoard |
| 27 | + */ |
| 28 | + public static getHash(): SimpleHandler { |
| 29 | + return (req, res): Response => { |
| 30 | + const glueBoard = res.locals.glueBoard as GlueBoardDoc |
| 31 | + |
| 32 | + // if sharing option is off, reject this request. |
| 33 | + if (glueBoard.sharing === false) { |
| 34 | + return res.status(403).json({ |
| 35 | + err: { |
| 36 | + msg: 'Sharing option for this GlueBoard is off.' |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + const responseBody: GetHashResponseBody = { |
| 42 | + hash: glueBoard.id |
| 43 | + } |
| 44 | + |
| 45 | + return res.status(200).json(responseBody) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Access to the shared GlueBoard |
| 51 | + */ |
| 52 | + public static get(): SimpleHandler { |
| 53 | + return async (req, res): Promise<Response> => { |
| 54 | + const glueBoard = (await GlueBoard.findById(res.locals.glueBoard._id, { |
| 55 | + category: 1, |
| 56 | + fragments: 1 |
| 57 | + }) |
| 58 | + .lean() |
| 59 | + .populate({ |
| 60 | + path: 'fragments', |
| 61 | + select: '-_id -id' |
| 62 | + })) as GlueBoardDoc |
| 63 | + |
| 64 | + const responseBody: GetResponseBody = { |
| 65 | + category: { |
| 66 | + name: glueBoard.category.name, |
| 67 | + color: glueBoard.category.color |
| 68 | + }, |
| 69 | + fragments: [] |
| 70 | + } |
| 71 | + |
| 72 | + // compose response body |
| 73 | + for (const fragment of glueBoard.fragments as FragmentDoc[]) { |
| 74 | + responseBody.fragments.push({ |
| 75 | + url: fragment.url, |
| 76 | + selector: fragment.selector, |
| 77 | + xPos: fragment.xPos, |
| 78 | + yPos: fragment.yPos, |
| 79 | + scale: fragment.scale |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + return res.status(200).json(responseBody) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments