Commit b7925fc8 authored by Andres Käver's avatar Andres Käver

broken reflection based mapper for pavel hw

parent 1b73d6b7
......@@ -24,6 +24,9 @@ namespace BLL.App.DTO
public virtual int AnimalCount { get; set; }
public string Description { get; set; }
public string Biography { get; set; }
public virtual TKey AppUserId{ get; set; } = default!;
public virtual AppUser<TKey>? AppUser { get; set; }
}
......
......@@ -25,6 +25,9 @@ namespace DAL.App.DTO
public virtual TKey AppUserId{ get; set; } = default!;
public virtual AppUser<TKey>? AppUser { get; set; }
public string Description { get; set; }
public string Biography { get; set; }
}
public class OwnerDisplay
......@@ -33,6 +36,9 @@ namespace DAL.App.DTO
public virtual string FirstName { get; set; } = default!;
public virtual string LastName { get; set; } = default!;
public virtual int AnimalCount { get; set; }
public string Description { get; set; }
public string Biography { get; set; }
}
......
......@@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace DAL.App.EF.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20200417110724_InitialDbCreation")]
[Migration("20200417152334_InitialDbCreation")]
partial class InitialDbCreation
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
......@@ -161,6 +161,17 @@ namespace DAL.App.EF.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Domain.MultiLangString", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.HasKey("Id");
b.ToTable("MultiLangString");
});
modelBuilder.Entity("Domain.Owner", b =>
{
b.Property<Guid>("Id")
......@@ -170,6 +181,9 @@ namespace DAL.App.EF.Migrations
b.Property<Guid>("AppUserId")
.HasColumnType("uniqueidentifier");
b.Property<Guid>("BiographyId")
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("ChangedAt")
.HasColumnType("datetime2");
......@@ -182,6 +196,9 @@ namespace DAL.App.EF.Migrations
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<Guid>("DescriptionId")
.HasColumnType("uniqueidentifier");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("nvarchar(64)")
......@@ -196,6 +213,10 @@ namespace DAL.App.EF.Migrations
b.HasIndex("AppUserId");
b.HasIndex("BiographyId");
b.HasIndex("DescriptionId");
b.ToTable("Owners");
});
......@@ -240,6 +261,32 @@ namespace DAL.App.EF.Migrations
b.ToTable("OwnerAnimals");
});
modelBuilder.Entity("Domain.Translation", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Culture")
.IsRequired()
.HasColumnType("nvarchar(5)")
.HasMaxLength(5);
b.Property<Guid>("MultiLangStringId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("nvarchar(max)")
.HasMaxLength(10240);
b.HasKey("Id");
b.HasIndex("MultiLangStringId");
b.ToTable("Translation");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.Property<int>("Id")
......@@ -357,6 +404,18 @@ namespace DAL.App.EF.Migrations
.HasForeignKey("AppUserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("Domain.MultiLangString", "Biography")
.WithMany()
.HasForeignKey("BiographyId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("Domain.MultiLangString", "Description")
.WithMany()
.HasForeignKey("DescriptionId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Domain.OwnerAnimal", b =>
......@@ -380,6 +439,15 @@ namespace DAL.App.EF.Migrations
.IsRequired();
});
modelBuilder.Entity("Domain.Translation", b =>
{
b.HasOne("Domain.MultiLangString", "MultiLangString")
.WithMany("Translations")
.HasForeignKey("MultiLangStringId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.HasOne("Domain.Identity.AppRole", null)
......
......@@ -48,6 +48,17 @@ namespace DAL.App.EF.Migrations
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "MultiLangString",
columns: table => new
{
Id = table.Column<Guid>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_MultiLangString", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetRoleClaims",
columns: table => new
......@@ -189,6 +200,8 @@ namespace DAL.App.EF.Migrations
ChangedAt = table.Column<DateTime>(nullable: false),
FirstName = table.Column<string>(maxLength: 64, nullable: false),
LastName = table.Column<string>(maxLength: 64, nullable: false),
DescriptionId = table.Column<Guid>(nullable: false),
BiographyId = table.Column<Guid>(nullable: false),
AppUserId = table.Column<Guid>(nullable: false)
},
constraints: table =>
......@@ -200,6 +213,38 @@ namespace DAL.App.EF.Migrations
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Owners_MultiLangString_BiographyId",
column: x => x.BiographyId,
principalTable: "MultiLangString",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Owners_MultiLangString_DescriptionId",
column: x => x.DescriptionId,
principalTable: "MultiLangString",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "Translation",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Culture = table.Column<string>(maxLength: 5, nullable: false),
Value = table.Column<string>(maxLength: 10240, nullable: false),
MultiLangStringId = table.Column<Guid>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Translation", x => x.Id);
table.ForeignKey(
name: "FK_Translation_MultiLangString_MultiLangStringId",
column: x => x.MultiLangStringId,
principalTable: "MultiLangString",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
......@@ -302,6 +347,21 @@ namespace DAL.App.EF.Migrations
name: "IX_Owners_AppUserId",
table: "Owners",
column: "AppUserId");
migrationBuilder.CreateIndex(
name: "IX_Owners_BiographyId",
table: "Owners",
column: "BiographyId");
migrationBuilder.CreateIndex(
name: "IX_Owners_DescriptionId",
table: "Owners",
column: "DescriptionId");
migrationBuilder.CreateIndex(
name: "IX_Translation_MultiLangStringId",
table: "Translation",
column: "MultiLangStringId");
}
protected override void Down(MigrationBuilder migrationBuilder)
......@@ -324,6 +384,9 @@ namespace DAL.App.EF.Migrations
migrationBuilder.DropTable(
name: "OwnerAnimals");
migrationBuilder.DropTable(
name: "Translation");
migrationBuilder.DropTable(
name: "AspNetRoles");
......@@ -335,6 +398,9 @@ namespace DAL.App.EF.Migrations
migrationBuilder.DropTable(
name: "AspNetUsers");
migrationBuilder.DropTable(
name: "MultiLangString");
}
}
}
......@@ -159,6 +159,17 @@ namespace DAL.App.EF.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Domain.MultiLangString", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.HasKey("Id");
b.ToTable("MultiLangString");
});
modelBuilder.Entity("Domain.Owner", b =>
{
b.Property<Guid>("Id")
......@@ -168,6 +179,9 @@ namespace DAL.App.EF.Migrations
b.Property<Guid>("AppUserId")
.HasColumnType("uniqueidentifier");
b.Property<Guid>("BiographyId")
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("ChangedAt")
.HasColumnType("datetime2");
......@@ -180,6 +194,9 @@ namespace DAL.App.EF.Migrations
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<Guid>("DescriptionId")
.HasColumnType("uniqueidentifier");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("nvarchar(64)")
......@@ -194,6 +211,10 @@ namespace DAL.App.EF.Migrations
b.HasIndex("AppUserId");
b.HasIndex("BiographyId");
b.HasIndex("DescriptionId");
b.ToTable("Owners");
});
......@@ -238,6 +259,32 @@ namespace DAL.App.EF.Migrations
b.ToTable("OwnerAnimals");
});
modelBuilder.Entity("Domain.Translation", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Culture")
.IsRequired()
.HasColumnType("nvarchar(5)")
.HasMaxLength(5);
b.Property<Guid>("MultiLangStringId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("nvarchar(max)")
.HasMaxLength(10240);
b.HasKey("Id");
b.HasIndex("MultiLangStringId");
b.ToTable("Translation");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.Property<int>("Id")
......@@ -355,6 +402,18 @@ namespace DAL.App.EF.Migrations
.HasForeignKey("AppUserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("Domain.MultiLangString", "Biography")
.WithMany()
.HasForeignKey("BiographyId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("Domain.MultiLangString", "Description")
.WithMany()
.HasForeignKey("DescriptionId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Domain.OwnerAnimal", b =>
......@@ -378,6 +437,15 @@ namespace DAL.App.EF.Migrations
.IsRequired();
});
modelBuilder.Entity("Domain.Translation", b =>
{
b.HasOne("Domain.MultiLangString", "MultiLangString")
.WithMany("Translations")
.HasForeignKey("MultiLangStringId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.HasOne("Domain.Identity.AppRole", null)
......
using System;
using System.Collections;
using System.Linq;
using AutoMapper;
using Contracts.DAL.Base.Mappers;
......@@ -44,7 +46,38 @@ namespace DAL.Base.EF.Mappers
{
if (inProperties.TryGetValue(property.Name, out var value))
{
property.SetValue(result, value);
// is it a struct or a string
if (!property.PropertyType.IsClass && !property.PropertyType.IsInterface)
{
property.SetValue(result, value);
continue;
}
if (typeof(string).IsAssignableFrom(property.PropertyType))
{
property.SetValue(result, value);
continue;
}
if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
property
.SetValue(result,
Convert.ChangeType(
((IEnumerable) value).Cast<object>()
.Select(x => Convert.ChangeType((GetType()
.GetMethod(nameof(Map))
.MakeGenericMethod(property.PropertyType)
.Invoke(this, new[] {x})), property.PropertyType.GetGenericArguments()[0]))
.ToList(), property.PropertyType)
);
continue;
}
property.SetValue(result, GetType()
.GetMethod(nameof(Map))
.MakeGenericMethod(property.PropertyType)
.Invoke(this, new[] {value}));
}
}
......
......@@ -4,8 +4,13 @@ using Contracts.DAL.Base;
namespace DAL.Base
{
public abstract class DomainBaseEntity : IDomainBaseEntity
public abstract class DomainBaseEntity : DomainBaseEntity<Guid>, IDomainBaseEntity
{
public virtual Guid Id { get; set; }
}
public abstract class DomainBaseEntity<TKey> : IDomainBaseEntity<TKey>
where TKey : IEquatable<TKey>
{
public virtual TKey Id { get; set; } = default!;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Threading;
using Contracts.DAL.Base;
using DAL.Base;
namespace Domain
{
public class MultiLangString : MultiLangString<Guid>, IDomainBaseEntity
{
}
public class MultiLangString<TKey> : DomainBaseEntity<TKey>
where TKey : IEquatable<TKey>
{
private static string _defaultCulture = "en";
public virtual ICollection<Translation>? Translations { get; set; }
public MultiLangString()
{
}
public MultiLangString(string value) : this(value, Thread.CurrentThread.CurrentUICulture.Name)
{
}
public MultiLangString(string value, string culture)
{
SetTranslation(value, culture);
}
public void SetTranslation(string value)
{
SetTranslation(value, Thread.CurrentThread.CurrentUICulture.Name);
}
public void SetTranslation(string value, string culture)
{
if (Translations == null)
{
Translations = new List<Translation>();
}
var translation = Translations.FirstOrDefault(t => t.Culture == culture);
if (translation != null)
{
translation.Value = value;
}
else
{
Translations.Add(new Translation()
{
Value = value,
Culture = culture
});
}
}
public string? Translate(string? culture = null)
{
if (culture == null)
{
culture = Thread.CurrentThread.CurrentUICulture.Name;
}
var translation = Translations?
.FirstOrDefault(t => t.Culture == culture);
if (translation != null)
{
return translation.Value;
}
translation = Translations?.FirstOrDefault(t => culture.StartsWith(t.Culture));
if (translation != null)
{
return translation.Value;
}
translation = Translations?.FirstOrDefault(t => culture.StartsWith(_defaultCulture));
if (translation != null)
{
return translation.Value;
}
return Translations?.First().Value;
}
public override string ToString()
{
return Translate() ?? "?????";
}
// automatic casting to string when needed
public static implicit operator string(MultiLangString<TKey> m) => m.ToString();
// automatic casting from string when needed
public static implicit operator MultiLangString<TKey>(string s) => new MultiLangString<TKey>(s);
}
}
\ No newline at end of file
......@@ -27,6 +27,13 @@ namespace Domain
public virtual ICollection<OwnerAnimal>? Animals { get; set; }
public TKey DescriptionId { get; set; } = default!;
public MultiLangString? Description { get; set; }
public TKey BiographyId { get; set; } = default!;
public MultiLangString? Biography { get; set; }
public virtual TKey AppUserId{ get; set; } = default!;
public virtual TUser? AppUser { get; set; }
......
using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Security.Principal;
using Contracts.DAL.Base;
using DAL.Base;
namespace Domain
{
public class Translation : Translation<Guid>, IDomainBaseEntity
{
}
public class Translation<TKey> : DomainBaseEntity<TKey>
where TKey : IEquatable<TKey>
{
[MaxLength(5)] public string Culture { get; set; }
[MaxLength(10240)] public string Value { get; set; }
public TKey MultiLangStringId { get; set; }
public virtual MultiLangString? MultiLangString { get; set; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
......@@ -13,6 +15,9 @@ namespace WebApp
{
public static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
CreateHostBuilder(args).Build().Run();
}
......
......@@ -4,6 +4,7 @@ using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BLL.App;
using Contracts.BLL.App;
......
......@@ -18,6 +18,12 @@
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Biography)
</th>
<th></th>
</tr>
</thead>
......@@ -30,6 +36,12 @@
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Biography)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
......
......@@ -10,6 +10,17 @@
<input asp-for="LastName" class="form-control"/>
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control"/>
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Biography" class="control-label"></label>
<input asp-for="Biography" class="form-control"/>
<span asp-validation-for="Biography" class="text-danger"></span>
</div>
@*
<div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label>
......
......@@ -6,7 +6,7 @@
@{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();