Skip to content

Commit 93dc654

Browse files
committed
update: new entities for testing
1 parent b05e739 commit 93dc654

24 files changed

Lines changed: 942 additions & 229 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace QueryKit.WebApiTestProject.Database;
2+
3+
using Entities.Recipes;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
6+
7+
public sealed class RecipeConfiguration : IEntityTypeConfiguration<Recipe>
8+
{
9+
/// <summary>
10+
/// The database configuration for Recipes.
11+
/// </summary>
12+
public void Configure(EntityTypeBuilder<Recipe> builder)
13+
{
14+
// example for a simple 1:1 value object
15+
// builder.Property(x => x.Percent)
16+
// .HasConversion(x => x.Value, x => new Percent(x))
17+
// .HasColumnName("percent");
18+
19+
// example for a more complex value object
20+
// builder.OwnsOne(x => x.PhysicalAddress, opts =>
21+
// {
22+
// opts.Property(x => x.Line1).HasColumnName("physical_address_line1");
23+
// opts.Property(x => x.Line2).HasColumnName("physical_address_line2");
24+
// opts.Property(x => x.City).HasColumnName("physical_address_city");
25+
// opts.Property(x => x.State).HasColumnName("physical_address_state");
26+
// opts.Property(x => x.PostalCode).HasColumnName("physical_address_postal_code")
27+
// .HasConversion(x => x.Value, x => new PostalCode(x));
28+
// opts.Property(x => x.Country).HasColumnName("physical_address_country");
29+
// }).Navigation(x => x.PhysicalAddress)
30+
// .IsRequired();
31+
}
32+
}

QueryKit.WebApiTestProject/Database/TestingDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace QueryKit.WebApiTestProject.Database;
22

3+
using Entities.Recipes;
34
using Microsoft.EntityFrameworkCore;
45
using QueryKit.WebApiTestProject.Entities;
56

@@ -11,11 +12,13 @@ public TestingDbContext(DbContextOptions<TestingDbContext> options)
1112
}
1213

1314
public DbSet<TestingPerson> People { get; set; }
15+
public DbSet<Recipe> Recipes { get; set; }
1416

1517
protected override void OnModelCreating(ModelBuilder modelBuilder)
1618
{
1719
base.OnModelCreating(modelBuilder);
1820

1921
modelBuilder.ApplyConfiguration(new PersonConfiguration());
22+
modelBuilder.ApplyConfiguration(new RecipeConfiguration());
2023
}
2124
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace QueryKit.WebApiTestProject.Entities;
2+
3+
public class Address : ValueObject
4+
{
5+
public string Line1 { get; }
6+
public string Line2 { get; }
7+
public string City { get; }
8+
public string State { get; }
9+
public PostalCode PostalCode { get; }
10+
public string Country { get; }
11+
public Address(string line1, string line2, string city, string state, string postalCode, string country)
12+
: this(line1, line2, city, state, PostalCode.Of(postalCode), country)
13+
{
14+
}
15+
16+
public Address(string line1, string line2, string city, string state, PostalCode postalCode, string country)
17+
{
18+
Line1 = line1;
19+
Line2 = line2;
20+
City = city;
21+
State = state;
22+
PostalCode = postalCode;
23+
Country = country;
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace QueryKit.WebApiTestProject.Entities.Authors;
2+
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Runtime.Serialization;
5+
using System.Text.Json.Serialization;
6+
using Models;
7+
using Recipes;
8+
9+
public class Author : BaseEntity
10+
{
11+
public string Name { get; private set; }
12+
13+
[JsonIgnore, IgnoreDataMember]
14+
[ForeignKey("Recipe")]
15+
public Guid RecipeId { get; private set; }
16+
public Recipe Recipe { get; private set; }
17+
18+
19+
public static Author Create(AuthorForCreation authorForCreation)
20+
{
21+
var newAuthor = new Author();
22+
23+
newAuthor.Name = authorForCreation.Name;
24+
newAuthor.RecipeId = authorForCreation.RecipeId;
25+
26+
return newAuthor;
27+
}
28+
29+
public Author Update(AuthorForUpdate authorForUpdate)
30+
{
31+
Name = authorForUpdate.Name;
32+
RecipeId = authorForUpdate.RecipeId;
33+
return this;
34+
}
35+
36+
protected Author() { } // For EF + Mocking
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QueryKit.WebApiTestProject.Entities.Authors.Models;
2+
3+
public sealed class AuthorForCreation
4+
{
5+
public string Name { get; set; }
6+
public Guid RecipeId { get; set; }
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QueryKit.WebApiTestProject.Entities.Authors.Models;
2+
3+
public sealed class AuthorForUpdate
4+
{
5+
public string Name { get; set; }
6+
public Guid RecipeId { get; set; }
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace QueryKit.WebApiTestProject.Entities;
2+
3+
using System.ComponentModel.DataAnnotations;
4+
5+
public abstract class BaseEntity
6+
{
7+
[Key]
8+
public Guid Id { get; private set; } = Guid.NewGuid();
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace QueryKit.WebApiTestProject.Entities;
2+
3+
public class EmailAddress : ValueObject
4+
{
5+
public EmailAddress(string? value)
6+
{
7+
Value = value;
8+
}
9+
10+
public string? Value { get; private set; }
11+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace QueryKit.WebApiTestProject.Entities.Ingredients;
2+
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Runtime.Serialization;
5+
using System.Text.Json.Serialization;
6+
using Models;
7+
using Recipes;
8+
9+
public class Ingredient : BaseEntity
10+
{
11+
public string Name { get; private set; }
12+
13+
public string Quantity { get; private set; }
14+
15+
public DateTime? ExpiresOn { get; private set; }
16+
17+
public string Measure { get; private set; }
18+
19+
[JsonIgnore, IgnoreDataMember]
20+
[ForeignKey("Recipe")]
21+
public Guid RecipeId { get; private set; }
22+
public Recipe Recipe { get; private set; }
23+
24+
25+
public static Ingredient Create(IngredientForCreation ingredientForCreation)
26+
{
27+
var newIngredient = new Ingredient();
28+
29+
newIngredient.Name = ingredientForCreation.Name;
30+
newIngredient.Quantity = ingredientForCreation.Quantity;
31+
newIngredient.ExpiresOn = ingredientForCreation.ExpiresOn;
32+
newIngredient.Measure = ingredientForCreation.Measure;
33+
newIngredient.RecipeId = ingredientForCreation.RecipeId;
34+
return newIngredient;
35+
}
36+
37+
public Ingredient Update(IngredientForUpdate ingredientForUpdate)
38+
{
39+
Name = ingredientForUpdate.Name;
40+
Quantity = ingredientForUpdate.Quantity;
41+
ExpiresOn = ingredientForUpdate.ExpiresOn;
42+
Measure = ingredientForUpdate.Measure;
43+
RecipeId = ingredientForUpdate.RecipeId;
44+
return this;
45+
}
46+
47+
protected Ingredient() { } // For EF + Mocking
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace QueryKit.WebApiTestProject.Entities.Ingredients.Models;
2+
3+
public sealed class IngredientForCreation
4+
{
5+
public string Name { get; set; }
6+
public string Quantity { get; set; }
7+
public DateTime? ExpiresOn { get; set; }
8+
public string Measure { get; set; }
9+
public Guid RecipeId { get; set; }
10+
}

0 commit comments

Comments
 (0)