Nueva mejoras Y estabilidad

This commit is contained in:
2025-12-26 22:27:20 -06:00
parent 203859b22a
commit ac96cb1f23
23 changed files with 1841 additions and 480 deletions

View File

@@ -1,29 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using MieSystem.Data.Interfaces;
using MieSystem.Models;
using MieSystem.Models.ViewModels;
using MieSystem.Services;
namespace MieSystem.Controllers
{
public class ExpedientesController : Controller
{
private static List<Expediente> _expedientes = new List<Expediente>();
private static int _idCounter = 1;
//private static int _idCounter = 1;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IExpedienteRepository _expedienteRepository;
public ExpedientesController(IExpedienteRepository expedienteRepository, IWebHostEnvironment hostingEnvironment)
private readonly ExpedienteService expedienteService;
public ExpedientesController(ExpedienteService expedienteService, IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
_expedienteRepository = expedienteRepository;
this.expedienteService = expedienteService;
}
// GET: Expedientes
public async Task<IActionResult> Index()
{
var today = DateTime.Today;
var lst = await _expedienteRepository.GetAllAsync();
var lst = await expedienteService.GetAllAsync();
_expedientes = [.. lst];
return View();
}
@@ -83,7 +81,7 @@ namespace MieSystem.Controllers
FechaNacimiento = e.FechaNacimiento,
Sexo = e.Sexo,
NombreResponsable = e.NombreResponsable,
FotoUrl = e.FotoUrl
FotoUrl = ExisteFoto(e.FotoUrl)
})
.ToList();
@@ -140,7 +138,6 @@ namespace MieSystem.Controllers
// Crear nuevo expediente
var expediente = new Expediente
{
Id = _idCounter++,
Nombre = model.Nombre,
Apellidos = model.Apellidos,
FechaNacimiento = model.FechaNacimiento,
@@ -160,7 +157,8 @@ namespace MieSystem.Controllers
try
{
await _expedienteRepository.CreateAsync(expediente);
//await _expedienteRepository.CreateAsync(expediente);
await expedienteService.SaveAsync(expediente);
return Json(new { success = true, message = "Expediente creado exitosamente" });
}
catch (Exception ex)
@@ -217,7 +215,8 @@ namespace MieSystem.Controllers
ModelState.Remove("Observaciones");
if (ModelState.IsValid)
{
var expediente = await _expedienteRepository.GetByIdAsync(id);
//var expediente = await _expedienteRepository.GetByIdAsync(id);
var expediente = await expedienteService.GetByIdAsync(id);
//var expediente = _expedientes.FirstOrDefault(e => e.Id == id);
if (expediente == null)
{
@@ -278,7 +277,8 @@ namespace MieSystem.Controllers
try
{
await _expedienteRepository.UpdateAsync(expediente);
//await _expedienteRepository.UpdateAsync(expediente);
await expedienteService.SaveAsync(expediente);
return Json(new { success = true, message = "Expediente actualizado exitosamente" });
}
@@ -292,7 +292,7 @@ namespace MieSystem.Controllers
}
// DELETE: Expedientes/Delete/5
[HttpDelete]
/*[HttpDelete]
public async Task<IActionResult> Delete(int id)
{
var expediente = _expedientes.FirstOrDefault(e => e.Id == id);
@@ -318,7 +318,7 @@ namespace MieSystem.Controllers
return Json(new { success = true, message = "Expediente eliminado exitosamente" });
}
*/
// GET: Expedientes/Details/5 (Opcional)
public IActionResult Details(int id)
{
@@ -410,6 +410,23 @@ namespace MieSystem.Controllers
}
}
public string ExisteFoto(string url)
{
if(string.IsNullOrEmpty(url))
return Path.Combine("images", "default-avatar.png");
var uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", "fotos");
string[] parts = url.Split('/');
string name = parts[^1];
string fullpath = Path.Combine(uploadsFolder, name);
if (System.IO.File.Exists(fullpath))
return url;
return Path.Combine("images", "default-avatar.png");
}
// GET: Expedientes/DeleteImage
[HttpDelete]
public IActionResult DeleteImage(string imageUrl)