Skip to content

Instantly share code, notes, and snippets.

View DmitrySikorsky's full-sized avatar

Dmitry Sikorsky DmitrySikorsky

View GitHub Profile
protected IEnumerable<ViewModels.Shared.Localization> CreateLocalizationsFor(LocalizationSet localizationSet)
{
ICultureRepository cultureRepository = this.HttpContext.RequestServices.GetService<ICultureRepository>();
return cultureRepository.GetAll()
.Select(c => new ViewModels.Shared.Localization() { CultureCode = c.Code, Value = localizationSet.Localizations.FirstOrDefault(l => l.CultureCode == c.Code)?.Value });
}
public Book GetById(int id)
{
return this.storage.Books
.Include(b => b.Name).ThenInclude(ls => ls.Localizations)
.Include(b => b.Description).ThenInclude(ls => ls.Localizations)
.Include(b => b.Author).ThenInclude(ls => ls.Localizations)
.FirstOrDefault(b => b.Id == id);
}
protected IEnumerable<ViewModels.Shared.Localization> CreateEmptyLocalizations()
{
ICultureRepository cultureRepository = this.HttpContext.RequestServices.GetService<ICultureRepository>();
return cultureRepository.GetAll().Select(c => new ViewModels.Shared.Localization() { CultureCode = c.Code });
}
[HttpGet]
[ImportModelStateFromTempData]
public IActionResult AddOrEdit(int? id)
{
AddOrEdit addOrEdit = new AddOrEdit();
if (id == null)
{
addOrEdit.NameLocalizations = this.CreateEmptyLocalizations();
addOrEdit.DescriptionLocalizations = this.CreateEmptyLocalizations();
public class Localization : ViewModelBase
{
public string CultureCode { get; set; }
public string Value { get; set; }
}
public class LocalizableAttribute : Attribute
{
}
public class AddOrEdit : ViewModelBase
{
public int? Id { get; set; }
[Localizable]
[Display(Name = "Name")]
[Required]
[StringLength(64)]
public string Name { get; set; }
public IEnumerable<Localization> NameLocalizations { get; set; }
<table class="content__table table" cellpadding="0" cellspacing="0">
<tr class="table__row">
<th class="table__cell table__cell--heading">@Localizer["ID"]</th>
<th class="table__cell table__cell--heading">@Localizer["Name"]</th>
<th class="table__cell table__cell--heading">@Localizer["Description"]</th>
<th class="table__cell table__cell--heading">@Localizer["Author"]</th>
<th class="table__cell table__cell--heading">@Localizer["Year"]</th>
<th class="table__cell table__cell--heading">&nbsp</th>
</tr>
@foreach (var book in this.Model)
public IActionResult Index()
{
return this.View(
localizedBookRepository.GetAll(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
.Select(b => new Book() { Id = b.Id, Name = b.Name, Description = b.Description, Author = b.Author, Year = b.Year })
);
}
SELECT "b"."Id", "lName"."Value" AS "Name", "lDescription"."Value" AS "Description", "lAuthor"."Value" AS "Author", "b"."Year"
FROM "Books" AS "b"
INNER JOIN "Localizations" AS "lName" ON "b"."NameId" = "lName"."LocalizationSetId"
INNER JOIN "Localizations" AS "lDescription" ON "b"."DescriptionId" = "lDescription"."LocalizationSetId"
INNER JOIN "Localizations" AS "lAuthor" ON "b"."AuthorId" = "lAuthor"."LocalizationSetId"
WHERE (("lName"."CultureCode" = 'en') AND ("lDescription"."CultureCode" = 'en')) AND ("lAuthor"."CultureCode" = 'en')