Skip to content

Commit

Permalink
allow mapping keys to entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Porro committed Jan 28, 2024
1 parent 5e31b10 commit 6d55e87
Show file tree
Hide file tree
Showing 62 changed files with 590 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Detached.Mappers.EntityFramework.Contrib.SysTec.Dtos.Bug17;
using Detached.Mappers.EntityFramework.Contrib.SysTec.Dtos.Bug18;
using Detached.Mappers.EntityFramework.Contrib.SysTec.Dtos.inheritance;
using Detached.Mappers.EntityFramework.Profiles;
using Detached.Mappers.EntityFramework.Extensions;
using GraphInheritenceTests.Dtos;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
Expand Down
19 changes: 13 additions & 6 deletions sample/Detached.Samples.RestApi/Controllers/InvoiceController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Detached.Mappers.Samples.RestApi.Models.Inputs;
using Detached.Mappers.Samples.RestApi.Models.Outputs;
using Detached.Mappers.Samples.RestApi.Services;
using Detached.Samples.RestApi.Models.Inputs;
using Detached.Samples.RestApi.Models.Outputs;
using Detached.Samples.RestApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Detached.Mappers.Samples.RestApi.Controllers
namespace Detached.Samples.RestApi.Controllers
{
[ApiController]
[Route("api/invoices")]
Expand All @@ -24,8 +24,15 @@ public async Task<ActionResult<IEnumerable<InvoiceListItem>>> GetAsync()
return Ok(await _invoiceService.GetAsync());
}

[HttpPost]
public async Task<ActionResult> SaveAsync([FromBody]SaveInvoiceInput input)
[HttpPost("identity_fks")]
public async Task<ActionResult> SaveAsync_Identity([FromBody] SaveInvoiceInput_Identity input)
{
await _invoiceService.SaveAsync(input);
return Ok();
}

