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.Linq;
using System.Threading.Tasks;
......@@ -16,7 +17,7 @@ namespace DAL.App.EF.Repositories
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>,
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
var result = domainEntities.Select(e => Mapper.Map(e));
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
return Mapper.Map<TLeftObject, TRightObject>(inObject);
}
public TLeftObject Map(TRightObject inObject)
public virtual TLeftObject Map(TRightObject inObject)
{
return Mapper.Map<TRightObject, TLeftObject>(inObject);
}
......
......@@ -14,7 +14,6 @@ namespace Domain.App
public ICollection<LangStrTranslation>? Translations { get; set; }
[InverseProperty(nameof(GpsSessionType.Name))]
public ICollection<GpsSessionType>? GpsSessionTypeNames { get; set; }
......
......@@ -2,6 +2,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Contracts.BLL.App;
using DAL.App.EF;
using Domain.App;
using Microsoft.AspNetCore.Authorization;
......@@ -15,22 +16,26 @@ namespace WebApp.Areas.Admin.Controllers
[Authorize(Roles = "admin")]
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
public async Task<IActionResult> Index()
{
var res = await _bll.GpsSessionTypes.GetAllAsync();
/*
var appDbContext = _context.GpsSessionTypes
.Include(g => g.Description)
.ThenInclude(g => g!.Translations)
.Include(g => g.Name)
.ThenInclude(g => g!.Translations);
return View(await appDbContext.ToListAsync());
*/
return View(res);
}
// GET: GpsSessionTypes/Details/5
......@@ -41,12 +46,7 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound();
}
var gpsSessionType = await _context.GpsSessionTypes
.Include(g => g.Description)
.ThenInclude(g => g!.Translations)
.Include(g => g.Name)
.ThenInclude(g => g!.Translations)
.FirstOrDefaultAsync(m => m.Id == id);
var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
if (gpsSessionType == null)
{
return NotFound();
......@@ -58,8 +58,6 @@ namespace WebApp.Areas.Admin.Controllers
// GET: GpsSessionTypes/Create
public IActionResult Create()
{
ViewData["DescriptionId"] = new SelectList(_context.LangStrs, "Id", "Id");
ViewData["NameId"] = new SelectList(_context.LangStrs, "Id", "Id");
return View();
}
......@@ -68,17 +66,15 @@ namespace WebApp.Areas.Admin.Controllers
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[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)
{
gpsSessionType.Id = Guid.NewGuid();
_context.Add(gpsSessionType);
await _context.SaveChangesAsync();
_bll.GpsSessionTypes.Add(gpsSessionType);
await _bll.SaveChangesAsync();
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);
}
......@@ -90,13 +86,12 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound();
}
var gpsSessionType = await _context.GpsSessionTypes.FindAsync(id);
var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
if (gpsSessionType == null)
{
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);
}
......@@ -105,7 +100,7 @@ namespace WebApp.Areas.Admin.Controllers
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[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)
{
......@@ -114,26 +109,11 @@ namespace WebApp.Areas.Admin.Controllers
if (ModelState.IsValid)
{
try
{
_context.Update(gpsSessionType);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!GpsSessionTypeExists(gpsSessionType.Id))
{
return NotFound();
}
else
{
throw;
}
}
await _bll.GpsSessionTypes.UpdateAsync(gpsSessionType);
await _bll.SaveChangesAsync();
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);
}
......@@ -145,10 +125,8 @@ namespace WebApp.Areas.Admin.Controllers
return NotFound();
}
var gpsSessionType = await _context.GpsSessionTypes
.Include(g => g.Description)
.Include(g => g.Name)
.FirstOrDefaultAsync(m => m.Id == id);
var gpsSessionType = await _bll.GpsSessionTypes.FirstOrDefaultAsync(id.Value);
if (gpsSessionType == null)
{
return NotFound();
......@@ -162,15 +140,10 @@ namespace WebApp.Areas.Admin.Controllers
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id)
{
var gpsSessionType = await _context.GpsSessionTypes.FindAsync(id);
_context.GpsSessionTypes.Remove(gpsSessionType);
await _context.SaveChangesAsync();
var gpsSessionType = await _bll.GpsSessionTypes.RemoveAsync(id);
await _bll.SaveChangesAsync();
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
// GET: LangStrTranslations
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());
}
......
@model Domain.App.GpsSessionType
@model BLL.App.DTO.GpsSessionType
@{
ViewData["Title"] = "Create";
......@@ -7,51 +7,34 @@
<h1>Create</h1>
<h4>GpsSessionType</h4>
<hr />
<hr/>
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="NameId" class="control-label"></label>
<select asp-for="NameId" class ="form-control" asp-items="ViewBag.NameId"></select>
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control"/>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DescriptionId" class="control-label"></label>
<select asp-for="DescriptionId" class ="form-control" asp-items="ViewBag.DescriptionId"></select>
<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="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>
</div>
<div class="form-group">
<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>
</div>
<div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label>
<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" />
<input type="submit" value="Create" class="btn btn-primary"/>
</div>
</form>
</div>
......@@ -62,5 +45,5 @@
</div>
@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";
......@@ -15,13 +15,13 @@
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name!.Id)
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Description!.Id)
@Html.DisplayFor(model => model.Description)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PaceMin)
......@@ -35,30 +35,6 @@
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PaceMax)
</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>
<form asp-action="Delete">
......
@model Domain.App.GpsSessionType
@model BLL.App.DTO.GpsSessionType
@{
ViewData["Title"] = "Details";
......@@ -14,13 +14,13 @@
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name!.Id)
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Description!.Id)
@Html.DisplayFor(model => model.Description)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.PaceMin)
......@@ -34,30 +34,6 @@
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PaceMax)
</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>
</div>
<div>
......
@model Domain.App.GpsSessionType
@model BLL.App.DTO.GpsSessionType
@{
ViewData["Title"] = "Edit";
......@@ -7,54 +7,42 @@
<h1>Edit</h1>
<h4>GpsSessionType</h4>
<hr />
<hr/>
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<input type="hidden" asp-for="Id"/>
<input type="hidden" asp-for="NameId"/>
<input type="hidden" asp-for="DescriptionId"/>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="NameId" class="control-label"></label>
<select asp-for="NameId" class="form-control" asp-items="ViewBag.NameId"></select>
<span asp-validation-for="NameId" class="text-danger"></span>
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control"/>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DescriptionId" class="control-label"></label>
<select asp-for="DescriptionId" class="form-control" asp-items="ViewBag.DescriptionId"></select>
<span asp-validation-for="DescriptionId" class="text-danger"></span>
<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="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>
</div>
<div class="form-group">
<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>
</div>
<div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label>
<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>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
<input type="submit" value="Save" class="btn btn-primary"/>
</div>
</form>
</div>
......@@ -65,5 +53,5 @@
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
\ No newline at end of file
@model IEnumerable<Domain.App.GpsSessionType>
@model IEnumerable<BLL.App.DTO.GpsSessionType>
@{
ViewData["Title"] = "Index";
......
......@@ -66,30 +66,7 @@
<label asp-for="GpsSessionTypeId" class="control-label"></label>
<select asp-for="GpsSessionTypeId" class ="form-control" asp-items="ViewBag.GpsSessionTypeId"></select>
</div>
<div class="form-group">
<label asp-for="AppUserId" class="control-label"></label>
<select asp-for="AppUserId" class ="form-control" asp-items="ViewBag.AppUserId"></select>
</div>
<div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label>
<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>
......
......@@ -17,13 +17,14 @@
@foreach (var item in Model)
{
<div class="col-sm-12 col-md-6 col-xl-4">
<div class="card">
<div class="card" style="margin-bottom: 12px;">
<div class="card-header">
@Html.DisplayFor(modelItem => item.Name)
</div>
<div class="card-body">
<h5 class="card-title">@Html.DisplayFor(modelItem => item.Description)</h5>
<p class="card-text">@Html.DisplayNameFor(model => model.GpsSessionType) - @(item.GpsSessionType!.Name!)</p>
<p class="card-text">@Html.DisplayNameFor(model => model.AppUser) - @(item.AppUser!.FirstLastName)</p>
<p class="card-text">@Html.DisplayNameFor(model => model.RecordedAt) - @Html.DisplayFor(model => item.RecordedAt)</p>
<p class="card-text">@Html.DisplayNameFor(model => model.Duration) - @Html.DisplayFor(model => item.Duration)</p>
<p class="card-text">@Html.DisplayNameFor(model => model.Distance) - @((item.Distance / 1000.0).ToString("N3"))</p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment