Merge pull request '🐛 🔥 ♻️ 💀 ⚰️ Fix #7 ; Fix #36 - Implement One-to-Many and set-up SQLite' (#62) from debug-one-to-many into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #62
pull/63/head
Alexis Drai 2 years ago
commit 48c0e0f03b

@ -3,12 +3,12 @@ using cat_cafe.Dto;
using cat_cafe.Entities; using cat_cafe.Entities;
using cat_cafe.Mappers; using cat_cafe.Mappers;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using cat_cafe.WeSo;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using FluentAssertions;
using cat_cafe.WeSo;
namespace cat_cafe.Controllers.Tests namespace cat_cafe.Controllers.Tests
{ {
@ -101,7 +101,7 @@ namespace cat_cafe.Controllers.Tests
actualResult!.Value.Should().BeEquivalentTo(aliceDto); actualResult!.Value.Should().BeEquivalentTo(aliceDto);
} }
/*
[TestMethod()] [TestMethod()]
public async Task PutCatTest() public async Task PutCatTest()
{ {
@ -111,12 +111,12 @@ namespace cat_cafe.Controllers.Tests
// Act // Act
var responseType = await controller.PutCat(bob.Id, robert); var responseType = await controller.PutCat(bob.Id, robert);
// System.InvalidOperationException: // System.InvalidOperationException:
// The instance of entity type 'Cat' cannot be tracked because another instance // The instance of entity type 'Cat' cannot be tracked because another instance
// with the same key value for {'Id'} is already being tracked. // with the same key value for {'Id'} is already being tracked.
// ... this simple update should work out of the box, // ... this simple update should work out of the box,
// DbContext is already 'scoped' by default instead of singleton // DbContext is already 'scoped' by default instead of singleton
// Assert // Assert
responseType.Should().BeOfType<NoContentResult>(); responseType.Should().BeOfType<NoContentResult>();
@ -125,7 +125,7 @@ namespace cat_cafe.Controllers.Tests
var actualResult = actual.Result as OkObjectResult; var actualResult = actual.Result as OkObjectResult;
actualResult!.Value.Should().BeEquivalentTo(robert); actualResult!.Value.Should().BeEquivalentTo(robert);
} }
*/
[TestMethod()] [TestMethod()]
public async Task PostCatTest() public async Task PostCatTest()

@ -1,29 +1,22 @@
using System; using AutoMapper;
using System.Collections.Generic; using cat_cafe.Dto;
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.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using AutoMapper; using Microsoft.AspNetCore.Mvc;
using cat_cafe.Dto; using Microsoft.EntityFrameworkCore;
using System.Collections;
using System.Xml.Linq;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
[Route("api/v{version:apiVersion}/[controller]")] [Route("api/v{version:apiVersion}/[controller]")]
[ApiController] [ApiController]
[ApiVersion("2.0")] [ApiVersion("1.0")]
public class BarsController : ControllerBase public class BarsController : ControllerBase
{ {
private readonly CatCafeContext _context; private readonly CatCafeContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger <BarsController> _logger; private readonly ILogger<BarsController> _logger;
public BarsController(CatCafeContext context,IMapper mapper, ILogger<BarsController> logger) public BarsController(CatCafeContext context, IMapper mapper, ILogger<BarsController> logger)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;
@ -32,97 +25,83 @@ namespace cat_cafe.Controllers
// GET: api/v1/Bars // GET: api/v1/Bars
[HttpGet] [HttpGet]
[MapToApiVersion("1.0")]
public async Task<ActionResult<IEnumerable<BarDto>>> GetBars() public async Task<ActionResult<IEnumerable<BarDto>>> GetBars()
{ {
var bars = _context.Bars var bars = await _context.Bars
.Include(a => a.cats) .Include(b => b.Cats)
.Select(a => new Bar .ToListAsync();
{
Id = a.Id, return Ok(_mapper.Map<IEnumerable<BarDto>>(bars));
Name = a.Name,
cats = a.cats.Select(p => new Cat { Name = p.Name, Age = p.Age, Id= p.Id}).ToList()
})
.ToList();
return _mapper.Map<List<BarDto>>(bars);
} }
// GET: api/v1/Bars/5 // GET: api/v1/Bars/5
[HttpGet("{id}")] [HttpGet("{id}")]
[MapToApiVersion("1.0")]
public async Task<ActionResult<BarDto>> GetBar(long id) public async Task<ActionResult<BarDto>> GetBar(long id)
{ {
var bar = _context.Bars.Include(p => p.cats) var bar = await _context.Bars
.Select(a => new Bar .Include(b => b.Cats)
{ .SingleOrDefaultAsync(b => b.Id == id);
Id = a.Id,
Name = a.Name,
cats = a.cats.Select(p => new Cat { Name = p.Name, Age = p.Age, Id = p.Id }).ToList()
}).FirstOrDefaultAsync(p => p.Id == id);
if (bar == null) if (bar == null)
{ {
return NotFound(); return NotFound();
} }
return _mapper.Map<BarDto>(bar.Result); return Ok(_mapper.Map<BarDto>(bar));
} }
// PUT: api/v1/Bars/5 // PUT: api/v1/Bars/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")] [HttpPut("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> PutBar(long id, BarDto barDto) public async Task<IActionResult> PutBar(long id, BarDto barDto)
{ {
if (id != barDto.Id) if (id != barDto.Id)
{ {
return BadRequest(); return BadRequest();
} }
Bar bar = _mapper.Map<Bar>(barDto);
_context.Entry(bar).State = EntityState.Modified;
try var bar = await _context.Bars
{ .Include(b => b.Cats)
await _context.SaveChangesAsync(); .SingleOrDefaultAsync(b => b.Id == id);
}
catch (DbUpdateConcurrencyException) if (bar == null)
{ {
if (!BarExists(id)) return NotFound();
{
return NotFound();
}
else
{
throw;
}
} }
_mapper.Map(barDto, bar);
bar.Cats = await _context.Cats
.Where(c => barDto.CatIds.Contains(c.Id))
.ToListAsync();
await _context.SaveChangesAsync();
return NoContent(); return NoContent();
} }
// POST: api/v1/Bars // POST: api/v1/Bars
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost] [HttpPost]
[MapToApiVersion("1.0")] public async Task<ActionResult<BarDto>> CreateBar(BarDto barDto)
public async Task<ActionResult<BarDto>> PostBar(BarDto barDto)
{ {
// Bar bar = _mapper.Map<Bar>(barDto);
var bar = _mapper.Map<Bar>(barDto); var bar = _mapper.Map<Bar>(barDto);
bar.Cats = await _context.Cats
.Where(c => barDto.CatIds.Contains(c.Id))
.ToListAsync();
_context.Bars.Add(bar); _context.Bars.Add(bar);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return CreatedAtAction("GetBar", new { id = barDto.Id }, _mapper.Map<BarDto>(bar)); return CreatedAtAction(nameof(GetBar), new { id = bar.Id }, _mapper.Map<BarDto>(bar));
} }
// DELETE: api/v1/Bars/5 // DELETE: api/v1/Bars/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> DeleteBar(long id) public async Task<IActionResult> DeleteBar(long id)
{ {
var bar = await _context.Bars.FindAsync(id); var bar = await _context.Bars
.Include(b => b.Cats)
.SingleOrDefaultAsync(b => b.Id == id);
if (bar == null) if (bar == null)
{ {
return NotFound(); return NotFound();
@ -133,10 +112,5 @@ namespace cat_cafe.Controllers
return NoContent(); return NoContent();
} }
private bool BarExists(long id)
{
return _context.Bars.Any(e => e.Id == id);
}
} }
} }

