forked from fullstackproltd/AspNetCoreSpa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
90 changed files
with
1,894 additions
and
343 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
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
84 changes: 84 additions & 0 deletions
84
src/AspNetCoreSpa.Infrastructure/EfCoreMaterializerSource.cs
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,84 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
||
namespace AspNetCoreSpa.Infrastructure | ||
{ | ||
/// <summary> | ||
/// When creating any objects with data from the database, make sure any required transformations are made: | ||
/// - DateTime should have DateTimeKind.Utc set. | ||
/// </summary> | ||
/// <remarks> | ||
/// See https://volosoft.com/datetime-specifykind-in-entity-framework-core-while-querying/ | ||
/// See also https://github.com/aspnet/EntityFrameworkCore/issues/4711#issuecomment-358695190 for changes in EF core 2.1 | ||
/// </remarks> | ||
/// <example> | ||
/// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
/// { | ||
/// optionsBuilder.ReplaceService<IEntityMaterializerSource, EfCoreMaterializerSource>(); | ||
/// } | ||
/// </example> | ||
public class EfCoreMaterializerSource : EntityMaterializerSource | ||
{ | ||
/// <summary> | ||
/// The DateTime normalize method | ||
/// </summary> | ||
private static readonly MethodInfo UtcDateTimeNormalizeMethod = typeof(DateTimeMapper).GetTypeInfo().GetMethod(nameof(DateTimeMapper.Normalize)); | ||
|
||
/// <summary> | ||
/// The DateTime? normalize method | ||
/// </summary> | ||
private static readonly MethodInfo UtcDateTimeNormalizeNullableMethod = typeof(DateTimeMapper).GetTypeInfo().GetMethod(nameof(DateTimeMapper.NormalizeNullable)); | ||
|
||
/// <summary> | ||
/// This function is called whenever any value is read from the database and needs to be turned into an instance. | ||
/// In our case, we're only interested in DateTime fields. | ||
/// </summary> | ||
/// <remarks> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </remarks> | ||
public override Expression CreateReadValueExpression(Expression valueBuffer, Type type, int index, IPropertyBase property) | ||
{ | ||
if (type == typeof(DateTime)) | ||
{ | ||
return Expression.Call(UtcDateTimeNormalizeMethod, base.CreateReadValueExpression(valueBuffer, type, index, property)); | ||
} | ||
|
||
if (type == typeof(DateTime?)) | ||
{ | ||
return Expression.Call(UtcDateTimeNormalizeNullableMethod, base.CreateReadValueExpression(valueBuffer, type, index, property)); | ||
} | ||
|
||
return base.CreateReadValueExpression(valueBuffer, type, index, property); | ||
} | ||
|
||
/// <summary> | ||
/// Make sure any DateTime objects are set to UTC. | ||
/// </summary> | ||
private static class DateTimeMapper | ||
{ | ||
/// <summary> | ||
/// Normalizes the specified value. | ||
/// </summary> | ||
/// <param name="value">The value.</param> | ||
/// <returns>The DateTime with UTC kind specified.</returns> | ||
public static DateTime Normalize(DateTime value) | ||
{ | ||
return DateTime.SpecifyKind(value, DateTimeKind.Utc); | ||
} | ||
|
||
/// <summary> | ||
/// Normalizes the specified value. | ||
/// </summary> | ||
/// <param name="value">The value.</param> | ||
/// <returns>The DateTime? with UTC kind specified (or null).</returns> | ||
public static DateTime? NormalizeNullable(DateTime? value) | ||
{ | ||
return value.HasValue ? DateTime.SpecifyKind(value.Value, DateTimeKind.Utc) : (DateTime?)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 |
---|---|---|
|
@@ -18,20 +18,20 @@ | |
|
||
namespace AspNetCoreSpa.Infrastructure | ||
{ | ||
public interface IDatabaseInitializer | ||
public interface ISeedData | ||
{ | ||
void Initialise(); | ||
} | ||
|
||
public class DatabaseInitializer : IDatabaseInitializer | ||
public class SeedData : ISeedData | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
private readonly ILogger _logger; | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
|
||
public DatabaseInitializer( | ||
public SeedData( | ||
ApplicationDbContext context, | ||
ILogger<DatabaseInitializer> logger, | ||
ILogger<SeedData> logger, | ||
IHostingEnvironment hostingEnvironment | ||
) | ||
{ | ||
|
@@ -96,6 +96,7 @@ private void AddShopData() | |
{ | ||
Name = "John Doe " + i, | ||
Email = $"{i}[email protected]", | ||
DateOfBirth = DateTime.Now.AddDays(i), | ||
PhoneNumber = "0123456789" + i, | ||
Address = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. | ||
Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet", | ||
|
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
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
2 changes: 2 additions & 0 deletions
2
src/AspNetCoreSpa.Web/ClientApp/src/app/+examples/examples.routes.ts
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
6 changes: 4 additions & 2 deletions
6
....Web/ClientApp/src/app/+examples/examples/crud-shop/customers/customers.component.spec.ts
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
19 changes: 19 additions & 0 deletions
19
src/AspNetCoreSpa.Web/ClientApp/src/app/+examples/examples/crud-shop/pipes/gender.pipe.ts
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,19 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { Gender } from '../crud-shop.models'; | ||
|
||
@Pipe({ | ||
name: 'gender' | ||
}) | ||
|
||
export class GenderPipe implements PipeTransform { | ||
transform(value: any, ...args: any[]): any { | ||
switch (value) { | ||
case Gender.Male: | ||
return 'Male'; | ||
case Gender.Female: | ||
return 'Female'; | ||
default: | ||
return 'None'; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/AspNetCoreSpa.Web/ClientApp/src/app/+examples/examples/crud-shop/pipes/index.ts
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 @@ | ||
export * from './gender.pipe'; |
Oops, something went wrong.