Skip to content

Commit

Permalink
return updated chip copy on update
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAccords committed Apr 20, 2018
1 parent 8d60dcc commit fcd8514
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 210 deletions.
Binary file modified dump.rdb
Binary file not shown.
2 changes: 1 addition & 1 deletion server/config/accessControlConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {
'delete:own': ['*']
},
folder: {
'create:any': ['*'],
'create:own': ['*'],
'read:any': ['*'],
'update:own': ['*'],
'delete:own': ['*']
Expand Down
165 changes: 1 addition & 164 deletions server/controllers/chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,167 +40,4 @@ module.exports = {

ctrlHelpers.handleResponse(true, res, 200, 'success', chips);
}
}
// get all chips
/**
// get all chips by single primary game
router.get(`${BASE_URL}/primary/:id`, async (ctx) => {
try {
const gameId = ctx.params.id
const game = await Game.query().findById(gameId);
if(game) {
const chips = await Chip.query().where('primary_game_id', gameId)
.eager('chip_codes');
ctx.body = {
status: 'success',
data: chips
};
} else {
ctx.status = 404;
ctx.body = {
status: 'error',
message: 'That game does not exist'
};
}
} catch (err) {
console.log(err);
ctx.throw(500, err);
}
});
// get all chips by sub game
router.get(`${BASE_URL}/sub/:id`, async (ctx) => {
try {
const gameId = ctx.params.id
const game = await Game.query().findById(gameId);
if(game) {
const chips = await Chip.query().where('sub_game_id', gameId);
ctx.body = {
status: 'success',
data: chips
};
} else {
ctx.status = 404;
ctx.body = {
status: 'error',
message: 'That game does not exist'
};
}
} catch (err) {
console.log(err);
ctx.throw(500, err);
}
});
// get all chips by primary game + sub game
router.get(`${BASE_URL}/all_chips/:main/:sub`, async (ctx) => {
try {
const primaryGameId = ctx.params.main;
const subGameId = ctx.params.sub;
const primaryGame = await Game.query().findById(primaryGameId);
const subGame = await Game.query().findById(subGameId);
if(primaryGame && subGame) {
const primaryChips = await Chip.query().where('primary_game_id', primaryGame.id)
.andWhere('sub_game_id', null);
const subChips = await Chip.query().where('sub_game_id', subGame.id);
const allChips = primaryChips.concat(subChips);
ctx.body = {
status: 'success',
data: allChips
};
} else {
ctx.status = 404;
ctx.body = {
status: 'error',
message: 'That game and subgame do not exist'
};
}
} catch (err) {
console.log(err);
ctx.throw(500, err);
}
});
// create new chip
router.post(BASE_URL, async(ctx) => {
try {
chipParams = ctx.request.body;
const chip = await Chip.query()
.insert(chipParams);
// success route
ctx.status = 201;
ctx.body = {
status: 'success',
data: chip
};
// error route
} catch(err) {
console.log(err);
// model validation errors thrown here
ctx.status = 400;
ctx.body = {
status: 'error',
message: err || 'something went wrong'
};
ctx.throw(401, err);
}
});
// update single chip
router.put(`${BASE_URL}/:id`, async(ctx) => {
try {
const id = ctx.params.id;
const chip = await Chip.query().findById(id);
const chipParams = ctx.request.body;
// check if chip was found
if(chip) {
// TODO: add authorization by author id, or moderator/admin role
const updatedChip = await Chip.query()
.patchAndFetchById(chip.id, chipParams);
// success route
ctx.status = 200;
ctx.body = {
status: 'success',
data: updatedChip
};
} else {
ctx.status = 404;
ctx.body = {
status: 'error',
message: 'That chip does not exist.'
};
}
// error route
} catch(err) {
console.log(err);
// model validation errors thrown here
ctx.status = 400;
ctx.body = {
status: 'error',
message: err || 'something went wrong'
};
// TODO: pretty this up, so a status is set as well
ctx.throw(400, err);
}
})
*/
}
53 changes: 8 additions & 45 deletions server/controllers/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module.exports = {
createChipCopy: async function(req, res) {
const id = req.params.id;
const chipCopyParams = req.body;
// IDEA: add better schema validation for updating values
chipCopyParams.folder_id = id; // override passed in folder id as needed
const newChipCopy = await ChipCopy.query().insert(chipCopyParams);

ctrlHelpers.handleResponse(true, res, 200, 'success', newChipCopy);
Expand All @@ -80,7 +82,11 @@ module.exports = {
.andWhere('folder_id', folder.id);

if(updatedChipCopy > 0) {
ctrlHelpers.handleResponse(true, res, 200, 'success');
const fetchedUpdatedChipCopy = await ChipCopy.query()
.where('id', copyId)
.andWhere('folder_id', folder.id)
.first();
ctrlHelpers.handleResponse(true, res, 200, 'success', fetchedUpdatedChipCopy);
} else {
throw Boom.notFound(`Chip copy with id of ${copyId} not found.`);
}
Expand All @@ -103,47 +109,4 @@ module.exports = {
throw Boom.notFound(`Chip copy with id of ${copyId} not found.`);
}
}
}

// router.get(BASE_URL, async (ctx) => {
// });

// // create a new chip copy for a folder
// router.post(`${BASE_URL}/:id/chips`, async(ctx) => {
// const id = ctx.params.id;
// const chipCopyParams = ctx.request.body;
// const newChipCopy = await ChipCopy.query().insert(chipCopyParams);

// ctx.body = {
// status: 'success',
// data: newChipCopy
// };
// });

// // create a new chip copy for a folder
// router.put(`${BASE_URL}/:folderId/chips/:chipCopyId`, async(ctx) => {
// const folderId = ctx.params.folderId;
// const chipCopyId = ctx.params.chipCopyId

// const chipCopyParams = ctx.request.body;
// const updatedChipCopy = await ChipCopy.query()
// .patchAndFetchById(chipCopyId, chipCopyParams);


// ctx.body = {
// status: 'success',
// data: updatedChipCopy
// };
// });

// router.del(`${BASE_URL}/:folderId/chips/:chipCopyId`, async(ctx) => {
// const chipCopyId = ctx.params.chipCopyId;
// const result = await ChipCopy.query()
// .delete()
// .where('id', chipCopyId);

// ctx.body = {
// status: 'success',
// data: result
// };
// });
}
6 changes: 6 additions & 0 deletions server/routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ router.post(`${folderBaseUrl}/:id/${chipCopyUrl}`,
isAllowed.check({
resource : folderResource,
action: 'create',
checkOwnerShip : true,
useModel: true,
operands : [
{ source : 'user', key : 'id' },
{ source : 'params', key : 'id', modelName: folderResource, modelKey: 'id', opKey: 'author_id' }
]
}),
folderController.createChipCopy);

Expand Down

0 comments on commit fcd8514

Please sign in to comment.