Compare commits

...

14 Commits

Author SHA1 Message Date
Developer 02
6dc8bd233b feat(ProxyController): Optionaler Körper als Dictionary<string, object> hinzugefügt 2025-01-30 15:11:38 +01:00
Developer 02
54fce2990b fix(ProxyController): Hinzufügen von Standard-Headern zur Antwort entfernt 2025-01-30 13:44:26 +01:00
Developer 02
3eed85e7de feat(ProxyController): Cookies und Header hinzugefügt 2025-01-30 13:34:04 +01:00
Developer 02
61bb5acdff refactor(ProxyController): Updated to add default headers 2025-01-30 12:10:30 +01:00
Developer 02
d8ee61d107 refactor(OriginServerParams): Umbenennung von Headers in DefaultHeaders. 2025-01-30 11:46:57 +01:00
Developer 02
7189b39d6b refactor(ProxyController): Aktualisiert, um JsonResult zu verwenden, wenn es einen Körper in der Redir-Methode gibt. 2025-01-30 11:39:18 +01:00
Developer 02
1fde6e7e34 feat(MockController): Erstellt, um einen echten Server zu simulieren 2025-01-30 11:10:41 +01:00
Developer 02
54eca6fceb feat(ProxyController): Umleitungsendpunkt für Post-Requests mit dem Muster „{ServicetierName}/ODataV4/{WebserviceName}_CreateInvoice“ implementiert 2025-01-30 10:20:00 +01:00
Developer 02
782680cda2 feat(CompanyRepo): Aktualisiert zur Verwendung von ApiDbContext 2025-01-30 00:49:36 +01:00
Developer 02
aeb1c4a810 chore: ef In-Memory-Datenbank hinzugefügt und Dienst hinzugefügt 2025-01-30 00:09:02 +01:00
Developer 02
67a4e91a1b feat(ApiDbContext): Mit Seed hinzugefügt 2025-01-30 00:06:58 +01:00
Developer 02
6ba7fe230a feat(CompaniesController): OData implementiert 2025-01-29 23:58:04 +01:00
Developer 02
9473ad7619 chore: Konfigurierte OData-Konvention Model Builder 2025-01-29 23:53:55 +01:00
Developer 02
c70257bb28 feat(Company): Entitäten und Repository erstellt 2025-01-29 23:52:21 +01:00
14 changed files with 352 additions and 9 deletions

View File

@ -0,0 +1,73 @@
using DigitalData.Swagger.MockAPI.Dtos;
using DigitalData.Swagger.MockAPI.Repos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
namespace DigitalData.Swagger.MockAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class CompaniesController(ICompanyRepo repo) : ControllerBase
{
private readonly ICompanyRepo _repo = repo;
[EnableQuery(PageSize = 3)]
[HttpGet]
public IQueryable<Company> Get()
{
return _repo.GetAll();
}
[EnableQuery]
[HttpGet("{id}")]
public SingleResult<Company> Get([FromODataUri] int key)
{
return SingleResult.Create(_repo.GetById(key));
}
[HttpPost]
public IActionResult Post([FromBody] Company company)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_repo.Create(company);
return Created("companies", company);
}
[HttpPut]
public IActionResult Put([FromODataUri] int key, [FromBody] Company company)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != company.ID)
{
return BadRequest();
}
_repo.Update(company);
return NoContent();
}
[HttpDelete]
public IActionResult Delete([FromODataUri] int key)
{
var company = _repo.GetById(key);
if (company is null)
{
return BadRequest();
}
_repo.Delete(company.First());
return NoContent();
}
}

View File

@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Mvc;
namespace DigitalData.Swagger.MockAPI.Controllers
{
[ApiController]
public class MockController : ControllerBase
{
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public IActionResult CreateInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, Dictionary<string, object>? body)
{
return Created($"{servicetierName}/ODataV4/{webserviceName}?id={1}", new { Id = 1, Foo = 1});
}
[HttpGet("{servicetierName}/ODataV4/{webserviceName}")]
public IActionResult GetInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string id)
{
return Ok(new { Id = id, Foo = 1 });
}
}
}

View File

@ -0,0 +1,11 @@
using DigitalData.Swagger.MockAPI.Dtos;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Swagger.MockAPI.Data;
public class ApiContext(DbContextOptions<ApiContext> options) : DbContext(options)
{
public DbSet<Company> Companies { get; set; }
public DbSet<Product> Products { get; set; }
}

View File

@ -0,0 +1,75 @@
using DigitalData.Swagger.MockAPI.Dtos;
using System;
namespace DigitalData.Swagger.MockAPI.Data
{
public class DBSeeder
{
public static void AddCompaniesData(WebApplication app)
{
var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetService<ApiContext>();
db.Companies.AddRange(new List<Company>()
{
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 }
}
}
});
db.SaveChanges();
}
}
}

View File

