Skip to content

Commit

Permalink
Merge pull request #525 from marcosdosea/Branch_Moises523
Browse files Browse the repository at this point in the history
Paginação em ensaio
  • Loading branch information
reinan47 authored Oct 2, 2023
2 parents c49bd80 + 36ce737 commit abf7208
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 69 deletions.
3 changes: 2 additions & 1 deletion Codigo/Core/DTO/EnsaioIndexDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class EnsaioIndexDTO
[DisplayName("Local")]
public string? Local { get; set; }
[DisplayName("Presença Obrigatória")]
public sbyte PresencaObrigatoria { get; set; }
public string? PresencaObrigatoria { get; set; }
public String? DataHora { get; set; }
}
}
4 changes: 4 additions & 0 deletions Codigo/Core/Service/IEnsaioService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Core.DTO;
using System.Data;
using System.Net;
using Core.Datatables;

namespace Core.Service
{
Expand Down Expand Up @@ -73,5 +75,7 @@ public interface IEnsaioService
/// HttpStatusCode.InternalServerError - Erro interno
/// </returns>
Task<HttpStatusCode> RegistrarJustificativaAsync(int idEnsaio, int idPessoa, string? justificativa);

Task<DatatableResponse<EnsaioIndexDTO>> GetDataPage(DatatableRequest request, int idGrupo);
}
}
7 changes: 7 additions & 0 deletions Codigo/GestaoGrupoMusicalWeb/Controllers/EnsaioController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using Core;
using Core.Datatables;
using Core.DTO;
using Core.Service;
using GestaoGrupoMusicalWeb.Models;
Expand All @@ -25,6 +26,12 @@ public EnsaioController(IMapper mapper, IEnsaioService ensaio, IPessoaService pe
_pessoa = pessoa;
_grupoMusical = grupoMusical;
}
[HttpPost]
public async Task<IActionResult> GetDataPage(DatatableRequest request)
{
var ensaios = await _ensaio.GetDataPage(request, await _grupoMusical.GetIdGrupo(User.Identity.Name));
return Json(ensaios);
}

[Authorize(Roles = "ADMINISTRADOR GRUPO,COLABORADOR,REGENTE")]
// GET: EnsaioController
Expand Down
2 changes: 1 addition & 1 deletion Codigo/GestaoGrupoMusicalWeb/Views/Ensaio/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<div class="container">
<div class="row d-flex flex-column-reverse d-grig gap-2 mx-auto d-flex flex-sm-column-reverse flex-xl-row">
<div class="col-5 d-none d-sm-block"></div>
<a class="btn btn-light text-secondary border border-secondary d-grid gap-2 col-sm" asp-action="Index"> Voltar </a>
<a class="btn btn-light text-secondary border border-secondary d-grid gap-2 col-sm" asp-controller="Ensaio" asp-action="Index"> Voltar </a>
<input type="submit" value="Salvar" class="btn btn-secondary d-grid gap-2 col-sm " />
</div>
</div>
Expand Down
111 changes: 68 additions & 43 deletions Codigo/GestaoGrupoMusicalWeb/Views/Ensaio/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@model IEnumerable<Core.DTO.EnsaioIndexDTO>

