Commit 174c88e0 authored by Andres Käver's avatar Andres Käver
Browse files

langstr full crud in admin/GpsSessionType

parent 2e314c36
Pipeline #826 passed with stages
in 1 minute and 30 seconds
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -16,7 +17,7 @@ namespace DAL.App.EF.Repositories ...@@ -16,7 +17,7 @@ namespace DAL.App.EF.Repositories
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>, EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>,
IGpsSessionTypeRepository IGpsSessionTypeRepository
{ {
public GpsSessionTypeRepository(AppDbContext repoDbContext) : base(repoDbContext, new DALMapper<Domain.App.GpsSessionType, DTO.GpsSessionType>()) public GpsSessionTypeRepository(AppDbContext repoDbContext) : base(repoDbContext, new DALMapper<Domain.App.GpsSessionType, DTO.GpsSessionType>())
{ {
} }
...@@ -33,5 +34,39 @@ namespace DAL.App.EF.Repositories ...@@ -33,5 +34,39 @@ namespace DAL.App.EF.Repositories
var result = domainEntities.Select(e => Mapper.Map(e)); var result = domainEntities.Select(e => Mapper.Map(e));
return result; return result;
} }
public override async Task<GpsSessionType> FirstOrDefaultAsync(Guid id, object? userId = null, bool noTracking = true)
{
var query = PrepareQuery(userId, noTracking);
var domainEntity = await query
.Include(l => l.Name)
.ThenInclude(t => t!.Translations)
.Include(l => l.Description)
.ThenInclude(t => t!.Translations)
.FirstOrDefaultAsync(e => e.Id.Equals(id));
var result = Mapper.Map(domainEntity);
return result;
}
public override async Task<GpsSessionType> UpdateAsync(GpsSessionType entity, object? userId = null)
{
var domainEntity = Mapper.Map(entity);
// fix the language string - from mapper we get new ones - so duplicate values will be created in db
// load back from db the originals
domainEntity.Name = await RepoDbContext.LangStrs.Include(t => t.Translations).FirstAsync(s => s.Id == domainEntity.NameId);
domainEntity.Name.SetTranslation(entity.Name);
domainEntity.Description = await RepoDbContext.LangStrs.Include(t => t.Translations).FirstAsync(s => s.Id == domainEntity.DescriptionId);
domainEntity.Description.SetTranslation(entity.Description);
await CheckDomainEntityOwnership(domainEntity, userId);
var trackedDomainEntity = RepoDbSet.Update(domainEntity).Entity;
var result = Mapper.Map(trackedDomainEntity);
return result; }
} }
} }
\ No newline at end of file
...@@ -31,7 +31,7 @@ namespace DAL.Base.Mappers ...@@ -31,7 +31,7 @@ namespace DAL.Base.Mappers
return Mapper.Map<TLeftObject, TRightObject>(inObject); return Mapper.Map<TLeftObject, TRightObject>(inObject);
} }
public TLeftObject Map(TRightObject inObject) public virtual TLeftObject Map(TRightObject inObject)
{ {
return Mapper.Map<TRightObject, TLeftObject>(inObject); return Mapper.Map<TRightObject, TLeftObject>(inObject);
} }
......
...@@ -14,7 +14,6 @@ namespace Domain.App ...@@ -14,7 +14,6 @@ namespace Domain.App
public ICollection<LangStrTranslation>? Translations { get; set; } public ICollection<LangStrTranslation>? Translations { get; set; }
[InverseProperty(nameof(GpsSessionType.Name))] [InverseProperty(nameof(GpsSessionType.Name))]
public ICollection<GpsSessionType>? GpsSessionTypeNames { get; set; } public ICollection<GpsSessionType>? GpsSessionTypeNames { get; set; }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Contracts.BLL.App;
using DAL.App.EF; using DAL.App.EF;
using Domain.App; using Domain.App;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
...@@ -15,22 +16,26 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -15,22 +16,26 @@ namespace WebApp.Areas.Admin.Controllers
[Authorize(Roles = "admin")] [Authorize(Roles = "admin")]
public class GpsSessionTypesController : Controller public class GpsSessionTypesController : Controller
{ {
private readonly AppDbContext _context; private readonly IAppBLL _bll;
public GpsSessionTypesController(AppDbContext context) public GpsSessionTypesController(IAppBLL bll)
{ {
_context = context; _bll = bll;
} }
// GET: GpsSessionTypes // GET: GpsSessionTypes
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var res = await _bll.GpsSessionTypes.GetAllAsync();
/*
var appDbContext = _context.GpsSessionTypes var appDbContext = _context.GpsSessionTypes
.Include(g => g.Description) .Include(g => g.Description)
.ThenInclude(g => g!.Translations) .ThenInclude(g => g!.Translations)
.Include(g => g.Name) .Include(g => g.Name)
.ThenInclude(g => g!.Translations); .ThenInclude(g => g!.Translations);
return View(await appDbContext.ToListAsync()); */
return View(res);
} }
// GET: GpsSessionTypes/Details/5 // GET: GpsSessionTypes/Details/5
...@@ -41,12 +46,7 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -41,12 +46,7 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound(); return NotFound();
} }
var gpsSessionType = await _context.GpsSessionTypes var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
.Include(g => g.Description)
.ThenInclude(g => g!.Translations)
.Include(g => g.Name)
.ThenInclude(g => g!.Translations)
.FirstOrDefaultAsync(m => m.Id == id);
if (gpsSessionType == null) if (gpsSessionType == null)
{ {
return NotFound(); return NotFound();
...@@ -58,8 +58,6 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -58,8 +58,6 @@ namespace WebApp.Areas.Admin.Controllers
// GET: GpsSessionTypes/Create // GET: GpsSessionTypes/Create
public IActionResult Create() public IActionResult Create()
{ {
ViewData["DescriptionId"] = new SelectList(_context.LangStrs, "Id", "Id");
ViewData["NameId"] = new SelectList(_context.LangStrs, "Id", "Id");
return View(); return View();
} }
...@@ -68,17 +66,15 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -68,17 +66,15 @@ namespace WebApp.Areas.Admin.Controllers
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598. // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("NameId,DescriptionId,PaceMin,PaceMax,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")] GpsSessionType gpsSessionType) public async Task<IActionResult> Create(BLL.App.DTO.GpsSessionType gpsSessionType)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
gpsSessionType.Id = Guid.NewGuid(); _bll.GpsSessionTypes.Add(gpsSessionType);
_context.Add(gpsSessionType); await _bll.SaveChangesAsync();
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["DescriptionId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.DescriptionId);
ViewData["NameId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.NameId);
return View(gpsSessionType); return View(gpsSessionType);
} }
...@@ -90,13 +86,12 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -90,13 +86,12 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound(); return NotFound();
} }
var gpsSessionType = await _context.GpsSessionTypes.FindAsync(id); var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
if (gpsSessionType == null) if (gpsSessionType == null)
{ {
return NotFound(); return NotFound();
} }
ViewData["DescriptionId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.DescriptionId);
ViewData["NameId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.NameId);
return View(gpsSessionType); return View(gpsSessionType);
} }
...@@ -105,7 +100,7 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -105,7 +100,7 @@ namespace WebApp.Areas.Admin.Controllers
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598. // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("NameId,DescriptionId,PaceMin,PaceMax,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")] GpsSessionType gpsSessionType) public async Task<IActionResult> Edit(Guid id, BLL.App.DTO.GpsSessionType gpsSessionType)
{ {
if (id != gpsSessionType.Id) if (id != gpsSessionType.Id)
{ {
...@@ -114,26 +109,11 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -114,26 +109,11 @@ namespace WebApp.Areas.Admin.Controllers
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
try await _bll.GpsSessionTypes.UpdateAsync(gpsSessionType);
{ await _bll.SaveChangesAsync();
_context.Update(gpsSessionType);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!GpsSessionTypeExists(gpsSessionType.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["DescriptionId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.DescriptionId);
ViewData["NameId"] = new SelectList(_context.LangStrs, "Id", "Id", gpsSessionType.NameId);
return View(gpsSessionType); return View(gpsSessionType);
} }
...@@ -145,10 +125,8 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -145,10 +125,8 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound(); return NotFound();
} }
var gpsSessionType = await _context.GpsSessionTypes var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
.Include(g => g.Description)
.Include(g => g.Name)
.FirstOrDefaultAsync(m => m.Id == id);
if (gpsSessionType == null) if (gpsSessionType == null)
{ {
return NotFound(); return NotFound();
...@@ -162,15 +140,10 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -162,15 +140,10 @@ namespace WebApp.Areas.Admin.Controllers
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id) public async Task<IActionResult> DeleteConfirmed(Guid id)
{ {
var gpsSessionType = await _context.GpsSessionTypes.FindAsync(id); var gpsSessionType = await _bll.GpsSessionTypes.RemoveAsync(id);
_context.GpsSessionTypes.Remove(gpsSessionType); await _bll.SaveChangesAsync();
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
private bool GpsSessionTypeExists(Guid id)
{
return _context.GpsSessionTypes.Any(e => e.Id == id);
}
} }
} }
\ No newline at end of file
...@@ -25,7 +25,7 @@ namespace WebApp.Areas.Admin.Controllers ...@@ -25,7 +25,7 @@ namespace WebApp.Areas.Admin.Controllers
// GET: LangStrTranslations // GET: LangStrTranslations
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var appDbContext = _context.LangStrTranslation.Include(l => l.LangStr); var appDbContext = _context.LangStrTranslation.Include(l => l.LangStr).OrderBy(l => l.LangStrId);
return View(await appDbContext.ToListAsync()); return View(await appDbContext.ToListAsync());
} }
......
@model Domain.App.GpsSessionType @model BLL.App.DTO.GpsSessionType
@{ @{
ViewData["Title"] = "Create"; ViewData["Title"] = "Create";
...@@ -7,51 +7,34 @@ ...@@ -7,51 +7,34 @@
<h1>Create</h1> <h1>Create</h1>
<h4>GpsSessionType</h4> <h4>GpsSessionType</h4>
<hr /> <hr/>
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<form asp-action="Create"> <form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div> <div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group"> <div class="form-group">
<label asp-for="NameId" class="control-label"></label> <label asp-for="Name" class="control-label"></label>
<select asp-for="NameId" class ="form-control" asp-items="ViewBag.NameId"></select> <input asp-for="Name" class="form-control"/>
<span asp-validation-for="Name" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="DescriptionId" class="control-label"></label> <label asp-for="Description" class="control-label"></label>
<select asp-for="DescriptionId" class ="form-control" asp-items="ViewBag.DescriptionId"></select> <input asp-for="Description" class="form-control"/>
<span asp-validation-for="Description" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="PaceMin" class="control-label"></label> <label asp-for="PaceMin" class="control-label"></label>
<input asp-for="PaceMin" class="form-control" /> <input asp-for="PaceMin" class="form-control"/>
<span asp-validation-for="PaceMin" class="text-danger"></span> <span asp-validation-for="PaceMin" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="PaceMax" class="control-label"></label> <label asp-for="PaceMax" class="control-label"></label>
<input asp-for="PaceMax" class="form-control" /> <input asp-for="PaceMax" class="form-control"/>
<span asp-validation-for="PaceMax" class="text-danger"></span> <span asp-validation-for="PaceMax" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label> <input type="submit" value="Create" class="btn btn-primary"/>
<input asp-for="CreatedBy" class="form-control" />
<span asp-validation-for="CreatedBy" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CreatedAt" class="control-label"></label>
<input asp-for="CreatedAt" class="form-control" />
<span asp-validation-for="CreatedAt" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ChangedBy" class="control-label"></label>
<input asp-for="ChangedBy" class="form-control" />
<span asp-validation-for="ChangedBy" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ChangedAt" class="control-label"></label>
<input asp-for="ChangedAt" class="form-control" />
<span asp-validation-for="ChangedAt" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div> </div>
</form> </form>
</div> </div>
...@@ -62,5 +45,5 @@ ...@@ -62,5 +45,5 @@
</div> </div>
@section Scripts { @section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");} @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
} }
\ No newline at end of file
@model Domain.App.GpsSessionType @model BLL.App.DTO.GpsSessionType
@{ @{
ViewData["Title"] = "Delete"; ViewData["Title"] = "Delete";
...@@ -15,13 +15,13 @@ ...@@ -15,13 +15,13 @@
@Html.DisplayNameFor(model => model.Name) @Html.DisplayNameFor(model => model.Name)
</dt> </dt>
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name!.Id) @Html.DisplayFor(model => model.Name)
</dd> </dd>
<dt class = "col-sm-2"> <dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Description) @Html.DisplayNameFor(model => model.Description)
</dt> </dt>
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.Description!.Id) @Html.DisplayFor(model => model.Description)
</dd> </dd>
<dt class = "col-sm-2"> <dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PaceMin) @Html.DisplayNameFor(model => model.PaceMin)
...@@ -35,30 +35,6 @@ ...@@ -35,30 +35,6 @@
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.PaceMax) @Html.DisplayFor(model => model.PaceMax)
</dd> </dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CreatedBy)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CreatedBy)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CreatedAt)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CreatedAt)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.ChangedBy)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.ChangedBy)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.ChangedAt)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.ChangedAt)
</dd>
</dl> </dl>
<form asp-action="Delete"> <form asp-action="Delete">
......
@model Domain.App.GpsSessionType @model BLL.App.DTO.GpsSessionType
@{ @{
ViewData["Title"] = "Details"; ViewData["Title"] = "Details";
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
@Html.DisplayNameFor(model => model.Name) @Html.DisplayNameFor(model => model.Name)
</dt> </dt>
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name!.Id) @Html.DisplayFor(model => model.Name)
</dd> </dd>
<dt class = "col-sm-2"> <dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Description) @Html.DisplayNameFor(model => model.Description)
</dt> </dt>
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.Description!.Id) @Html.DisplayFor(model => model.Description)
</dd> </dd>
<dt class = "col-sm-2"> <dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PaceMin) @Html.DisplayNameFor(model => model.PaceMin)
...@@ -34,30 +34,6 @@ ...@@ -34,30 +34,6 @@
<dd class = "col-sm-10"> <dd class = "col-sm-10">
@Html.DisplayFor(model => model.PaceMax) @Html.DisplayFor(model => model.PaceMax)
</dd> </dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CreatedBy)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CreatedBy)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CreatedAt)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CreatedAt)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.ChangedBy)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.ChangedBy)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.ChangedAt)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.ChangedAt)
</dd>
</dl> </dl>
</div> </div>
<div> <div>
......
@model Domain.App.GpsSessionType @model BLL.App.DTO.GpsSessionType
@{ @{
ViewData["Title"] = "Edit"; ViewData["Title"] = "Edit";
...@@ -7,54 +7,42 @@ ...@@ -7,54 +7,42 @@
<h1>Edit</h1> <h1>Edit</h1>
<h4>GpsSessionType</h4> <h4>GpsSessionType</h4>
<hr /> <hr/>
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<form asp-action="Edit">