From e873c3f903510d29410fd74840ba179f6284bbe9 Mon Sep 17 00:00:00 2001 From: MARIFER HERNANDEZ GONZALEZ Date: Tue, 28 Oct 2025 19:40:21 -0600 Subject: [PATCH] MODELS AND CONTROLLERS --- BibliotecaDigitalApi/Program.cs | 39 +++------ .../BibliotecaDigitalApi.AssemblyInfo.cs | 4 +- ...liotecaDigitalApi.AssemblyInfoInputs.cache | 2 +- ...lApi.MvcApplicationPartsAssemblyInfo.cache | 0 ...italApi.MvcApplicationPartsAssemblyInfo.cs | 17 ++++ ...aDigitalApi.csproj.CoreCompileInputs.cache | 1 + ...tecaDigitalApi.csproj.FileListAbsolute.txt | 7 ++ Models/BibliotecaContext.cs | 14 ++++ Models/Libro.cs | 16 ++++ Models/Prestamo.cs | 24 ++++++ Models/Usuario.cs | 12 +++ controllers/LibrosController.cs | 60 ++++++++++++++ controllers/PrestamosController.cs | 82 +++++++++++++++++++ controllers/UsuariosController.cs | 33 ++++++++ 14 files changed, 279 insertions(+), 32 deletions(-) create mode 100644 BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cache create mode 100644 BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cs create mode 100644 BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.CoreCompileInputs.cache create mode 100644 BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.FileListAbsolute.txt create mode 100644 Models/BibliotecaContext.cs create mode 100644 Models/Libro.cs create mode 100644 Models/Prestamo.cs create mode 100644 Models/Usuario.cs create mode 100644 controllers/LibrosController.cs create mode 100644 controllers/PrestamosController.cs create mode 100644 controllers/UsuariosController.cs diff --git a/BibliotecaDigitalApi/Program.cs b/BibliotecaDigitalApi/Program.cs index 00ff539..172964c 100644 --- a/BibliotecaDigitalApi/Program.cs +++ b/BibliotecaDigitalApi/Program.cs @@ -1,13 +1,17 @@ +using BibliotecaAPI.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + + +builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); -// Configure the HTTP request pipeline. +// Activar Swagger para probar endpoints if (app.Environment.IsDevelopment()) { app.UseSwagger(); @@ -15,30 +19,7 @@ if (app.Environment.IsDevelopment()) } app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast") -.WithOpenApi(); - -app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} +app.Run(); \ No newline at end of file diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfo.cs b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfo.cs index 14074d1..2448c22 100644 --- a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfo.cs +++ b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfo.cs @@ -13,10 +13,10 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("BibliotecaDigitalApi")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7b037ee7692fed1179b172f2163d0f866cb8e57d")] [assembly: System.Reflection.AssemblyProductAttribute("BibliotecaDigitalApi")] [assembly: System.Reflection.AssemblyTitleAttribute("BibliotecaDigitalApi")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] -// Generated by the MSBuild WriteCodeFragment class. +// Generado por la clase WriteCodeFragment de MSBuild. diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfoInputs.cache b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfoInputs.cache index 21b5fed..a8c9c26 100644 --- a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfoInputs.cache +++ b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.AssemblyInfoInputs.cache @@ -1 +1 @@ -6ed0a80a11b43174ad50914035db8ef21ed9a2256f21b1d4e57a6b5c8f6a8e18 +fe9b27aa300c3789229c7b6b1cb8aa68e1db82d30fd78e439c90942c46816c80 diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cache b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cache new file mode 100644 index 0000000..e69de29 diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cs b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cs new file mode 100644 index 0000000..9fe4d8c --- /dev/null +++ b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cs @@ -0,0 +1,17 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +using System; +using System.Reflection; + +[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartAttribute("Microsoft.AspNetCore.OpenApi")] +[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartAttribute("Swashbuckle.AspNetCore.SwaggerGen")] + +// Generado por la clase WriteCodeFragment de MSBuild. + diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.CoreCompileInputs.cache b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..26ec213 --- /dev/null +++ b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +90de409f17a71d4aa851b14ec70bdf8c6d47c7ea53281b0480308c94120b5983 diff --git a/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.FileListAbsolute.txt b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..b03a08e --- /dev/null +++ b/BibliotecaDigitalApi/obj/Debug/net8.0/BibliotecaDigitalApi.csproj.FileListAbsolute.txt @@ -0,0 +1,7 @@ +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.csproj.AssemblyReference.cache +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.GeneratedMSBuildEditorConfig.editorconfig +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.AssemblyInfoInputs.cache +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.AssemblyInfo.cs +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.csproj.CoreCompileInputs.cache +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cs +C:\Users\CHIOH\source\repos\BibliotecaDigital\BibliotecaDigitalApi\obj\Debug\net8.0\BibliotecaDigitalApi.MvcApplicationPartsAssemblyInfo.cache diff --git a/Models/BibliotecaContext.cs b/Models/BibliotecaContext.cs new file mode 100644 index 0000000..67adabf --- /dev/null +++ b/Models/BibliotecaContext.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; +using BibliotecaAPI.Models; + +namespace BibliotecaAPI.Data +{ + public class BibliotecaContext : DbContext + { + public BibliotecaContext(DbContextOptions options) : base(options) { } + + public DbSet Libros { get; set; } + public DbSet Usuarios { get; set; } + public DbSet Prestamos { get; set; } + } +} diff --git a/Models/Libro.cs b/Models/Libro.cs new file mode 100644 index 0000000..e78f443 --- /dev/null +++ b/Models/Libro.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace BibliotecaAPI.Models +{ + public class Libro + { + [Key] + public int Id { get; set; } + public string Titulo { get; set; } = string.Empty; + public string Autor { get; set; } = string.Empty; + public string ISBN { get; set; } = string.Empty; + public int Año { get; set; } + public string Categoria { get; set; } = string.Empty; + public int CopiasDisponibles { get; set; } + } +} diff --git a/Models/Prestamo.cs b/Models/Prestamo.cs new file mode 100644 index 0000000..37a93cb --- /dev/null +++ b/Models/Prestamo.cs @@ -0,0 +1,24 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BibliotecaAPI.Models +{ + public class Prestamo + { + [Key] + public int Id { get; set; } + + [ForeignKey("Usuario")] + public int UsuarioId { get; set; } + public Usuario? Usuario { get; set; } + + [ForeignKey("Libro")] + public int LibroId { get; set; } + public Libro? Libro { get; set; } + + public DateTime FechaPrestamo { get; set; } = DateTime.Now; + public DateTime? FechaDevolucion { get; set; } + public bool Devuelto { get; set; } = false; + } +} diff --git a/Models/Usuario.cs b/Models/Usuario.cs new file mode 100644 index 0000000..0b57a67 --- /dev/null +++ b/Models/Usuario.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace BibliotecaAPI.Models +{ + public class Usuario + { + [Key] + public int Id { get; set; } + public string Nombre { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + } +} diff --git a/controllers/LibrosController.cs b/controllers/LibrosController.cs new file mode 100644 index 0000000..6778f0d --- /dev/null +++ b/controllers/LibrosController.cs @@ -0,0 +1,60 @@ +using Microsoft.AspNetCore.Mvc; +using BibliotecaAPI.Data; +using BibliotecaAPI.Models; +using Microsoft.EntityFrameworkCore; + +namespace BibliotecaAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class LibrosController : ControllerBase + { + private readonly BibliotecaContext _context; + + public LibrosController(BibliotecaContext context) + { + _context = context; + } + + [HttpGet] + public async Task>> GetLibros() + { + return await _context.Libros.ToListAsync(); + } + + [HttpGet("{id}")] + public async Task> GetLibro(int id) + { + var libro = await _context.Libros.FindAsync(id); + if (libro == null) return NotFound(); + return libro; + } + + [HttpPost] + public async Task> PostLibro(Libro libro) + { + _context.Libros.Add(libro); + await _context.SaveChangesAsync(); + return CreatedAtAction(nameof(GetLibro), new { id = libro.Id }, libro); + } + + [HttpPut("{id}")] + public async Task PutLibro(int id, Libro libro) + { + if (id != libro.Id) return BadRequest(); + _context.Entry(libro).State = EntityState.Modified; + await _context.SaveChangesAsync(); + return NoContent(); + } + + [HttpDelete("{id}")] + public async Task DeleteLibro(int id) + { + var libro = await _context.Libros.FindAsync(id); + if (libro == null) return NotFound(); + _context.Libros.Remove(libro); + await _context.SaveChangesAsync(); + return NoContent(); + } + } +} diff --git a/controllers/PrestamosController.cs b/controllers/PrestamosController.cs new file mode 100644 index 0000000..44d1a30 --- /dev/null +++ b/controllers/PrestamosController.cs @@ -0,0 +1,82 @@ +using Microsoft.AspNetCore.Mvc; +using BibliotecaAPI.Data; +using BibliotecaAPI.Models; +using Microsoft.EntityFrameworkCore; + +namespace BibliotecaAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class PrestamosController : ControllerBase + { + private readonly BibliotecaContext _context; + + public PrestamosController(BibliotecaContext context) + { + _context = context; + } + + // Crear préstamo + [HttpPost("crear")] + public async Task> CrearPrestamo(Prestamo prestamo) + { + var libro = await _context.Libros.FindAsync(prestamo.LibroId); + if (libro == null || libro.CopiasDisponibles <= 0) + return BadRequest("Libro no disponible."); + + libro.CopiasDisponibles--; + _context.Prestamos.Add(prestamo); + await _context.SaveChangesAsync(); + + return Ok(prestamo); + } + + // Registrar devolución + [HttpPut("devolver/{id}")] + public async Task RegistrarDevolucion(int id) + { + var prestamo = await _context.Prestamos.Include(p => p.Libro).FirstOrDefaultAsync(p => p.Id == id); + if (prestamo == null) return NotFound(); + + prestamo.Devuelto = true; + prestamo.FechaDevolucion = DateTime.Now; + prestamo.Libro.CopiasDisponibles++; + await _context.SaveChangesAsync(); + + return Ok("Devolución registrada con éxito."); + } + + // Consultar préstamos activos + [HttpGet("activos")] + public async Task>> GetActivos() + { + return await _context.Prestamos + .Include(p => p.Libro) + .Include(p => p.Usuario) + .Where(p => !p.Devuelto) + .ToListAsync(); + } + + // Consultar préstamos vencidos (más de 15 días) + [HttpGet("vencidos")] + public async Task>> GetVencidos() + { + var hoy = DateTime.Now; + return await _context.Prestamos + .Include(p => p.Libro) + .Include(p => p.Usuario) + .Where(p => !p.Devuelto && (hoy - p.FechaPrestamo).TotalDays > 15) + .ToListAsync(); + } + + // Historial por usuario + [HttpGet("historial/{usuarioId}")] + public async Task>> GetHistorialUsuario(int usuarioId) + { + return await _context.Prestamos + .Include(p => p.Libro) + .Where(p => p.UsuarioId == usuarioId) + .ToListAsync(); + } + } +} diff --git a/controllers/UsuariosController.cs b/controllers/UsuariosController.cs new file mode 100644 index 0000000..8a44d32 --- /dev/null +++ b/controllers/UsuariosController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using BibliotecaAPI.Data; +using BibliotecaAPI.Models; +using Microsoft.EntityFrameworkCore; + +namespace BibliotecaAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class UsuariosController : ControllerBase + { + private readonly BibliotecaContext _context; + + public UsuariosController(BibliotecaContext context) + { + _context = context; + } + + [HttpGet] + public async Task>> GetUsuarios() + { + return await _context.Usuarios.ToListAsync(); + } + + [HttpPost] + public async Task> PostUsuario(Usuario usuario) + { + _context.Usuarios.Add(usuario); + await _context.SaveChangesAsync(); + return CreatedAtAction(nameof(GetUsuarios), new { id = usuario.Id }, usuario); + } + } +}