@ -1,18 +1,10 @@
using System; using AutoMapper;
using System.Collections.Generic; using cat_cafe.Dto;
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.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using AutoMapper;
using cat_cafe.Dto;
using Serilog;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging.Abstractions;
using cat_cafe.WeSo; using cat_cafe.WeSo;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
@ -65,7 +57,6 @@ namespace cat_cafe.Controllers
// GET: api/v1/Cats/5 // GET: api/v1/Cats/5
[HttpGet("{id}")] [HttpGet("{id}")]
[MapToApiVersion("1.0")]
public async Task<ActionResult<CatDto>> GetCat(long id) public async Task<ActionResult<CatDto>> GetCat(long id)
{ {
var cat = await _context.Cats.FindAsync(id); var cat = await _context.Cats.FindAsync(id);
@ -81,7 +72,6 @@ namespace cat_cafe.Controllers
// PUT: api/v1/Cats/5 // PUT: api/v1/Cats/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")] [HttpPut("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> PutCat(long id, CatDto catDto) public async Task<IActionResult> PutCat(long id, CatDto catDto)
{ {
if (id != catDto.Id) if (id != catDto.Id)
@ -89,32 +79,24 @@ namespace cat_cafe.Controllers
return BadRequest(); return BadRequest();
} }
Cat cat = _mapper.Map<Cat>(catDto); var cat = await _context.Cats
_context.Entry(cat).State = EntityState.Modified; .SingleOrDefaultAsync(c => c.Id == id);
try if (cat == null)
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{ {
if (!CatExists(id)) return NotFound();
{
return NotFound();
}
else
{
throw;
}
} }
_mapper.Map(catDto, cat);
await _context.SaveChangesAsync();
return NoContent(); return NoContent();
} }
// POST: api/v1/Cats // POST: api/v1/Cats
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost] [HttpPost]
[MapToApiVersion("1.0")]
public async Task<ActionResult<CatDto>> PostCat(CatDto catDto) public async Task<ActionResult<CatDto>> PostCat(CatDto catDto)
{ {
Cat cat = _mapper.Map<Cat>(catDto); Cat cat = _mapper.Map<Cat>(catDto);
@ -128,7 +110,6 @@ namespace cat_cafe.Controllers
// DELETE: api/v1/Cats/5 // DELETE: api/v1/Cats/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> DeleteCat(long id) public async Task<IActionResult> DeleteCat(long id)
{ {
var cat = await _context.Cats.FindAsync(id); var cat = await _context.Cats.FindAsync(id);

@ -1,29 +1,24 @@
using System; using AutoMapper;
using System.Collections.Generic; using cat_cafe.Dto;
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.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using cat_cafe.Dto; using Microsoft.AspNetCore.Mvc;
using AutoMapper; using Microsoft.EntityFrameworkCore;
using Serilog;
using Newtonsoft.Json; using Newtonsoft.Json;
using Serilog;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
[Route("api/v{version:apiVersion}/[controller]")] [Route("api/v{version:apiVersion}/[controller]")]
[ApiController] [ApiController]
[ApiVersion("2.0")] [ApiVersion("1.0")]
public class CustomersController : ControllerBase public class CustomersController : ControllerBase
{ {
private readonly CatCafeContext _context; private readonly CatCafeContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger<CustomersController> _logger; private readonly ILogger<CustomersController> _logger;
public CustomersController(CatCafeContext context,IMapper mapper,ILogger<CustomersController> logger) public CustomersController(CatCafeContext context, IMapper mapper, ILogger<CustomersController> logger)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;
@ -32,7 +27,6 @@ namespace cat_cafe.Controllers
// GET: api/v1/Customers // GET: api/v1/Customers
[HttpGet] [HttpGet]
[MapToApiVersion("1.0")]
public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers() public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers()
{ {
Log.Information(this.Request.Method + " => get All customers"); Log.Information(this.Request.Method + " => get All customers");
@ -48,10 +42,9 @@ namespace cat_cafe.Controllers
// GET: api/v1/Customers/5 // GET: api/v1/Customers/5
[HttpGet("{id}")] [HttpGet("{id}")]
[MapToApiVersion("1.0")]
public async Task<ActionResult<CustomerDto>> GetCustomer(long id) public async Task<ActionResult<CustomerDto>> GetCustomer(long id)
{ {
Log.Information(this.Request.Method + " => get by ID {@id}",id); Log.Information(this.Request.Method + " => get by ID {@id}", id);
var customer = await _context.Customers.FindAsync(id); var customer = await _context.Customers.FindAsync(id);
if (customer == null) if (customer == null)
@ -70,49 +63,34 @@ namespace cat_cafe.Controllers
// PUT: api/v1/Customers/5 // PUT: api/v1/Customers/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")] [HttpPut("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto) public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto)
{ {
Log.Information(this.Request.Method + " => put by ID {@id}", id); Log.Information(this.Request.Method + " => put by ID {@id}", id);
if (id != customerDto.Id) if (id != customerDto.Id)
{ {
Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString()+" IDs not matching"); Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString() + " IDs not matching");
return BadRequest(); return BadRequest();
} }
Customer customer = _mapper.Map<Customer>(customerDto); var customer = await _context.Customers
.SingleOrDefaultAsync(c => c.Id == id);
_context.Entry(customer).State = EntityState.Modified;
try if (customer == null)
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException e)
{ {
if (!CustomerExists(id)) return NotFound();
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
else
{
Log.Error(this.Request.Method + " => " + e.Message);
throw;
}
} }
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " " _mapper.Map(customerDto, customer);
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString()); await _context.SaveChangesAsync();
return Ok();
return NoContent();
} }
// POST: api/v1/Customers // POST: api/v1/Customers
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost] [HttpPost]
[MapToApiVersion("1.0")] public async Task<ActionResult<CustomerDto>> PostCustomer(CustomerDto customerDto)
public async Task<ActionResult<Customer>> PostCustomer(CustomerDto customerDto)
{ {
Log.Information(this.Request.Method + " => post customer"); Log.Information(this.Request.Method + " => post customer");
@ -125,12 +103,11 @@ namespace cat_cafe.Controllers
+ customer.GetType().ToString() + " " + customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString()); + JsonConvert.SerializeObject(customer).ToString());
return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<Customer>( customer)); return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<CustomerDto>(customer));
} }
// DELETE: api/v1/Customers/5 // DELETE: api/v1/Customers/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
[MapToApiVersion("1.0")]
public async Task<IActionResult> DeleteCustomer(long id) public async Task<IActionResult> DeleteCustomer(long id)
{ {
Log.Information(this.Request.Method + " => delete by ID {@id}", id); Log.Information(this.Request.Method + " => delete by ID {@id}", id);

@ -1,13 +1,10 @@
using System; namespace cat_cafe.Dto
using cat_cafe.Entities;
namespace cat_cafe.Dto
{ {
public class BarDto public class BarDto
{ {
public long Id { get; set; } public long Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public List<CatDto> cats { get; set; } = new List<CatDto>(); public List<long> CatIds { get; set; } = new();
} }
} }

@ -1,9 +1,9 @@
using System; namespace cat_cafe.Dto
namespace cat_cafe.Dto
{ {
public class CatDto public class CatDto
{ {
public long Id { get; set; } public long Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public long? BarId { get; set; }
} }
} }

@ -1,10 +1,9 @@
using System; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Dto namespace cat_cafe.Dto
{ {
public class CustomerDto public class CustomerDto
{ {
public long Id { get; set; } public long Id { get; set; }
[Required] [Required]
public string FullName { get; set; } public string FullName { get; set; }

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

@ -8,7 +8,8 @@ namespace cat_cafe.Entities
public string? Name { get; set; } public string? Name { get; set; }
[Required] [Required]
public int Age { get; set; } = 0; public int Age { get; set; } = 0;
public long? BarId { get; set; }
public Bar? Bar { get; set; }
public string Meow() { return "meow"; } public string Meow() { return "meow"; }
} }
} }

@ -1,5 +1,4 @@
using System; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Entities namespace cat_cafe.Entities
{ {

@ -1,18 +1,17 @@
using System; using AutoMapper;
using AutoMapper;
using cat_cafe.Dto; using cat_cafe.Dto;
using cat_cafe.Entities; using cat_cafe.Entities;
namespace cat_cafe.Mappers namespace cat_cafe.Mappers
{ {
public class BarMapper:Profile public class BarMapper : Profile
{ {
public BarMapper() public BarMapper()
{ {
CreateMap<Bar, BarDto>()
.ForMember(dest => dest.CatIds, opt => opt.MapFrom(src => src.Cats.Select(c => c.Id)));
// var mapper = config.CreateMapper(); CreateMap<BarDto, Bar>();
CreateMap<Bar, BarDto>().ReverseMap(); }
} }
}
} }

@ -2,15 +2,14 @@
using cat_cafe.Dto; using cat_cafe.Dto;
using cat_cafe.Entities; using cat_cafe.Entities;
namespace cat_cafe.Mappers namespace cat_cafe.Mappers
{ {
public class CustomerMapper:Profile public class CustomerMapper : Profile
{ {
public CustomerMapper() public CustomerMapper()
{ {
CreateMap<Customer, CustomerDto>().ReverseMap(); CreateMap<Customer, CustomerDto>().ReverseMap();
} }
} }
} }

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using cat_cafe.WeSo;
using Microsoft.EntityFrameworkCore;
using Serilog; using Serilog;
using System.Net.WebSockets; using System.Net.WebSockets;
using cat_cafe.WeSo;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -15,22 +15,32 @@ List<WebSocket> _sockets = new();
builder.Services.AddSingleton<List<WebSocket>>(x => _sockets); builder.Services.AddSingleton<List<WebSocket>>(x => _sockets);
builder.Services.AddSingleton<WebSocketHandler>(); builder.Services.AddSingleton<WebSocketHandler>();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddDbContext<CatCafeContext>(opt => opt.UseInMemoryDatabase("CatCafe")); builder.Services.AddDbContext<CatCafeContext>(opt => opt.UseSqlite("Data Source=cat_cafe.db"));
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper(typeof(Program)); builder.Services.AddAutoMapper(typeof(Program));
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddApiVersioning(o => { o.ReportApiVersions = true; }); builder.Services.AddApiVersioning(opt => { opt.ReportApiVersions = true; });
builder.Services.AddVersionedApiExplorer( builder.Services.AddVersionedApiExplorer(
options => opt =>
{ {
options.GroupNameFormat = "'v'VVV"; opt.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true; opt.SubstituteApiVersionInUrl = true;
} }
); );
var app = builder.Build(); var app = builder.Build();
using (var serviceScope = app.Services.CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<CatCafeContext>();
context.Database.EnsureCreated();
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}
}
app.UseHttpLogging(); app.UseHttpLogging();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
@ -48,7 +58,6 @@ app.MapControllers();
app.UseWebSockets(); app.UseWebSockets();
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {
if (context.Request.Path == "/ws") if (context.Request.Path == "/ws")

@ -14,5 +14,12 @@ namespace cat_cafe.Repositories
public DbSet<Bar> Bars { get; set; } = null!; public DbSet<Bar> Bars { get; set; } = null!;
public DbSet<Customer> Customers { get; set; } = null!; public DbSet<Customer> Customers { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Cat>()
.HasOne(c => c.Bar)
.WithMany(b => b.Cats)
.HasForeignKey(c => c.BarId);
}
} }
} }

@ -4,16 +4,18 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" /> <PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.12"> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>

Loading…
Cancel
Save