263 lines
9.9 KiB
C#
263 lines
9.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using vita_asistente.Data;
|
|
using vita_asistente.Models;
|
|
|
|
namespace vita_asistente.Controllers
|
|
{
|
|
[Authorize]
|
|
public class PacientesController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public PacientesController(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: Pacientes
|
|
public async Task<IActionResult> Index(string nombreBusqueda)
|
|
{
|
|
var pacientes = _context.Pacientes.AsNoTracking();
|
|
|
|
if (!string.IsNullOrWhiteSpace(nombreBusqueda))
|
|
{
|
|
pacientes = pacientes.Where(p =>
|
|
(p.FirstName + " " + p.LastName).Contains(nombreBusqueda.Trim()) ||
|
|
p.DocumentNumber.Contains(nombreBusqueda.Trim()));
|
|
}
|
|
|
|
ViewBag.NombreBusqueda = nombreBusqueda;
|
|
return View(await pacientes.OrderByDescending(p => p.CreatedAt).ToListAsync());
|
|
}
|
|
|
|
// GET: Pacientes/Details/5
|
|
public async Task<IActionResult> Details(string id)
|
|
{
|
|
if (id == null) return NotFound();
|
|
var paciente = await _context.Pacientes.FindAsync(id);
|
|
if (paciente == null) return NotFound();
|
|
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")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Activate(string id)
|
|
{
|
|
var paciente = await _context.Pacientes.FindAsync(id);
|
|
if (paciente == null) return NotFound();
|
|
|
|
paciente.IsActive = true;
|
|
_context.Pacientes.Update(paciente);
|
|
await _context.SaveChangesAsync();
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |