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 16 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
108 changes: 100 additions & 8 deletions components/studentComboBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,52 @@
@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 height="200px">
<tr
v-for="pendency of totalPendencies"
:key="pendency.id"
class="field"
>
<td>{{ pendency.name }}</td>
<td>
<b-checkbox
v-model="studentPendencies"
:native-value="pendency"
:disabled="!canEdit"
></b-checkbox>
</td>
</tr>
</tbody>
</table>
<b-button @click="updatePendencies">Confirmar</b-button>
<div class="level-right">
<b-pagination
:total="totalPendenciesLength"
lubien marked this conversation as resolved.
Show resolved Hide resolved
:current.sync="currentPendenciesPage"
:order="'default'"
:size="'default'"
:simple="false"
:rounded="false"
:per-page="10"
@change="onPageChange"
></b-pagination>
</div>
</div>
</div>
</div>
</b-modal>
<br />
</div>
<div class="column is-half">
Expand Down Expand Up @@ -242,15 +282,19 @@ export default {
ataCheck: false,
laudaCheck: false,
presCheck: false,
showPendencies: false,
ataDocument: {},
laudaDocument: {},
presDocument: {},
uploadFile: File,
crg: '',
pendencies: '',
totalPendencies: [],
studentPendencies: [],
studentData: Object.assign({}, this.student),
isLoading: false,
defenseDate: new Date()
defenseDate: new Date(),
currentPendenciesPage: 1,
totalPendenciesLength: 0
}
},
computed: {
Expand Down Expand Up @@ -319,6 +363,12 @@ export default {
)
)
: null
this.$axios
.get(`/api/students/${this.student.id}/pendencies`)
.then(response => {
this.studentPendencies = response.data
})
.catch(e => this.openErrorNotification(e))
},

methods: {
Expand Down Expand Up @@ -375,6 +425,44 @@ export default {
toggleEdit() {
this.canEdit = !this.canEdit
},
getPendencies() {
this.$axios
.get(`/api/subjects`, {
params: {
page: this.currentPendenciesPage
}
})
.then(response => {
this.totalPendenciesLength = response.headers['pagination-row-count']
this.totalPendencies = 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`,
this.studentPendencies
souzaluuk marked this conversation as resolved.
Show resolved Hide resolved
)
.then(response => {
this.$toast.open({
message: 'Pendências de aluno atualizadas com sucesso',
type: 'is-success'
})
})
.catch(e => this.openErrorNotification(e))
}
this.showPendencies = false
},
onPageChange(page) {
this.currentPendenciesPage = page
this.getPendencies()
},
mapDocuments(documents) {
documents.forEach(element => {
if (element.type === 1) {
Expand Down Expand Up @@ -478,4 +566,8 @@ export default {
.icon {
margin-left: 1em;
}

.scrollable {
overflow-y: scroll;
}
</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()))
}
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
}
41 changes: 41 additions & 0 deletions server/controllers/pendencies/Update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const errors = require('../../../shared/errors')
lubien marked this conversation as resolved.
Show resolved Hide resolved
const Students = require('../../models/Student')
const Pendencies = require('../../models/Pendency')
const { knex } = require('../../db')

module.exports = async function updatePendencies(ctx) {
lubien marked this conversation as resolved.
Show resolved Hide resolved
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()
}
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 Update = require('./Update')
lubien marked this conversation as resolved.
Show resolved Hide resolved

module.exports = { Show, List, Update }
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
8 changes: 8 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,19 @@ 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.Update)
lubien marked this conversation as resolved.
Show resolved Hide resolved

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