Skip to content

Commit

Permalink
Aggiunti modelli e API per allegati e commenti
Browse files Browse the repository at this point in the history
l'API è solo un mockup e deve essere integrata con il middleware multer
di express per la gestione dei file
  • Loading branch information
Fran314 committed Aug 8, 2023
1 parent c6ec57a commit 0bc6c4e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
44 changes: 44 additions & 0 deletions api/controllers/AttachmentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const ModelController = require('./ModelController');
const Attachment = require('../models/Attachment');

const fields = {
"mimetype": {
can_filter: true,
},
"uploader_id": {
can_filter: true,
can_sort: true,
match_id_object: true,
},
"size": {
can_filter: true,
can_sort: true
},
};

const AttachmentController = {

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

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

post: async req => {
// TODO: il contenuto dell'allegato deve essere gestito all'interno
// di questo metodo, probabilmente utlizzando il middleware `multer`
// per express, modificando poi il body all'occorrenza
const attachment = new Attachment(req.body);
return await attachment.save();
}
}

module.exports = AttachmentController;
47 changes: 47 additions & 0 deletions api/controllers/CommentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const ModelController = require('./ModelController');
const Comment = require('../models/Comment');

const fields = {
"creator_id": {
can_filter: true,
can_sort: true,
match_id_object: true,
},
"modifier_id": {
can_filter: true,
can_sort: true,
match_id_object: true,
},
"academic_year": {
can_filter: true,
can_sort: true
},
"degree.name": {
can_filter: true,
can_sort: true
}
};

const CommentController = {

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

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

post: async req => {
const comment = new Comment(req.body);
return await comment.save();
}
}

module.exports = CommentController;
9 changes: 8 additions & 1 deletion api/migrate-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ let Curriculum = require('./models/Curriculum')
let FormTemplate = require('./models/FormTemplate')
let Form = require('./models/Form')
let Proposal = require('./models/Proposal')
let Attachment = require('./models/Attachment')
let Comment = require('./models/Comment')

let { CurriculumCompulsoryExam,
CurriculumCompulsoryGroup,
Expand Down Expand Up @@ -325,6 +327,11 @@ async function importData() {
const e = new Proposal(element)
return e.save()
}))

// Temporaneo, solo per creare la collezione. Da sostituire con
// l'effettiva migrazione degli allegati
Attachment.createCollection();
Comment.createCollection();

// import form_templates
write("> Form templates")
Expand Down Expand Up @@ -369,4 +376,4 @@ async function importData() {
}

write("Starting the import of the data from MySQL to MongoDB")
importData().then((res) => write("Import done"));
importData().then((res) => write("Import done"));
19 changes: 19 additions & 0 deletions api/models/Attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mongoose = require('mongoose');

const Attachment = mongoose.model('Attachment', {
mimetype: {
type: String
},
uploader_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
size: {
type: String
},
content: {
type: String
}
})

module.exports = Attachment;
20 changes: 20 additions & 0 deletions api/models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose');

const Comment = mongoose.model('Comment', {
creator_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
modifier_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
content: {
type: String
},
attachments: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'Attachment' }
]
})

module.exports = Comment;
8 changes: 8 additions & 0 deletions api/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const Curricula = require('./controllers/CurriculaController')
const FormTemplates = require('./controllers/FormTemplates')
const Forms = require('./controllers/FormsController')
const Proposals = require('./controllers/ProposalsController')
const Attachments = require('./controllers/AttachmentController')
const Comments = require('./controllers/CommentController')

// JSON parsing middleware
router.use(express.json())
Expand Down Expand Up @@ -51,6 +53,12 @@ router.post('/exams', response_envelope(Exams.insert))
router.get('/users', response_envelope(Users.index))
router.get('/users/:id', response_envelope(Users.view))
router.post('/users', response_envelope(Users.post))
router.get('/comments', response_envelope(Comments.index))
router.get('/comments/:id', response_envelope(Comments.view))
router.post('/comments', response_envelope(Comments.post))
router.get('/attachments', response_envelope(Attachments.index))
router.get('/attachments/:id', response_envelope(Attachments.view))
router.post('/attachments', response_envelope(Attachments.post))

router.all(/.*/, response_envelope((req) => {throw new NotFoundError()}))

Expand Down

0 comments on commit 0bc6c4e

Please sign in to comment.