Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD de Pendências #147

Merged
merged 22 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 92 additions & 7 deletions components/studentComboBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,44 @@
@blur="onCrgBlur"
></b-input>
<br />
<strong>Componentes Pendentes</strong>:
<b-input
v-model="pendencies"
:disabled="!canEdit"
:value="studentData.pendencies"
></b-input>
<b-button class="is-primary" @click="getPendencies">
{{ !canEdit ? 'Verificar Pendências' : 'Editar Pendências' }}
</b-button>
<b-modal :active.sync="showPendencies">
<div class="card">
<header class="card-header">
<b-icon pack="fas" icon="check" size="is-medium"></b-icon>
<p class="card-header-title">Pendências</p>
</header>
<div class="card-content">
<div class="content">
<table class="table is-narrow scrollable">
<tbody>
<tr
v-for="subject of totalSubjects"
:key="subject.id"
class="field"
>
<td>{{ subject.name }}</td>
<td>
<b-checkbox
v-model="studentSubjects"
:native-value="subject.id"
:disabled="!canEdit"
></b-checkbox>
</td>
</tr>
</tbody>
</table>
<div class="modal-card-foot bottom-sticky">
<b-button @click="updatePendencies">
Confirmar
</b-button>
</div>
</div>
</div>
</div>
</b-modal>
<br />
</div>
<div class="column is-half">
Expand Down Expand Up @@ -242,12 +274,14 @@ export default {
ataCheck: false,
laudaCheck: false,
presCheck: false,
showPendencies: false,
ataDocument: {},
laudaDocument: {},
presDocument: {},
uploadFile: File,
crg: '',
pendencies: '',
totalSubjects: [],
studentSubjects: [],
studentData: Object.assign({}, this.student),
isLoading: false,
defenseDate: new Date()
Expand Down Expand Up @@ -319,6 +353,14 @@ export default {
)
)
: null
this.$axios
.get(`/api/students/${this.student.id}/pendencies`)
.then(response => {
this.studentSubjects = response.data.map(
pendency => pendency.subjectId
)
})
.catch(e => this.openErrorNotification(e))
},