<link href="~/lib/datatable/css/datatables.min.css" rel="stylesheet" />
@{
ViewData["Title"] = "Ensaios";
}
Expand All @@ -9,7 +9,7 @@
<a role="button" class="btn btn-secondary m-3" asp-controller="Ensaio" asp-action="Create">Novo Ensaio</a>
</div>
<div class="table-responsive">
<table class="table">
<table id="tableEnsaio" class="table table-striped table-bordered dt-responsive nowrap ">
<thead>
<tr class="bg-danger bg-opacity-75 text-white">
<th>
Expand All @@ -27,47 +27,6 @@
<th>Ações</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="border-bottom border-danger border-opacity-75 p-3 align-middle">
<td>
@Html.DisplayFor(modelItem => item.DataHoraInicio)
</td>
<td>
@Html.DisplayFor(modelItem => item.Tipo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Local)
</td>
@{
String presenca = String.Empty;
if (item.PresencaObrigatoria == 1)
{
presenca = "Sim";
}
else
{
presenca = "Não";
}
}
<td>
@Html.DisplayFor(modelItem => presenca)
</td>
<td class="d-flex gap-1 border-0 flex-wrap py-3">
<a role="button" class="btn btn-secondary btn-sm mb-xl-0 mb-2" asp-controller="Ensaio" asp-action="Edit" asp-route-id="@item.Id"><i class="fa-solid fa-pen-to-square"> </i> Editar </a>
<a role="button" class="btn btn-secondary btn-sm mb-xl-0 mb-2" asp-controller="Ensaio" asp-action="Details" asp-route-id="@item.Id"><i class="fa-solid fa-file"> </i> Detalhes </a>
<form id="@(item.Id + "_confirmar")" asp-action="Delete" asp-route-id="@item.Id" method="post">
<a onclick="showModal('@(item.Id + "_confirmar")', 'ensaioModal')" class="btn btn-secondary btn-sm mb-xl-0 mb-2" role="button" data-bs-toggle="modal" data-bs-target="#ensaioModal">
<i class="fa-solid fa-xmark"> </i>
Excluir
</a>
</form>
<a role="button" class="btn btn-secondary btn-sm mb-xl-0" asp-controller="Ensaio" asp-action="RegistrarFrequencia" asp-route-idEnsaio="@item.Id"><i class="fa-solid fa-list-check"> </i> Frequência</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="modal fade" id="ensaioModal" tabindex="-1" aria-labelledby="ensaioLabel" aria-hidden="true">
Expand All @@ -87,3 +46,69 @@
</div>
</div>
</div>
@section Scripts {
<script src="~/lib/datatable/js/datatables.min.js"></script>
<script>
$(document).ready(function () {
$('#tableEnsaio').DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"pageLength": 5,
"lengthMenu": [
[5, 10, 20, -1],
[5, 10, 20, "Todos"]
],
"ajax": {
"url": "/Ensaio/GetDataPage",
"type": "POST",
"datatype": "json"
},
"columnDefs": [{
"targets": [0],
"visible": true,
"searchable": false
},
{
"targets": [3],
"visible": true,
"searchable": false,
"orderable": false
}],
"columns": [
{ "data": "dataHora", "name": "Data Hora Inicio", "autoWidth": true },
{ "data": "tipo", "name": "Tipo", "autoWidth": true },
{ "data": "local", "name": "Local", "autoWidth": true },
{ "data": "presencaObrigatoria", "name": "Presença Obrigatória", "autoWidth": true },
{
"render": function (data, type, full, meta) {
return '<a class="btn btn btn-secondary btn-sm m-2" href="/Ensaio/Edit/' + full.id + '"><i class="fa-solid fa-pen-to-square"> </i> Editar</a> ' +
'<a onclick="showConfirmationModal(' + full.id + ')" class="btn btn-sm btn-secondary" role="button"><i class="fa-solid fa-xmark"></i> Excluir</a>' +
'<a class="btn btn btn-secondary btn-sm m-2" href="/Ensaio/Details/' + full.id + '"><i class="fa-solid fa-arrow-right-arrow-left"></i> Detalhes</a> '
;
}
},
],
language: {
url: "lib/datatable/js/pt-br.json",
}
});
});
</script>

