bar_branche #21

Merged
alexis.drai merged 2 commits from bar_branche into master 2 years ago

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using cat_cafe.Entities;
using cat_cafe.Repositories;
namespace cat_cafe.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BarsController : ControllerBase
{
private readonly BarContext _context;
public BarsController(BarContext context)
{
_context = context;
}
// GET: api/Bars
[HttpGet]
public async Task<ActionResult<IEnumerable<Bar>>> GetBars()
{
return await _context.Bars.ToListAsync();
}
// GET: api/Bars/5
[HttpGet("{id}")]
public async Task<ActionResult<Bar>> GetBar(long id)
{
var bar = await _context.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
return bar;
}
// PUT: api/Bars/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutBar(long id, Bar bar)
{
if (id != bar.Id)
{
return BadRequest();
}
_context.Entry(bar).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BarExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Bars
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Bar>> PostBar(Bar bar)
{
_context.Bars.Add(bar);
await _context.SaveChangesAsync();
return CreatedAtAction("GetBar", new { id = bar.Id }, bar);
}
// DELETE: api/Bars/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBar(long id)
{
var bar = await _context.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
_context.Bars.Remove(bar);
await _context.SaveChangesAsync();
return NoContent();
}
private bool BarExists(long id)
{
return _context.Bars.Any(e => e.Id == id);
}
}
}

@ -0,0 +1,24 @@
using System;
namespace cat_cafe.Entities
{
public class Bar
{
public long Id { get; set; }
public string? Name { get; set; }
public List<Cat> cats { get; set; } = new List<Cat>();
public void addCat(Cat c)
{
cats.Add(c);
}
public void removeCat(Cat c)
{
cats.Remove(c);
}
}
}

@ -8,6 +8,8 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddDbContext<CatContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddDbContext<BarContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

@ -1,4 +1,4 @@
{
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
@ -11,7 +11,6 @@
"profiles": {
"cat_cafe": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7229;http://localhost:5229",
@ -28,4 +27,4 @@
}
}
}
}
}

@ -0,0 +1,17 @@
using cat_cafe.Entities;
using Microsoft.EntityFrameworkCore;
using System;
namespace cat_cafe.Repositories
{
public class BarContext : DbContext
{
public BarContext(DbContextOptions<BarContext> options)
: base(options)
{
}
public DbSet<Bar> Bars { get; set; } = null!;
}
}
Loading…
Cancel
Save