-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start data seeding
- Loading branch information
Showing
18 changed files
with
307 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.OData.Query; | ||
using Microsoft.AspNetCore.OData.Routing.Attributes; | ||
|
||
using Faker; | ||
|
||
using Api.Data; | ||
using Api.Models; | ||
|
||
namespace Api.Controllers; | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class CustomerController : ControllerBase { | ||
private readonly ApiContext _context; | ||
public CustomerController(ApiContext context) { | ||
_context = context; | ||
} | ||
|
||
[EnableQuery] | ||
[ODataAttributeRouting] | ||
[HttpGet("~/customers")] | ||
public IActionResult OData() { | ||
return Ok(_context.Customers); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
using Microsoft.OData.ModelBuilder; | ||
using Microsoft.OData.Edm; | ||
|
||
namespace Api.Data { | ||
public static class ODataModelBuilder { | ||
public static IEdmModel Build() { | ||
var builder = new ODataConventionModelBuilder(); | ||
using Api.Models; | ||
|
||
return builder.GetEdmModel(); | ||
} | ||
namespace Api.Data; | ||
public static class ODataModelBuilder { | ||
public static IEdmModel Build() { | ||
var builder = new ODataConventionModelBuilder(); | ||
|
||
builder.EntitySet<Customer>("Customer"); | ||
|
||
return builder.GetEdmModel(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Api.Models; | ||
|
||
namespace Api.Data; | ||
|
||
public class Seeder { | ||
public async Task Seed() { | ||
List<Customer> customers = new List<Customer>(); | ||
for (int i = 0; i < 1000; i++) { | ||
customers.Add(new Customer { | ||
FirstName = Faker.Name.First(), | ||
MiddleNames = _randomNull(Faker.Name.Middle()), | ||
Surname = Faker.Name.Last(), | ||
CreatedDate = _randomDateTime(), | ||
Addresses = _randomCollection(() => new Address { | ||
Line1 = _randomChoice(Faker.Address.StreetAddress(), Faker.Address.SecondaryAddress(), out bool line1Full, 75), | ||
Line2 = !line1Full ? Faker.Address.StreetName() : null, | ||
Line3 = _randomNull(Faker.Address.City(), 10), | ||
Town = Faker.Address.City(), | ||
County = _randomNull(Faker.Address.UkCounty(), 90), | ||
Country = Faker.Address.UkCountry(), | ||
PostCode = Faker.Address.UkPostCode() | ||
}).ToList() | ||
}); | ||
} | ||
} | ||
|
||
private T? _randomNull<T>(T value, int chance = 50) where T : class { | ||
if (Random.Shared.Next(0, 100) < chance) { | ||
return value; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private T _randomChoice<T>(T val1, T val2, out bool firstChosen, int chance = 50) { | ||
if (Random.Shared.Next(0, 100) < chance) { | ||
firstChosen = true; | ||
return val1; | ||
} else { | ||
firstChosen = false; | ||
return val2; | ||
} | ||
} | ||
|
||
private IEnumerable<T> _randomCollection<T>(Func<T> generate, int min = 0, int max = 5) { | ||
int count = Random.Shared.Next(min, max); | ||
|
||
for (int i = 0; i < count; i++) { | ||
yield return generate(); | ||
} | ||
} | ||
|
||
private DateTime _randomDateTime() { | ||
long range = (_maxDate - _minDate).Ticks; | ||
|
||
return _minDate.AddTicks(Random.Shared.NextInt64(0, range)); | ||
} | ||
|
||
private readonly DateTime _minDate = new DateTime(2010, 01, 01); | ||
private readonly DateTime _maxDate = DateTime.Now; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/obj/**/*</DefaultItemExcludes> | ||
<DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/bin/**/*</DefaultItemExcludes> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == 'true'"> | ||
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/container/</BaseIntermediateOutputPath> | ||
<BaseOutputPath>$(MSBuildProjectDirectory)/bin/container/</BaseOutputPath> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env | ||
WORKDIR /app | ||
|
||
# Copy csproj and restore as distinct layers | ||
COPY *.csproj ./ | ||
RUN dotnet restore | ||
|
||
# Copy everything else and build | ||
ADD . ./ | ||
|
||
RUN dotnet publish -c Release -o out | ||
|
||
# Build runtime image | ||
FROM mcr.microsoft.com/dotnet/aspnet:6.0 | ||
WORKDIR /app | ||
COPY --from=build-env /app/out . | ||
ENTRYPOINT ["dotnet", "api.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env | ||
WORKDIR /app | ||
|
||
# install Visual Studio debugger | ||
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg | ||
|
||
# Copy csproj and restore as distinct layers | ||
COPY *.csproj ./ | ||
RUN dotnet restore | ||
|
||
ENTRYPOINT dotnet watch run --urls=https://+:5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
namespace Api.Models { | ||
public class Address { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int CustomerId { get; set; } | ||
public Customer Customer { get; set; } = null!; | ||
public string Line1 { get; set; } = null!; | ||
public string? Line2 { get; set; } | ||
public string? Line3 { get; set; } | ||
public string Town { get; set; } = null!; | ||
public string? County { get; set; } | ||
public string Country { get; set; } = null!; | ||
public string PostCode { get; set; } = null!; | ||
} | ||
namespace Api.Models; | ||
public class Address { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int CustomerId { get; set; } | ||
public Customer Customer { get; set; } = null!; | ||
public string Line1 { get; set; } = null!; | ||
public string? Line2 { get; set; } | ||
public string? Line3 { get; set; } | ||
public string Town { get; set; } = null!; | ||
public string? County { get; set; } | ||
public string Country { get; set; } = null!; | ||
public string PostCode { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
namespace Api.Models { | ||
public class Customer { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public string FirstName { get; set; } = null!; | ||
public string? MiddleNames { get; set; } | ||
public string Surname { get; set; } = null!; | ||
public string EmailAddress { get; set; } = null!; | ||
public DateTime CreatedDate { get; set; } | ||
public List<Address> Addresses { get; set; } = null!; | ||
public List<Order> Orders { get; set; } = null!; | ||
} | ||
namespace Api.Models; | ||
public class Customer { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public string FirstName { get; set; } = null!; | ||
public string? MiddleNames { get; set; } | ||
public string Surname { get; set; } = null!; | ||
public string EmailAddress { get; set; } = null!; | ||
public DateTime CreatedDate { get; set; } | ||
public List<Address> Addresses { get; set; } = null!; | ||
public List<Order> Orders { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
namespace Api.Models { | ||
public class Order { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int CustomerId { get; set; } | ||
public Customer Customer { get; set; } = null!; | ||
public int DeliveryAddressId { get; set; } | ||
public Address DeliveryAddress { get; set; } = null!; | ||
public decimal Total { get; set; } | ||
public List<OrderProduct> Products { get; set; } = null!; | ||
} | ||
namespace Api.Models; | ||
public class Order { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int CustomerId { get; set; } | ||
public Customer Customer { get; set; } = null!; | ||
public int DeliveryAddressId { get; set; } | ||
public Address DeliveryAddress { get; set; } = null!; | ||
public decimal Total { get; set; } | ||
public List<OrderProduct> Products { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
namespace Api.Models { | ||
public class OrderProduct { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int OrderId { get; set; } | ||
public Order Order { get; set; } = null!; | ||
public int ProductId { get; set; } | ||
public Product Product { get; set; } = null!; | ||
public int Quantity { get; set; } | ||
} | ||
namespace Api.Models; | ||
public class OrderProduct { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public int OrderId { get; set; } | ||
public Order Order { get; set; } = null!; | ||
public int ProductId { get; set; } | ||
public Product Product { get; set; } = null!; | ||
public int Quantity { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
namespace Api.Models { | ||
public class Product { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public string Name { get; set; } = null!; | ||
public decimal Price { get; set; } | ||
} | ||
namespace Api.Models; | ||
public class Product { | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int Id { get; set; } | ||
public string Name { get; set; } = null!; | ||
public decimal Price { get; set; } | ||
} |
Oops, something went wrong.