Skip to content

Commit

Permalink
Merge branch 'caps-api' of github.com:Unipisa/caps into caps-api
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Aug 29, 2023
2 parents 6751cd0 + d1c8ad1 commit f14e1fc
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 157 deletions.
17 changes: 7 additions & 10 deletions api/controllers/AttachmentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ const attachmentHandler = multer({
const AttachmentController = {

index: async req => {
return await ModelController.index(req, {
Model: Attachment,
fields
});
const query = req.query
return await ModelController.index(Attachment, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Attachment,
fields
})
const { id } = req.params
return await ModelController.view(Attachment, id)
},

viewContent: async (req, res, next) => {
Expand Down Expand Up @@ -81,14 +77,15 @@ const AttachmentController = {
post: async req => {
let reply = []
for (const file of req.files) {
const attachment_id = await ModelController.insert(Attachment, {
const data = {
filename: file.originalname,
mimetype: file.mimetype,
encoding: file.encoding,
size: file.size,
content: file.filename,
uploader_id: req.body.uploader_id
})
}
const attachment_id = await ModelController.insert(Attachment, data)
reply.push(attachment_id)
}
return reply
Expand Down
33 changes: 6 additions & 27 deletions api/controllers/CommentController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const ModelController = require('./ModelController');
const Comment = require('../models/Comment');

const { BadRequestError } = require('../exceptions/ApiException');
const AttachmentController = require('./AttachmentController');

const fields = {
"creator_id": {
can_filter: true,
Expand All @@ -25,40 +22,22 @@ const fields = {
const CommentController = {

index: async req => {
return await ModelController.index(req, {
Model: Comment,
fields,
populate: 'attachments creator_id'
});
const query = req.query
return await ModelController.index(Comment, query, fields, { populate: 'attachments creator_id' });
},

view: async req => {
return await ModelController.view(req, {
Model: Comment,
fields
})
const { id } = req.params
return await ModelController.view(Comment, id)
},

post: async req => {
return await ModelController.insert(Comment, req.body)
const data = req.body
return await ModelController.insert(Comment, data)
},

delete: async req => {
const { id } = req.params;
try {
const comment = await ModelController.view(req, { Model: Comment })
for (const attachment_id of comment.attachments) {
const attachmentOccurrence = await ModelController.index({ query: { attachments: attachment_id }}, {
Model: Comment,
fields
})
if (attachmentOccurrence.total === 1) {
await AttachmentController.delete({ params: { id: attachment_id }})
}
}
} catch(e) {
throw new BadRequestError(e)
}
return await ModelController.delete(Comment, id);
}
}
Expand Down
14 changes: 5 additions & 9 deletions api/controllers/CurriculaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ const fields = {
const CurriculaController = {

index: async req => {
return await ModelController.index(req, {
Model: Curriculum,
fields
});
const query = req.query
return await ModelController.index(Curriculum, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Curriculum,
fields
})
const { id } = req.params
return await ModelController.view(Curriculum, id)
},

post: async req => {
Expand All @@ -39,4 +35,4 @@ const CurriculaController = {
}
}

module.exports = CurriculaController;
module.exports = CurriculaController;
14 changes: 5 additions & 9 deletions api/controllers/DegreesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ const fields = {
const DegreesController = {

index: async req => {
return await ModelController.index(req, {
Model: Degree,
fields
});
const query = req.query
return await ModelController.index(Degree, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Degree,
fields
})
const { id } = req.params
return await ModelController.view(Degree, id)
},

post: async req => {
Expand All @@ -40,4 +36,4 @@ const DegreesController = {
}
}

module.exports = DegreesController;
module.exports = DegreesController;
20 changes: 8 additions & 12 deletions api/controllers/ExamsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,24 @@ const fields = {
const ExamsController = {

index: async req => {
return await ModelController.index(req, {
Model: Exam,
fields
});
const query = req.query
return await ModelController.index(Exam, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Exam,
fields
})
const { id } = req.params
return await ModelController.view(Exam, id)
},

update: async req => {
const { id } = req.params;
// Some pre-processing might be needed here on req.body
// for tags (eg remove duplicate etc)
return await ModelController.update(Exam, id, req.body);
const data = req.body
return await ModelController.update(Exam, id, data);
},

insert: async req => {
return await ModelController.insert(Exam, req.body);
const data = req.body
return await ModelController.insert(Exam, data);
},

delete: async req => {
Expand Down
14 changes: 5 additions & 9 deletions api/controllers/FormTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ const fields = {
const FormTemplatesController = {

index: async req => {
return await ModelController.index(req, {
Model: FormTemplate,
fields
});
const query = req.query
return await ModelController.index(FormTemplate, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: FormTemplate,
fields
})
const { id } = req.params
return await ModelController.view(FormTemplate, id)
},

post: async req => {
Expand All @@ -35,4 +31,4 @@ const FormTemplatesController = {
}
}

module.exports = FormTemplatesController;
module.exports = FormTemplatesController;
13 changes: 5 additions & 8 deletions api/controllers/FormsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ const fields = {

const FormsController = {
index: async req => {
return await ModelController.index(req, {
Model: Form, fields
});
const query = req.query
return await ModelController.index(Form, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Form,
fields
})
const { id } = req.params
return await ModelController.view(Form, id)
},

post: async req => {
Expand All @@ -40,4 +37,4 @@ const FormsController = {
}
}

module.exports = FormsController;
module.exports = FormsController;
49 changes: 25 additions & 24 deletions api/controllers/ModelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,17 @@
const { default: mongoose } = require("mongoose");
const { BadRequestError, ValidationError, NotImplementedError } = require("../exceptions/ApiException");

const validateModel = (Model, data) => {
const validationTest = (new Model(data)).validateSync()
if (validationTest) {
let errors = {}
for (const err in validationTest.errors) {
errors[err] = validationTest.errors[err].message
}
throw new ValidationError(errors)
}
}

const ModelController = {

index: async (req, {
Model, fields, populate }
) => {
index: async (Model, query, fields, { populate } = {}) => {
let $match = {};
let filter = {};
let sort = "_id";
let direction = 1;
let limit = 100;

for (key in req.query) {
const value = req.query[key];
for (key in query) {
const value = query[key];
if (key == '_direction') {
if (value=="1") direction = 1;
else if (value=="-1") direction = -1;
Expand Down Expand Up @@ -102,34 +89,48 @@ const ModelController = {
};
},

view: async (req, { Model }) => {
const { id } = req.params
view: async (Model, id, { populate } = {}) => {
try {
const obj = await Model.findById(id)
return obj
if (populate !== undefined) return await obj.populate(populate)
else return obj
} catch(err) {
console.log(`not found ${id}`)
throw new BadRequestError()
}
},

update: async (Model, id, data) => {
validateModel(Model, data)
try {
await Model.findByIdAndUpdate(id, { $set: data})
await Model.findByIdAndUpdate(id, data, { runValidators: true })
} catch(err) {
throw new BadRequestError()
if (err instanceof mongoose.Error.ValidationError) {
let validationErrors = {}
for (const field in err.errors) {
validationErrors[field] = err.errors[field].message
}
throw new ValidationError(validationErrors)
} else {
throw new BadRequestError()
}
}
},

insert: async (Model, data) => {
validateModel(Model, data)
try {
const entry = new Model(data)
await entry.save()
return entry._id
} catch (err) {
throw new BadRequestError()
if (err instanceof mongoose.Error.ValidationError) {
let validationErrors = {}
for (const field in err.errors) {
validationErrors[field] = err.errors[field].message
}
throw new ValidationError(validationErrors)
} else {
throw new BadRequestError()
}
}
},

Expand Down
12 changes: 4 additions & 8 deletions api/controllers/ProposalsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ const fields = {
const ProposalsController = {

index: async req => {
return await ModelController.index(req, {
Model: Proposal,
fields
});
const query = req.query
return await ModelController.index(Proposal, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: Proposal,
fields
})
const { id } = req.params
return await ModelController.view(Proposal, id)
},

post: async req => {
Expand Down
24 changes: 14 additions & 10 deletions api/controllers/UsersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ const fields = {
const UsersController = {

index: async req => {
return await ModelController.index(req, {
Model: User, fields
});
const query = req.query
return await ModelController.index(User, query, fields);
},

view: async req => {
return await ModelController.view(req, {
Model: User, fields
})
const { id } = req.params
return await ModelController.view(User, id, { populate: { path: 'comments', populate: { path: 'attachments creator_id' } } })
},

post: async req => {
const item = new User(req.body);
return await item.save();
update: async req => {
const { id } = req.params
const data = req.body
return await ModelController.update(User, id, data)
},

insert: async req => {
const data = req.body
return await ModelController.insert(User, data)
}
}

module.exports = UsersController;
module.exports = UsersController;
Loading

0 comments on commit f14e1fc

Please sign in to comment.