Skip to content

Commit 226d099

Browse files
committed
Controlador para Libro completa con metodos get, post, put y delete, API completo y funcional, probado con postman
1 parent d8cec9b commit 226d099

File tree

75 files changed

+5451
-462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+5451
-462
lines changed

.vs/APIBiblioteca/v16/.suo

3.5 KB
Binary file not shown.

APIBiblioteca/APIBiblioteca.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0" />
89
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
910
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0">
1011
<PrivateAssets>all</PrivateAssets>

APIBiblioteca/Controllers/AutorController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public AutorController(ApplicationDBContext context)
2323
[HttpGet]
2424
public ActionResult<IEnumerable<Autor>> Get()
2525
{
26-
return context.Autor.ToList();
26+
return context.Autor.Include(x=>x.Libros).ToList();
2727
}
2828

2929
[HttpGet("{id}",Name ="ObtenerAutor")]
3030
public ActionResult<Autor>Get(int id)
3131
{
32-
var autor = context.Autor.FirstOrDefault(x => x.Id == id);
32+
var autor = context.Autor.Include(x=>x.Libros).FirstOrDefault(x => x.Id == id);
3333
if(autor == null)
3434
{
3535
return NotFound();
@@ -68,7 +68,7 @@ public ActionResult<Autor> Delete(int id)
6868
{
6969
return NotFound();
7070
}
71-
71+
//tambien puede usarse context.Autor.Remove(autor);
7272
context.Entry(autor).State = EntityState.Deleted;
7373
context.SaveChanges();
7474
return autor;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using APIBiblioteca.Context;
2+
using APIBiblioteca.Entities;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.EntityFrameworkCore;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace APIBiblioteca.Controllers
11+
{
12+
[ApiController]
13+
[Route("api/{controller}")]
14+
public class LibroController : ControllerBase
15+
{
16+
private readonly ApplicationDBContext context;
17+
18+
public LibroController(ApplicationDBContext context)
19+
{
20+
this.context = context;
21+
}
22+
23+
24+
[HttpGet]
25+
public ActionResult<List<Libro>> Get()
26+
{
27+
var libros = context.Libro.Include(x => x.Autor).ToList();
28+
return libros;
29+
}
30+
31+
[HttpGet("{id}",Name = "ObtenerLibro")]
32+
public ActionResult<Libro> Get(int id)
33+
{
34+
var libro = context.Libro.Include(x => x.Autor).FirstOrDefault(x => x.Id == id);
35+
36+
if (libro == null)
37+
{
38+
return NotFound();
39+
}
40+
return libro;
41+
}
42+
43+
[HttpPost]
44+
public ActionResult Post([FromBody] Libro libro)
45+
{
46+
context.Libro.Add(libro);
47+
context.SaveChanges();
48+
return new CreatedAtRouteResult("ObtenerLibro", new { id = libro.Id }, libro);
49+
}
50+
51+
[HttpPut("{id}")]
52+
public ActionResult Put([FromBody]Libro libro, int id)
53+
{
54+
if (id != libro.Id)
55+
{
56+
return BadRequest();
57+
}
58+
context.Entry(libro).State = EntityState.Modified;
59+
context.SaveChanges();
60+
return Ok();
61+
}
62+
63+
[HttpDelete("{id}")]
64+
public ActionResult<Libro> Delete(int id)
65+
{
66+
var libro = context.Libro.FirstOrDefault(x => x.Id == id);
67+
68+
if (libro == null)
69+
{
70+
return NotFound();
71+
}
72+
context.Libro.Remove(libro);
73+
context.SaveChanges();
74+
return libro;
75+
}
76+
77+
}
78+
}

APIBiblioteca/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public Startup(IConfiguration configuration)
2828
public void ConfigureServices(IServiceCollection services)
2929
{
3030
services.AddDbContext<ApplicationDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));
31-
services.AddControllers();
31+
32+
services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
3233
}
3334

3435
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
 (0)