Modulos Pacientes Listo
This commit is contained in:
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using vita_asistente.Data;
|
||||
using vita_asistente.Data.Models;
|
||||
using vita_asistente.Models;
|
||||
|
||||
namespace vita_asistente.Controllers
|
||||
{
|
||||
@@ -26,12 +26,12 @@ namespace vita_asistente.Controllers
|
||||
if (!string.IsNullOrWhiteSpace(nombreBusqueda))
|
||||
{
|
||||
pacientes = pacientes.Where(p =>
|
||||
(p.Nombre + " " + p.Apellido).Contains(nombreBusqueda.Trim()) ||
|
||||
p.Documento.Contains(nombreBusqueda.Trim()));
|
||||
(p.FirstName + " " + p.LastName).Contains(nombreBusqueda.Trim()) ||
|
||||
p.DocumentNumber.Contains(nombreBusqueda.Trim()));
|
||||
}
|
||||
|
||||
ViewBag.NombreBusqueda = nombreBusqueda;
|
||||
return View(await pacientes.OrderByDescending(p => p.FechaRegistro).ToListAsync());
|
||||
return View(await pacientes.OrderByDescending(p => p.CreatedAt).ToListAsync());
|
||||
}
|
||||
|
||||
// GET: Pacientes/Details/5
|
||||
@@ -43,6 +43,186 @@ namespace vita_asistente.Controllers
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// GET: Pacientes/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Pacientes/Create
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(Pacientes paciente)
|
||||
{
|
||||
if (string.IsNullOrEmpty(paciente.Id))
|
||||
{
|
||||
paciente.Id = Guid.NewGuid().ToString("N");
|
||||
if (ModelState.ContainsKey("Id"))
|
||||
ModelState.Remove("Id");
|
||||
}
|
||||
paciente.AllergiesDescription ??= string.Empty;
|
||||
if (ModelState.ContainsKey("AllergiesDescription") &&
|
||||
ModelState["AllergiesDescription"].Errors.Any(e => e.ErrorMessage.Contains("required")))
|
||||
{
|
||||
ModelState.Remove("AllergiesDescription");
|
||||
}
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.Add(paciente);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if (Request.Headers["Accept"].Contains("application/json"))
|
||||
{
|
||||
return Json(new
|
||||
{
|
||||
success = true,
|
||||
data = new
|
||||
{
|
||||
id = paciente.Id,
|
||||
documentType = paciente.DocumentType,
|
||||
documentNumber = paciente.DocumentNumber,
|
||||
firstName = paciente.FirstName,
|
||||
lastName = paciente.LastName,
|
||||
email = paciente.Email,
|
||||
birthDate = paciente.BirthDate?.ToString("yyyy-MM-dd"),
|
||||
address = paciente.Address,
|
||||
phone = paciente.Phone,
|
||||
emergencyContactName = paciente.EmergencyContactName,
|
||||
emergencyContactPhone = paciente.EmergencyContactPhone,
|
||||
bloodType = paciente.BloodType,
|
||||
allergiesDescription = paciente.AllergiesDescription,
|
||||
hasMedicationAllergies = paciente.HasMedicationAllergies,
|
||||
isActive = paciente.IsActive,
|
||||
createdAt = paciente.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
if (Request.Headers["Accept"].Contains("application/json"))
|
||||
{
|
||||
var errorDict = new Dictionary<string, string[]>();
|
||||
foreach (var key in ModelState.Keys)
|
||||
{
|
||||
var errors = ModelState[key]?.Errors;
|
||||
if (errors != null && errors.Any())
|
||||
{
|
||||
errorDict.Add(key, errors.Select(e => e.ErrorMessage).ToArray());
|
||||
}
|
||||
}
|
||||
return BadRequest(new { success = false, errors = errorDict });
|
||||
}
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// GET: Pacientes/Edit/5
|
||||
public async Task<IActionResult> Edit(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente == null) return NotFound();
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// POST: Pacientes/Edit/5
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(string id, Pacientes paciente)
|
||||
{
|
||||
if (id != paciente.Id) return NotFound();
|
||||
paciente.Id = id;
|
||||
paciente.AllergiesDescription ??= string.Empty;
|
||||
if (ModelState.ContainsKey("AllergiesDescription") &&
|
||||
ModelState["AllergiesDescription"].Errors.Any(e => e.ErrorMessage.Contains("required")))
|
||||
{
|
||||
ModelState.Remove("AllergiesDescription");
|
||||
}
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(paciente);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if (Request.Headers["Accept"].Contains("application/json"))
|
||||
{
|
||||
return Json(new
|
||||
{
|
||||
success = true,
|
||||
data = new
|
||||
{
|
||||
id = paciente.Id,
|
||||
documentType = paciente.DocumentType,
|
||||
documentNumber = paciente.DocumentNumber,
|
||||
firstName = paciente.FirstName,
|
||||
lastName = paciente.LastName,
|
||||
email = paciente.Email,
|
||||
birthDate = paciente.BirthDate?.ToString("yyyy-MM-dd"),
|
||||
address = paciente.Address,
|
||||
phone = paciente.Phone,
|
||||
emergencyContactName = paciente.EmergencyContactName,
|
||||
emergencyContactPhone = paciente.EmergencyContactPhone,
|
||||
bloodType = paciente.BloodType,
|
||||
allergiesDescription = paciente.AllergiesDescription,
|
||||
hasMedicationAllergies = paciente.HasMedicationAllergies,
|
||||
isActive = paciente.IsActive,
|
||||
createdAt = paciente.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")
|
||||
}
|
||||
});
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!PacienteExists(paciente.Id)) return NotFound();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
if (Request.Headers["Accept"].Contains("application/json"))
|
||||
{
|
||||
var errorDict = new Dictionary<string, string[]>();
|
||||
foreach (var key in ModelState.Keys)
|
||||
{
|
||||
var errors = ModelState[key]?.Errors;
|
||||
if (errors != null && errors.Any())
|
||||
{
|
||||
errorDict.Add(key, errors.Select(e => e.ErrorMessage).ToArray());
|
||||
}
|
||||
}
|
||||
return BadRequest(new { success = false, errors = errorDict });
|
||||
}
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// GET: Pacientes/Delete/5
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
if (id == null) return NotFound();
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente == null) return NotFound();
|
||||
return View(paciente);
|
||||
}
|
||||
|
||||
// POST: Pacientes/Delete/5
|
||||
[HttpPost, ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(string id)
|
||||
{
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente != null)
|
||||
{
|
||||
_context.Pacientes.Remove(paciente);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
if (Request.Headers["Accept"].Contains("application/json"))
|
||||
{
|
||||
return Json(new { success = true });
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// POST: Pacientes/Activate/5
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
@@ -58,5 +238,26 @@ namespace vita_asistente.Controllers
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// POST: Pacientes/Disable/5
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Disable(string id)
|
||||
{
|
||||
var paciente = await _context.Pacientes.FindAsync(id);
|
||||
if (paciente == null) return NotFound();
|
||||
|
||||
paciente.IsActive = false;
|
||||
_context.Pacientes.Update(paciente);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
private bool PacienteExists(string id)
|
||||
{
|
||||
return _context.Pacientes.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user