categories crud methods working

This commit is contained in:
Annika
2022-07-01 16:25:00 -06:00
parent a943a20d25
commit bffc0564ea
2 changed files with 24 additions and 10 deletions
@@ -13,6 +13,7 @@ export class CategoriesController extends BaseController {
.get('', this.getAll) .get('', this.getAll)
.get('/:id', this.getById) .get('/:id', this.getById)
.put('/:id', this.modify) .put('/:id', this.modify)
.delete('/:id', this.remove)
} }
async create(req, res, next) { async create(req, res, next) {
@@ -43,15 +44,21 @@ export class CategoriesController extends BaseController {
} }
} }
// TODO modify()
async modify(req, res, next) { async modify(req, res, next) {
try { try {
const data = await categoriesService.modify(res.data, req.params.id, req.userInfo.id) const data = await categoriesService.modify(req.body, req.params.id, req.userInfo.id)
return res.send(data) return res.send(data)
} catch (error) { } catch (error) {
next(error) next(error)
} }
} }
// TODO delete() async remove(req, res, next) {
try {
const data = await categoriesService.remove(req.params.id, req.userInfo.id)
return res.send(data)
} catch (error) {
next(error)
}
}
} }
+14 -7
View File
@@ -20,14 +20,11 @@ class CategoriesService {
} }
async getById(categoryId, accountId) { async getById(categoryId, accountId) {
const category = await dbContext.Categories.find({accountId: accountId, _id: categoryId}) // needs to be findOne to not wrap object inside an object
const category = await dbContext.Categories.findOne({accountId: accountId, _id: categoryId})
if (!category){ if (!category){
throw new BadRequest('Invalid Id') throw new BadRequest('Invalid Id')
} }
// if (accountId != category.accountId) {
// // TODO this still is a data leak, because it tells the attacker that the category exists
// throw new BadRequest('das nacho category')
// }
return category return category
} }
@@ -37,16 +34,26 @@ class CategoriesService {
if (!original) { if (!original) {
throw new BadRequest('category does not exist') throw new BadRequest('category does not exist')
} }
// throw new BadRequest(original)
original.name = update.name || original.name original.name = update.name || original.name
original.budgeted = update.budgeted || original.budgeted original.budgeted = update.budgeted || original.budgeted
original.targetAmt = update.targetAmt || original.targetAmt original.targetAmt = update.targetAmt || original.targetAmt
original.targetStart = update.targetStart || original.targetStart original.targetStart = update.targetStart || original.targetStart
original.targetFreq = update.targetFreq || original.targetFreq original.targetFreq = update.targetFreq || original.targetFreq
original.targetUnit = update.targetUnit || original.targetUnit original.targetUnit = update.targetUnit || original.targetUnit
update = await dbContext.Categories.findByIdAndUpdate(categoryId, original) await original.save()
// this works because original is a mongoose object - but it only works if it's findOne - not an object of object
return original
} }
// TODO delete() async remove(categoryId, accountId) {
const category = await this.getById(categoryId, accountId)
if (!category) {
throw new BadRequest('category does not exist')
}
category.remove()
return category
}
} }
export const categoriesService = new CategoriesService export const categoriesService = new CategoriesService