[HttpPost("primitive_fks")]
public async Task<ActionResult> SaveAsync_Primitive([FromBody] SaveInvoiceInput_Primitive input)
{
await _invoiceService.SaveAsync(input);
return Ok();
Expand Down
6 changes: 3 additions & 3 deletions sample/Detached.Samples.RestApi/MainDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Detached.Mappers.Samples.RestApi.Models;
using Detached.Samples.RestApi.Models;
using Microsoft.EntityFrameworkCore;

namespace Detached.Mappers.Samples.RestApi
namespace Detached.Samples.RestApi
{
public class MainDbContext : DbContext
{
public MainDbContext(DbContextOptions<MainDbContext> options)
public MainDbContext(DbContextOptions<MainDbContext> options)
: base(options)
{
}
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/Core/Identity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models.Core
{
public class Identity
{
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/Core/Item.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Detached.Mappers.Samples.RestApi.Models.Core
namespace Detached.Samples.RestApi.Models.Core
{
public class Item : Identity
{
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Detached.Annotations;
using System.ComponentModel.DataAnnotations;

namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models
{
[Entity]
public class Customer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Detached.Samples.RestApi.Models.Core;
using System;
using System.Collections.Generic;

namespace Detached.Mappers.Samples.RestApi.Models.Inputs
namespace Detached.Samples.RestApi.Models.Inputs
{
public class SaveInvoiceInput
public class SaveInvoiceInput_Identity
{
public int Id { get; set; }
public List<SaveInvoiceRowInput> Rows { get; set; }

public List<SaveInvoiceRowInput_Identity> Rows { get; set; }

public Identity Type { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace Detached.Samples.RestApi.Models.Inputs
{
public class SaveInvoiceInput_Primitive
{
public int Id { get; set; }

public List<SaveInvoiceRowInput_Primitive> Rows { get; set; }

public int TypeId { get; set; }

public int CustomerId { get; set; }

public DateTime DateTime { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Detached.Mappers.Samples.RestApi.Models.Inputs
using Detached.Samples.RestApi.Models.Core;

namespace Detached.Samples.RestApi.Models.Inputs
{
public class SaveInvoiceRowInput
public class SaveInvoiceRowInput_Identity
{
public int Id { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Detached.Samples.RestApi.Models.Inputs
{
public class SaveInvoiceRowInput_Primitive
{
public int Id { get; set; }

public int SKUId { get; set; }

public string Description { get; set; }

public decimal UnitPrice { get; set; }

public double Quantity { get; set; }
}
}
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/Invoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models
{
[Entity]
public class Invoice
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/InvoiceRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Detached.Mappers.Annotations;
using System.ComponentModel.DataAnnotations;

namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models
{
[Entity]
public class InvoiceRow
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/InvoiceType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Detached.Annotations;
using System.ComponentModel.DataAnnotations;

namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models
{
[Entity]
public class InvoiceType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Detached.Mappers.Samples.RestApi.Models.Core;
using Detached.Samples.RestApi.Models.Core;
using System;

namespace Detached.Mappers.Samples.RestApi.Models.Outputs
namespace Detached.Samples.RestApi.Models.Outputs
{
public class InvoiceListItem
{
public int Id { get; set; }

public Item Type { get; set; }

public Item Customer { get; set; }

public DateTime DateTime { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Models/StockUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Detached.Mappers.Annotations;
using System.ComponentModel.DataAnnotations;

namespace Detached.Mappers.Samples.RestApi.Models
namespace Detached.Samples.RestApi.Models
{
[Entity]
public class StockUnit
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Detached.Mappers.Samples.RestApi
namespace Detached.Samples.RestApi
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Detached.Mappers.Samples.RestApi": {
"Kestrel": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
Expand Down
15 changes: 10 additions & 5 deletions sample/Detached.Samples.RestApi/Services/InvoiceService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Detached.Mappers.Samples.RestApi.Models.Inputs;
using Detached.Mappers.Samples.RestApi.Models.Outputs;
using Detached.Mappers.Samples.RestApi.Stores;
using Detached.Samples.RestApi.Models.Inputs;
using Detached.Samples.RestApi.Models.Outputs;
using Detached.Samples.RestApi.Stores;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Detached.Mappers.Samples.RestApi.Services
namespace Detached.Samples.RestApi.Services
{
public class InvoiceService
{
Expand All @@ -20,7 +20,12 @@ public Task<IEnumerable<InvoiceListItem>> GetAsync()
return _invoiceStore.GetAsync();
}

public Task SaveAsync(SaveInvoiceInput input)
public Task SaveAsync(SaveInvoiceInput_Identity input)
{
return _invoiceStore.SaveAsync(input);
}

public Task SaveAsync(SaveInvoiceInput_Primitive input)
{
return _invoiceStore.SaveAsync(input);
}
Expand Down
17 changes: 13 additions & 4 deletions sample/Detached.Samples.RestApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Detached.Mappers;
using Detached.Mappers.EntityFramework;
using Detached.Mappers.Samples.RestApi.Models;
using Detached.Mappers.Samples.RestApi.Services;
using Detached.Mappers.Samples.RestApi.Stores;
using Detached.Samples.RestApi.Models;
using Detached.Samples.RestApi.Services;
using Detached.Samples.RestApi.Stores;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
Expand All @@ -11,7 +12,7 @@
using System;
using System.Collections.Generic;

namespace Detached.Mappers.Samples.RestApi
namespace Detached.Samples.RestApi
{
public class Startup
{
Expand Down Expand Up @@ -79,13 +80,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MainDbCo
void Seed(MainDbContext db)
{
db.InvoiceTypes.Add(new InvoiceType { Name = "Taxed" });
db.InvoiceTypes.Add(new InvoiceType { Name = "Free" });
db.Customers.Add(new Customer
{
DocumentNumber = "123",
Name = "sample customer",
Email = "[email protected]",
Phone = "123 456 7890"
});
db.Customers.Add(new Customer
{
DocumentNumber = "457",
Name = "another customer",
Email = "[email protected]",
Phone = "123 456 7890"
});
db.StockUnits.Add(new StockUnit { Name = "Potato", Quantity = 10 });
db.StockUnits.Add(new StockUnit { Name = "Tomato", Quantity = 8 });
db.SaveChanges();
Expand Down
18 changes: 12 additions & 6 deletions sample/Detached.Samples.RestApi/Stores/InvoiceStore.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Detached.Mappers.Samples.RestApi.Models;
using Detached.Mappers.Samples.RestApi.Models.Inputs;
using Detached.Mappers.Samples.RestApi.Models.Outputs;
using Detached.Mappers.EntityFramework.Extensions;
using Detached.Samples.RestApi.Models;
using Detached.Samples.RestApi.Models.Inputs;
using Detached.Samples.RestApi.Models.Outputs;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using Detached.Mappers.EntityFramework;

namespace Detached.Mappers.Samples.RestApi.Stores
namespace Detached.Samples.RestApi.Stores
{
public class InvoiceStore
{
Expand All @@ -22,7 +22,13 @@ public async Task<IEnumerable<InvoiceListItem>> GetAsync()
return await _context.Project<Invoice, InvoiceListItem>(_context.Invoices).ToListAsync();
}

public async Task SaveAsync(SaveInvoiceInput input)
public async Task SaveAsync(SaveInvoiceInput_Identity input)
{
await _context.MapAsync<Invoice>(input);
await _context.SaveChangesAsync();
}

public async Task SaveAsync(SaveInvoiceInput_Primitive input)
{
await _context.MapAsync<Invoice>(input);
await _context.SaveChangesAsync();
Expand Down
2 changes: 1 addition & 1 deletion sample/Detached.Samples.RestApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MainDb": "Server=.;Database=DetachedTest;Trusted_Connection=True;"
"MainDb": "Server=localhost\\SQLEXPRESS;Database=DetachedTest;Trusted_Connection=True;"
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 6d55e87

Please sign in to comment.