|
1 | 1 | import { Router } from "express"; |
2 | | -import { createDiff } from "../services/createDiff.js"; |
3 | | -import { getInputModelsOrError } from "../utils/prepInputModels.js"; |
| 2 | +import { getInputModelsOrError, prepForPatch } from "../utils/prepInput.js"; |
| 3 | +import * as comparissonMergingService from "../services/comparisonMerging.service.js"; |
| 4 | +import * as patchingService from "../services/patching.service.js"; |
4 | 5 |
|
5 | 6 | const dicome = Router(); |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Compare two models and detect conflicts |
9 | 10 | * |
10 | | - * @param {InputModels} req.body - The first operand |
11 | | - * @returns {DiffModel} The sum of both operands |
| 11 | + * @param {InputModels} req.body |
| 12 | + * @returns {DiffModel} |
12 | 13 | */ |
13 | 14 | dicome.post("/compare", (req, res) => { |
14 | 15 | console.log("request body", req.body); |
15 | 16 | const inputModelsOrError = getInputModelsOrError(req.body, req.session); |
16 | 17 |
|
17 | 18 | if (typeof inputModelsOrError === "string") { |
18 | 19 | console.log(inputModelsOrError); |
19 | | - res.status(500).send(inputModelsOrError); |
| 20 | + res.status(400).send(inputModelsOrError); |
20 | 21 | } else { |
21 | | - const diffModel = createDiff(inputModelsOrError); |
| 22 | + const diffModel = comparissonMergingService.createDiff(inputModelsOrError); |
22 | 23 | req.session.diffModel = diffModel; |
23 | 24 | res.send(diffModel); |
24 | 25 | } |
25 | 26 | }); |
26 | 27 |
|
27 | 28 | /** |
28 | 29 | * Apply differences |
| 30 | + * @description for conflict: opposing change is automatically rejected (DISCARD) |
| 31 | + * |
| 32 | + * changes State of respective differences in DiffModel |
29 | 33 | * |
30 | | - * for conflicts: if this is a left change then right automatically rejected and vis a versa |
31 | | - * |
32 | | - * @param {number[]} req.body - non-empty list of DiffModel Differences IDs |
33 | | - * @returns {object} JSON patch (if technically possible) of diff model and left/right |
| 34 | + * @param {leftIds: number[], rightIds: number[]} req.body - json with a list of DiffModel Differences IDs coming from left and right side |
| 35 | + * @returns {object} JSON patch |
| 36 | + * |
34 | 37 | */ |
35 | 38 | dicome.put("/apply", (req, res) => { |
36 | | - const sessionDiffModel = req.session.diffModel; |
| 39 | + const userInputOrError = prepForPatch(req.body, req.session); |
| 40 | + |
| 41 | + if (typeof userInputOrError === "string") { |
| 42 | + res.status(400).send(userInputOrError); |
| 43 | + } else { |
| 44 | + const applyResp = patchingService.applyDifferences(userInputOrError.leftIds as number[], userInputOrError.rightIds as number[], userInputOrError.diffmodel); |
| 45 | + |
| 46 | + res.sendStatus(applyResp); |
| 47 | + } |
37 | 48 |
|
38 | | - // TODO |
39 | | - // create service |
40 | | - // get differences from id array |
41 | | - // ... |
42 | | - // change status of conflict if 3 way |
| 49 | + // TODO: Test if diff model in sess is really overwritten ??? |
43 | 50 |
|
44 | | - res.sendStatus(200); |
45 | 51 | }); |
46 | 52 |
|
47 | 53 | /** |
48 | 54 | * Discard differences |
| 55 | + * @description if conflict: opposing difference will NOT be applied automatical |
| 56 | + * |
| 57 | + * changes State of respective differences in DiffModel |
49 | 58 | * |
50 | | - * for conflicts: if this is a left change then right will NOT be applied automatical, |
51 | | - * the change has to be in list of apply endpoint |
52 | | - * and vis a versa |
53 | | - * |
54 | | - * @param {number[]} req.body - non empty list of DiffModel Differences IDs |
55 | | - * @returns {object} JSON patch (if technically possible) of diff model and left/right |
| 59 | + * @param {leftIds: number[], rightIds: number[]} req.body - json with a list of DiffModel Differences IDs coming from left and right side |
| 60 | + * @returns {object} JSON patch |
| 61 | + * Sideeffect: |
56 | 62 | */ |
57 | 63 | dicome.put("/discard", (req, res) => { |
58 | | - const sessionDiffModel = req.session.diffModel; |
| 64 | + const userInputOrError = prepForPatch(req.body, req.session); |
| 65 | + |
| 66 | + if (typeof userInputOrError === "string") { |
| 67 | + res.status(400).send(userInputOrError); |
| 68 | + } else { |
| 69 | + const discardResp = patchingService.discardDifferences(userInputOrError.leftIds as number[], userInputOrError.rightIds as number[], userInputOrError.diffmodel); |
59 | 70 |
|
60 | | - // TODO |
61 | | - // create service |
62 | | - // get differences from id array |
63 | | - // ... |
64 | | - // change status of conflict if 3 way |
| 71 | + res.sendStatus(discardResp); |
| 72 | + } |
65 | 73 |
|
66 | | - res.sendStatus(200); |
| 74 | + // TODO: Test if diff model in sess is really overwritten ??? |
67 | 75 | }); |
68 | 76 |
|
69 | 77 | dicome.delete("/session", (req, res) => { |
|
0 commit comments