feat(CompanyRepo): Aktualisiert zur Verwendung von ApiDbContext

This commit is contained in:
Developer 02 2025-01-30 00:49:36 +01:00
parent aeb1c4a810
commit 782680cda2
2 changed files with 25 additions and 73 deletions

View File

@ -9,13 +9,12 @@ var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddSingleton<ICompanyRepo, CompanyRepo>();
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
builder.Services.AddScoped<ICompanyRepo, CompanyRepo>();
ODataConventionModelBuilder oDataModelbuilder = new();
oDataModelbuilder.EntitySet<Company>("Companies");
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
builder.Services.AddControllers()
.AddOData(options => options
.AddRouteComponents("odata", oDataModelbuilder.GetEdmModel())
@ -34,6 +33,8 @@ builder.Services.AddSwaggerGen();
var app = builder.Build();
DBSeeder.AddCompaniesData(app);
// Configure the HTTP request pipeline.
if (config.GetValue<bool>("UseSwagger"))
{
@ -47,4 +48,4 @@ app.UseAuthorization();
app.MapControllers();
app.Run();
app.Run();

View File

@ -1,95 +1,46 @@
using DigitalData.Swagger.MockAPI.Dtos;
using System;
using DigitalData.Swagger.MockAPI.Data;
using DigitalData.Swagger.MockAPI.Dtos;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Swagger.MockAPI.Repos;
public class CompanyRepo : ICompanyRepo
{
private readonly Lazy<List<Company>> _lazyCompanies = new(() => new()
{
new()
{
ID = 1,
Name = "TechCorp",
Size = 500,
Products = new List<Product>
{
new() { ID = 1, CompanyID = 1, Name = "Laptop X1", Price = 1200.99m },
new() { ID = 2, CompanyID = 1, Name = "Smartphone S2", Price = 799.49m }
}
},
new Company
{
ID = 2,
Name = "AutoWorks",
Size = 300,
Products = new List<Product>
{
new Product { ID = 3, CompanyID = 2, Name = "Electric Car E1", Price = 35000m },
new() { ID = 4, CompanyID = 2, Name = "Hybrid SUV H2", Price = 42000m }
}
},
new Company
{
ID = 3,
Name = "MediHealth",
Size = 200,
Products = new List<Product>
{
new() { ID = 5, CompanyID = 3, Name = "Vitamin Pack V10", Price = 19.99m },
new() { ID = 6, CompanyID = 3, Name = "Protein Shake P5", Price = 25.49m }
}
},
new Company
{
ID = 4,
Name = "HomeStyle",
Size = 150,
Products = new List<Product>
{
new() { ID = 7, CompanyID = 4, Name = "Smart Blender B3", Price = 89.99m },
new() { ID = 8, CompanyID = 4, Name = "Air Purifier A1", Price = 149.99m }
}
},
new Company
{
ID = 5,
Name = "GadgetZone",
Size = 400,
Products = new List<Product>
{
new() { ID = 9, CompanyID = 5, Name = "Wireless Earbuds W1", Price = 99.99m },
new() { ID = 10, CompanyID = 5, Name = "Smart Watch G5", Price = 199.99m }
}
}
});
private List<Company> _companies => _lazyCompanies.Value;
public class CompanyRepo(ApiContext context) : ICompanyRepo
{
private readonly ApiContext _context = context;
public IQueryable<Company> GetAll()
{
return _companies.AsQueryable();
return _context.Companies
.Include(a => a.Products)
.AsQueryable();
}
public IQueryable<Company> GetById(int id)
{
return _companies
return _context.Companies
.Include(a => a.Products)
.AsQueryable()
.Where(c => c.ID == id);
}
public void Create(Company company)
{
_companies.Add(company);
_context.Companies
.Add(company);
_context.SaveChanges();
}
public void Update(Company company)
{
var i = _companies.FindIndex(c => c.ID == company.ID);
_companies[i] = company;
_context.Companies
.Update(company);
_context.SaveChanges();
}
public void Delete(Company company)
{
_companies.RemoveAll(c => c.ID == company.ID);
_context.Companies
.Remove(company);
_context.SaveChanges();
}
}