Compare commits
4 Commits
master
...
67a4e91a1b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67a4e91a1b | ||
|
|
6ba7fe230a | ||
|
|
9473ad7619 | ||
|
|
c70257bb28 |
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal file
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal 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; }
|
||||||
|
}
|
||||||
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal file
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,11 +7,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Controllers\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
9
src/DigitalData.Swagger.MockAPI/Entities/Company.cs
Normal file
9
src/DigitalData.Swagger.MockAPI/Entities/Company.cs
Normal 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; }
|
||||||
|
}
|
||||||
9
src/DigitalData.Swagger.MockAPI/Entities/Product.cs
Normal file
9
src/DigitalData.Swagger.MockAPI/Entities/Product.cs
Normal 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; }
|
||||||
|
}
|
||||||
@@ -1,7 +1,27 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using DigitalData.Swagger.MockAPI.Repos;
|
||||||
|
using Microsoft.AspNetCore.OData;
|
||||||
|
using Microsoft.OData.ModelBuilder;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
var config = builder.Configuration;
|
var config = builder.Configuration;
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
builder.Services.AddSingleton<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();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
|||||||
95
src/DigitalData.Swagger.MockAPI/Repos/CompanyRepo.cs
Normal file
95
src/DigitalData.Swagger.MockAPI/Repos/CompanyRepo.cs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
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 IQueryable<Company> GetAll()
|
||||||
|
{
|
||||||
|
return _companies.AsQueryable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IQueryable<Company> GetById(int id)
|
||||||
|
{
|
||||||
|
return _companies
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(c => c.ID == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Create(Company company)
|
||||||
|
{
|
||||||
|
_companies.Add(company);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(Company company)
|
||||||
|
{
|
||||||
|
var i = _companies.FindIndex(c => c.ID == company.ID);
|
||||||
|
_companies[i] = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(Company company)
|
||||||
|
{
|
||||||
|
_companies.RemoveAll(c => c.ID == company.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/DigitalData.Swagger.MockAPI/Repos/ICompanyRepo.cs
Normal file
11
src/DigitalData.Swagger.MockAPI/Repos/ICompanyRepo.cs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user