methods: {
Expand Down Expand Up @@ -375,6 +417,40 @@ export default {
toggleEdit() {
this.canEdit = !this.canEdit
},
getPendencies() {
this.$axios
.get(`/api/subjects`, {
params: {
paginate: 0
}
})
.then(response => {
this.totalSubjectsLength = response.headers['pagination-row-count']
this.totalSubjects = response.data
this.showPendencies = true
})
.catch(e => {
this.showPendencies = false
this.openErrorNotification(e)
})
},
updatePendencies() {
if (this.canEdit) {
this.$axios
.post(
`/api/students/${this.student.id}/pendencies/batch`,
this.studentSubjects
)
.then(response => {
this.$toast.open({
message: 'Pendências de aluno atualizadas com sucesso',
type: 'is-success'
})
})
.catch(e => this.openErrorNotification(e))
}
this.showPendencies = false
},
mapDocuments(documents) {
documents.forEach(element => {
if (element.type === 1) {
Expand Down Expand Up @@ -478,4 +554,13 @@ export default {
.icon {
margin-left: 1em;
}

.scrollable {
overflow-y: scroll;
}

.bottom-sticky {
bottom: 0;
position: sticky;
}
</style>
26 changes: 26 additions & 0 deletions migrations/20190605024900_pendencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports.up = function(knex, Promise) {
return knex.schema.createTable('pendencies', table => {
table.increments('id').primary()
table
.integer('studentId')
.unsigned()
.notNullable()
table
.foreign('studentId')
.references('id')
.inTable('students')
table
.integer('subjectId')
.unsigned()
.notNullable()
table
.foreign('subjectId')
.references('id')
.inTable('subjects')
table.unique(['studentId', 'subjectId'])
})
}

exports.down = function(knex, Promise) {
return knex.schema.dropTable('pendencies')
}
39 changes: 39 additions & 0 deletions seeds/pendencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Pendency = require('../server/models/Pendency')

exports.seed = async function(knex, Promise) {
// Deletes ALL existing entries
await knex('pendencies').del()

const data = [
{
studentId: 1,
subjectId: 1
},
{
studentId: 1,
subjectId: 2
},
{
studentId: 1,
subjectId: 3
},
{
studentId: 2,
subjectId: 1
},
{
studentId: 3,
subjectId: 1
},
{
studentId: 3,
subjectId: 3
},
{
studentId: 5,
subjectId: 6
}
]

return Promise.all(data.map(pendency => Pendency.forge(pendency).save()))
}
41 changes: 41 additions & 0 deletions server/controllers/pendencies/FromBatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const errors = require('../../../shared/errors')
const Students = require('../../models/Student')
const Pendencies = require('../../models/Pendency')
const { knex } = require('../../db')

module.exports = async function pendenciesFromBatch(ctx) {
const { studentId } = ctx.params
const subjectsIdsReceived = ctx.request.body

if ((await Students.where('id', studentId).count()) === 0) {
ctx.status = 404
ctx.body = { code: errors.NOT_FOUND }
return
}

await knex.transaction(async trx => {
const pendenciesExisting = await Pendencies.where({ studentId }).fetchAll({
transacting: trx
})

const subjectsIdsExisting = pendenciesExisting.map(pend =>
pend.get('subjectId')
)

const addPendencies = subjectsIdsReceived.filter(
id => !subjectsIdsExisting.includes(id)
)
const delPendencies = pendenciesExisting.filter(
pendency => !subjectsIdsReceived.includes(pendency.get('subjectId'))
)
const addPromises = addPendencies.map(subjectId =>
new Pendencies({ subjectId, studentId }).save(null, { transacting: trx })
)
const delPromises = delPendencies.map(({ id }) =>
Pendencies.where({ id }).destroy({ transacting: trx })
)
return Promise.all(addPromises.concat(delPromises))
})

ctx.body = await Pendencies.where({ studentId }).fetchAll()
}
16 changes: 16 additions & 0 deletions server/controllers/pendencies/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const errors = require('../../../shared/errors')

const Students = require('../../models/Student')
const Pendencies = require('../../models/Pendency')

module.exports = async function listPendencies(ctx) {
lubien marked this conversation as resolved.
Show resolved Hide resolved
const { studentId } = ctx.params

if ((await Students.where('id', studentId).count()) === 0) {
ctx.status = 404
ctx.body = { code: errors.NOT_FOUND }
return
}

ctx.body = await Pendencies.where({ studentId }).fetchAll()
}
24 changes: 24 additions & 0 deletions server/controllers/pendencies/Show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const errors = require('../../../shared/errors')

const Students = require('../../models/Student')
const Pendencies = require('../../models/Pendency')

module.exports = async function showPendency(ctx) {
const { studentId, id } = ctx.params

if ((await Students.where('id', studentId).count()) === 0) {
ctx.status = 404
ctx.body = { code: errors.NOT_FOUND }
return
}

const pendencyFind = await Pendencies.where({ id }).fetch()

if (pendencyFind === null) {
ctx.status = 404
ctx.body = { code: errors.NOT_FOUND }
return
}

ctx.body = pendencyFind
}
5 changes: 5 additions & 0 deletions server/controllers/pendencies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Show = require('./Show')
const List = require('./List')
const FromBatch = require('./FromBatch')

module.exports = { Show, List, FromBatch }
6 changes: 5 additions & 1 deletion server/controllers/subjects/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const Subject = require('../../models/Subject')
const utils = require('../../utils')

module.exports = async function listSubjects(ctx) {
const { page = 1 } = ctx.request.query
const { page = 1, paginate } = ctx.request.query
if (paginate !== undefined && paginate === '0') {
ctx.body = await Subject.fetchAll()
return
}
utils.paginateContext(ctx, await Subject.fetchPage({ page }))
}
7 changes: 7 additions & 0 deletions server/models/Pendency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { bookshelf } = require('../db')

const Pendency = bookshelf.model('Pendency', {
tableName: 'pendencies'
})

module.exports = Pendency
12 changes: 12 additions & 0 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const students = require('./controllers/students')
const subjects = require('./controllers/subjects')
const auth = require('./controllers/auth')
const solicitations = require('./controllers/solicitations')
const pendencies = require('./controllers/pendencies')

const router = new Router()
const api = new Router({ prefix: '/api' })
Expand Down Expand Up @@ -39,12 +40,23 @@ api.put(
students.UpdateAcademicHighlight
)
api.put('/students/:id', bodyJson, students.Update)

// Documents Routes
api.get('/students/:studentId/documents', documents.List)
api.get('/students/:studentId/documents/:id', documents.Show)
api.get('/students/:studentId/documents/:id/view', documents.View)
api.post('/students/:studentId/documents', bodyMultipart, documents.Upload)
api.post('/students/from-csv', bodyMultipart, students.FromCsv)

// Pendencies Routes
api.get('/students/:studentId/pendencies/:id', pendencies.Show)
api.get('/students/:studentId/pendencies/', pendencies.List)
api.post(
'/students/:studentId/pendencies/batch',
bodyJson,
pendencies.FromBatch
)

// Subjects Routes
api.get('/subjects/', subjects.List)
api.get('/subjects/:id', subjects.Show)
Expand Down
Loading