diff --git a/cat_cafe/Controllers/BarsController.cs b/cat_cafe/Controllers/BarsController.cs new file mode 100644 index 0000000..26b907d --- /dev/null +++ b/cat_cafe/Controllers/BarsController.cs @@ -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>> GetBars() + { + return await _context.Bars.ToListAsync(); + } + + // GET: api/Bars/5 + [HttpGet("{id}")] + public async Task> 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 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> 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 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); + } + } +} diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index e1c623e..b315b51 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -8,6 +8,8 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("CatCafe")); +builder.Services.AddDbContext(opt => +opt.UseInMemoryDatabase("CatCafe")); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); diff --git a/cat_cafe/Repositories/BarContext.cs b/cat_cafe/Repositories/BarContext.cs index 951737d..8b085c9 100644 --- a/cat_cafe/Repositories/BarContext.cs +++ b/cat_cafe/Repositories/BarContext.cs @@ -1,11 +1,17 @@ -using System; +using cat_cafe.Entities; +using Microsoft.EntityFrameworkCore; +using System; namespace cat_cafe.Repositories { - public class BarContext + public class BarContext : DbContext { - public BarContext() - { - } - } + public BarContext(DbContextOptions options) + : base(options) + { + } + + public DbSet Bars { get; set; } = null!; + + } }