@ -7,11 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,9 @@
namespace DigitalData.Swagger.MockAPI.Dtos;
public class Company
{
public int ID { get; set; }
public string? Name { get; set; }
public int Size { get; set; }
public List<Product>? Products { get; set; }
}

View File

@ -0,0 +1,9 @@
namespace DigitalData.Swagger.MockAPI.Dtos;
public class Product
{
public int ID { get; set; }
public int CompanyID { get; set; }
public string? Name { get; set; }
public decimal Price { get; set; }
}

View File

@ -1,7 +1,30 @@
using DigitalData.Swagger.MockAPI.Data;
using DigitalData.Swagger.MockAPI.Dtos;
using DigitalData.Swagger.MockAPI.Repos;
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.ModelBuilder;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
builder.Services.AddScoped<ICompanyRepo, CompanyRepo>();
ODataConventionModelBuilder oDataModelbuilder = new();
oDataModelbuilder.EntitySet<Company>("Companies");
builder.Services.AddControllers()
.AddOData(options => options
.AddRouteComponents("odata", oDataModelbuilder.GetEdmModel())
.Select()
.Filter()
.OrderBy()
.SetMaxTop(20)
.Count()
.Expand()
);
builder.Services.AddControllers();
@ -10,6 +33,8 @@ builder.Services.AddSwaggerGen();
var app = builder.Build();
DBSeeder.AddCompaniesData(app);
// Configure the HTTP request pipeline.
if (config.GetValue<bool>("UseSwagger"))
{
@ -23,4 +48,4 @@ app.UseAuthorization();
app.MapControllers();
app.Run();
app.Run();

View File

@ -0,0 +1,46 @@
using DigitalData.Swagger.MockAPI.Data;
using DigitalData.Swagger.MockAPI.Dtos;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Swagger.MockAPI.Repos;
public class CompanyRepo(ApiContext context) : ICompanyRepo
{
private readonly ApiContext _context = context;
public IQueryable<Company> GetAll()
{
return _context.Companies
.Include(a => a.Products)
.AsQueryable();
}
public IQueryable<Company> GetById(int id)
{
return _context.Companies
.Include(a => a.Products)
.AsQueryable()
.Where(c => c.ID == id);
}
public void Create(Company company)
{
_context.Companies
.Add(company);
_context.SaveChanges();
}
public void Update(Company company)
{
_context.Companies
.Update(company);
_context.SaveChanges();
}
public void Delete(Company company)
{
_context.Companies
.Remove(company);
_context.SaveChanges();
}
}

View File

@ -0,0 +1,11 @@
using DigitalData.Swagger.MockAPI.Dtos;
namespace DigitalData.Swagger.MockAPI.Repos;
public interface ICompanyRepo
{
public IQueryable<Company> GetAll();
public IQueryable<Company> GetById(int id);
public void Create(Company company);
public void Update(Company company);
public void Delete(Company company);
}

View File

@ -4,6 +4,6 @@
{
public required string Url { get; init; }
public Dictionary<string, string>? Headers { get; init; }
public Dictionary<string, string> DefaultHeaders { get; init; } = new();
}
}

View File

@ -0,0 +1,58 @@
namespace DigitalData.Swagger.Proxy.Controllers;
using DigitalData.Swagger.Proxy.Configs;
using Flurl;
using Flurl.Http;
using Flurl.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
[ApiController]
public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase
{
private readonly OriginServerParams _origin = originServerParamsOptions.Value;
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, [FromBody] Dictionary<string, object>? body = null)
{
var req = _origin.Url
.AppendPathSegment(servicetierName)
.AppendPathSegment("ODataV4")
.AppendPathSegment(webserviceName + "_CreateInvoice")
.SetQueryParams(new { company })
.WithHeader("X-Forwarded-For", HttpContext.Connection.RemoteIpAddress?.ToString());
// Add default headers
foreach (var header in _origin.DefaultHeaders)
req = req.WithHeader(header.Key, header.Value);
// Add request headers
foreach (var header in HttpContext.Request.Headers)
req = req.WithHeader(header.Key, header.Value);
// Add reqest cookies
foreach (var cookie in HttpContext.Request.Cookies)
req = req.WithCookie(cookie.Key, cookie.Value);
//post request
var res = body is null ? await req.PostAsync() : await req.PostJsonAsync(body);
// set headers
foreach (var (Name, Value) in res.Headers)
HttpContext.Response.Headers[Name] = Value;
// set cookies
foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs())
HttpContext.Response.Cookies.Append(Name, Value.ToString() ?? string.Empty);
var resBody = res.GetJsonAsync<dynamic>();
if (resBody is not null)
return new JsonResult(resBody)
{
StatusCode = res.StatusCode
};
return StatusCode(res.StatusCode);
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -5,5 +5,11 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
"AllowedHosts": "*",
"OriginServer": {
"Url": "https://localhost:7248",
"DefaultHeaders": {
"Authorization": "Basic username:password"
}
}
}