<script>
function showConfirmationModal(formId) {
var modal = document.getElementById('ensaioModal');
var modalForm = modal.querySelector('form');
modalForm.action = '/Ensaio/Delete/' + formId; // Define a ação do formulário com a rota correta
var modalBody = modal.querySelector('.modal-body');
modalBody.innerHTML = 'Deseja <b>Excluir</b> o <b>Ensaio</b>?'; // Conteúdo do modal
// Exibe o modal
var modal = new bootstrap.Modal(modal);
modal.show();
}
</script>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/css/headerLayout.css" asp-append-version="true" />
<link rel="stylesheet" href="~/GestaoGrupoMusicalWeb.styles.css" asp-append-version="true" />
<link href="~/lib/datatable/css/datatables.min.css" rel="stylesheet" />
@await RenderSectionAsync("Header", required: false)
<link rel="icon" type="image/x-icon" href="~/assets/logo_batalase_SF.png" />
</head>
Expand Down
89 changes: 65 additions & 24 deletions Codigo/Service/EnsaioService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Core;
using Core.Datatables;
using Core.DTO;
using Core.Service;
using Email;
Expand Down Expand Up @@ -28,7 +29,7 @@ public async Task<HttpStatusCode> Create(Ensaio ensaio, IEnumerable<int> idRegen
{
if (ensaio.DataHoraFim > ensaio.DataHoraInicio)
{
if(ensaio.DataHoraInicio >= DateTime.Now)
if (ensaio.DataHoraInicio >= DateTime.Now)
{
await _context.Ensaios.AddAsync(ensaio);

Expand Down Expand Up @@ -56,10 +57,10 @@ await associadosRegentes.ForEachAsync(async associadoRegente => {
IdPessoa = associadoRegente.Id,
Presente = 1
};
if(associadoRegente.IdPapelGrupo == 5 && idRegentes.Contains(associadoRegente.Id))
if (associadoRegente.IdPapelGrupo == 5 && idRegentes.Contains(associadoRegente.Id))
{
ensaioPessoa.IdPapelGrupoPapelGrupo = associadoRegente.IdPapelGrupo;
await _context.Ensaiopessoas.AddAsync(ensaioPessoa);
await _context.Ensaiopessoas.AddAsync(ensaioPessoa);
email.To.Add(associadoRegente.Email);
}
else
Expand All @@ -82,14 +83,14 @@ await associadosRegentes.ForEachAsync(async associadoRegente => {
await transaction.RollbackAsync();
return HttpStatusCode.BadRequest;
}

}
else
else
{
await transaction.RollbackAsync();
return HttpStatusCode.PreconditionFailed;
}

}
catch
{
Expand Down Expand Up @@ -145,32 +146,32 @@ public async Task<HttpStatusCode> Edit(Ensaio ensaio, IEnumerable<int> idRegente
{
using var transaction = _context.Database.BeginTransaction();

try
{
try
{
var ensaioDb = await _context.Ensaios.Where(e => e.Id == ensaio.Id).AsNoTracking().SingleOrDefaultAsync();
if(ensaioDb == null)
if (ensaioDb == null)
{
await transaction.RollbackAsync();
return HttpStatusCode.NotFound;
}

ensaio.IdColaboradorResponsavel = ensaioDb.IdColaboradorResponsavel;
ensaio.IdGrupoMusical = ensaioDb.IdGrupoMusical;

if (ensaio.DataHoraFim > ensaio.DataHoraInicio)
{
if(ensaio.DataHoraInicio >= DateTime.Now)
if (ensaio.DataHoraInicio >= DateTime.Now)
{
var idEnsaioRegentes = _context.Ensaiopessoas
.Where(ep => ep.IdEnsaio == ensaioDb.Id && ep.IdPapelGrupoPapelGrupo == 5)
.Select(ep => ep.IdPessoa).AsEnumerable();

if((idRegentes.Count() != idEnsaioRegentes.Count()) || (idRegentes.Except(idEnsaioRegentes).Any()))
if ((idRegentes.Count() != idEnsaioRegentes.Count()) || (idRegentes.Except(idEnsaioRegentes).Any()))
{
var ensaioPessoaRegente = _context.Ensaiopessoas
.Where(ep => ep.IdEnsaio == ensaioDb.Id && ep.IdPapelGrupoPapelGrupo == 5);
_context.Ensaiopessoas.RemoveRange(ensaioPessoaRegente);
foreach(int idRegente in idRegentes)
foreach (int idRegente in idRegentes)
{
Ensaiopessoa ensaioPessoa = new()
{
Expand All @@ -195,17 +196,17 @@ public async Task<HttpStatusCode> Edit(Ensaio ensaio, IEnumerable<int> idRegente
return HttpStatusCode.BadRequest;
}
}
else
else
{
await transaction.RollbackAsync();
return HttpStatusCode.PreconditionFailed;
}
}
catch
{
}
catch
{
await transaction.RollbackAsync();
return HttpStatusCode.InternalServerError;
}
}
}
/// <summary>
/// Consulta um Ensaio no banco de dados
Expand Down Expand Up @@ -243,14 +244,15 @@ public async Task<IEnumerable<EnsaioIndexDTO>> GetAllIndexDTO(int idGrupo)
{
var query = _context.Ensaios
.OrderBy(g => g.DataHoraInicio)
.Where(g => g.IdGrupoMusical == idGrupo)
.Where(g => g.IdGrupoMusical == idGrupo)
.Select(g => new EnsaioIndexDTO
{
Id = g.Id,
DataHoraInicio = g.DataHoraInicio,
DataHora = g.DataHoraInicio.ToString("dd/MM/yyyy HH:mm:ss"),
Tipo = g.Tipo,
Local = g.Local,
PresencaObrigatoria = g.PresencaObrigatoria
PresencaObrigatoria = g.PresencaObrigatoria == 1 ? "Sim" : "Não",
DataHoraInicio = g.DataHoraInicio

}).AsNoTracking().ToListAsync();
return await query;
Expand Down Expand Up @@ -338,7 +340,7 @@ public async Task<HttpStatusCode> RegistrarFrequenciaAsync(List<EnsaioListaFrequ
int pos = 0;
await dbFrequencias.ForEachAsync(dbFrequencia =>
{
if(dbFrequencia.IdEnsaio == frequencias[0].IdEnsaio && dbFrequencia.IdPessoa == frequencias[pos].IdPessoa)
if (dbFrequencia.IdEnsaio == frequencias[0].IdEnsaio && dbFrequencia.IdPessoa == frequencias[pos].IdPessoa)
{
dbFrequencia.JustificativaAceita = Convert.ToSByte(frequencias[pos].JustificativaAceita);
dbFrequencia.Presente = Convert.ToSByte(frequencias[pos].Presente);
Expand All @@ -350,15 +352,15 @@ await dbFrequencias.ForEachAsync(dbFrequencia =>

await _context.SaveChangesAsync();

return HttpStatusCode.OK ;
return HttpStatusCode.OK;
}
catch
{
return HttpStatusCode.InternalServerError;
}
}

public async Task<IEnumerable<EnsaioAssociadoDTO>> GetEnsaiosByIdPesoaAsync(int idPessoa)
public async Task<IEnumerable<EnsaioAssociadoDTO>> GetEnsaiosByIdPesoaAsync(int idPessoa)
{
var query = from ensaioPessoa in _context.Ensaiopessoas
where ensaioPessoa.IdPessoa == idPessoa
Expand Down Expand Up @@ -416,5 +418,44 @@ public async Task<IEnumerable<int>> GetIdRegentesEnsaioAsync(int idEnsaio)
.Where(ep => ep.IdEnsaio == idEnsaio && ep.IdPapelGrupoPapelGrupo == 5)
.Select(ep => ep.IdPessoa).ToListAsync();
}

public async Task<DatatableResponse<EnsaioIndexDTO>> GetDataPage(DatatableRequest request, int idGrupo)
{
var ensaios = await GetAllIndexDTO(idGrupo);
var totalRecords = ensaios.Count();

// filtra pelo campos de busca
if (request.Search != null && request.Search.GetValueOrDefault("value") != null)
{
ensaios = ensaios.Where(ensaios => ensaios.Id.ToString().Contains(request.Search.GetValueOrDefault("value"))
|| ensaios.Local.ToLower().Contains(request.Search.GetValueOrDefault("value")));
}

if (request.Order != null && request.Order[0].GetValueOrDefault("column").Equals("0"))
{
if (request.Order[0].GetValueOrDefault("dir").Equals("asc"))
ensaios = ensaios.OrderBy(ensaios => ensaios.DataHoraInicio);
else
ensaios = ensaios.OrderByDescending(ensaios => ensaios.DataHoraInicio);
}
else if (request.Order != null && request.Order[0].GetValueOrDefault("column").Equals("2"))
{
if (request.Order[0].GetValueOrDefault("dir").Equals("asc"))
ensaios = ensaios.OrderBy(ensaios => ensaios.Local);
else
ensaios = ensaios.OrderByDescending(ensaios => ensaios.Local);
}
int countRecordsFiltered = ensaios.Count();

ensaios = ensaios.Skip(request.Start).Take(request.Length);
return new DatatableResponse<EnsaioIndexDTO>
{
Data = ensaios.ToList(),
Draw = request.Draw,
RecordsFiltered = countRecordsFiltered,
RecordsTotal = totalRecords
};
}
}

}

0 comments on commit abf7208

Please sign in to comment.