Compare commits

...

10 Commits

8 changed files with 117 additions and 75 deletions

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

@ -9,6 +9,7 @@
<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>

View File

@ -1,13 +1,16 @@
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.AddSingleton<ICompanyRepo, CompanyRepo>();
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
builder.Services.AddScoped<ICompanyRepo, CompanyRepo>();
ODataConventionModelBuilder oDataModelbuilder = new();
oDataModelbuilder.EntitySet<Company>("Companies");
@ -30,6 +33,8 @@ builder.Services.AddSwaggerGen();
var app = builder.Build();
DBSeeder.AddCompaniesData(app);
// Configure the HTTP request pipeline.
if (config.GetValue<bool>("UseSwagger"))
{
@ -43,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();
}
}

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"
}
}
}