Binding Components to Sets function
This commit is contained in:
@@ -19,5 +19,7 @@ namespace YAPContracts.BindingModels
|
||||
[Required(ErrorMessage = "This field is required")]
|
||||
[Display(Name = "Component Type")]
|
||||
public string? ComponentType { get; set; }
|
||||
|
||||
public List<string>? ProductSetIds { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,17 @@ public class ComponentDataModel : IValidation
|
||||
public string Name { get; private set; }
|
||||
public ComponentType ComponentType { get; private set; }
|
||||
public bool IsDeleted { get; private set; }
|
||||
|
||||
// public List<ComponentInProductDataModel> Products = products;
|
||||
// public List<ComponentInProductSetDataModel> ProductSets = productSets;
|
||||
|
||||
public ComponentDataModel(string id, string name, ComponentType componentType, bool isDeleted)
|
||||
// public List<ComponentInProductDataModel> Products = products;
|
||||
public List<ComponentInProductSetDataModel> ProductSets { get; private set; }
|
||||
|
||||
public ComponentDataModel(string id, string name, ComponentType componentType, bool isDeleted, List<ComponentInProductSetDataModel> productSets)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
ComponentType = componentType;
|
||||
IsDeleted = isDeleted;
|
||||
ProductSets = productSets;
|
||||
}
|
||||
|
||||
public ComponentDataModel()
|
||||
@@ -34,6 +35,7 @@ public class ComponentDataModel : IValidation
|
||||
Name = string.Empty;
|
||||
ComponentType = ComponentType.None;
|
||||
IsDeleted = false;
|
||||
ProductSets = new List<ComponentInProductSetDataModel>();
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
|
||||
@@ -7,4 +7,6 @@ public class ComponentViewModel
|
||||
public string Id { get; set; } = default!;
|
||||
public string Name { get; set; } = default!;
|
||||
public ComponentType ComponentType { get; set; }
|
||||
|
||||
public List<string>? ProductSetIds { get; set; }
|
||||
}
|
||||
@@ -42,7 +42,8 @@ internal class ComponentStorageContract : IComponentStorageContract
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Components.Add(_mapper.Map<Component>(component));
|
||||
var entity = _mapper.Map<Component>(component);
|
||||
_dbContext.Components.Add(entity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -89,7 +90,7 @@ internal class ComponentStorageContract : IComponentStorageContract
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ComponentDataModel>(_dbContext.Components.FirstOrDefault(x => x.Name == name));
|
||||
return _mapper.Map<ComponentDataModel>(_dbContext.Components.Include(x => x.ProductSets).FirstOrDefault(x => x.Name == name));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -132,7 +133,7 @@ internal class ComponentStorageContract : IComponentStorageContract
|
||||
}
|
||||
}
|
||||
|
||||
private Component? GetComponentById(string id) => _dbContext.Components.FirstOrDefault(x => x.Id == id);
|
||||
private Component? GetComponentById(string id) => _dbContext.Components.Include(x => x.ProductSets).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public List<ComponentReportModel>? GetDataForReport(DateTime start, DateTime finish)
|
||||
{
|
||||
|
||||
@@ -20,8 +20,25 @@ namespace YAPWebApplication.Adapters
|
||||
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<ComponentBindingModel, ComponentDataModel>();
|
||||
cfg.CreateMap<ComponentDataModel, ComponentViewModel>();
|
||||
cfg.CreateMap<ComponentBindingModel, ComponentDataModel>()
|
||||
.ForMember(dest => dest.ProductSets,
|
||||
opt => opt.MapFrom(src =>
|
||||
(src.ProductSetIds ?? new List<string>())
|
||||
.Select(psId => new ComponentInProductSetDataModel(
|
||||
src.Id ?? Guid.NewGuid().ToString(),
|
||||
psId,
|
||||
1
|
||||
)).ToList()
|
||||
)
|
||||
);
|
||||
|
||||
cfg.CreateMap<ComponentDataModel, ComponentViewModel>()
|
||||
.ForMember(dest => dest.ProductSetIds,
|
||||
opt => opt.MapFrom(src =>
|
||||
src.ProductSets.Select(ps => ps.ProductSetId).ToList()
|
||||
)
|
||||
);
|
||||
|
||||
cfg.CreateMap<ComponentViewModel, ComponentDataModel>();
|
||||
});
|
||||
|
||||
|
||||
@@ -24,6 +24,20 @@
|
||||
asp-items="Model.ComponentTypeList"></select>
|
||||
<span asp-validation-for="Component.ComponentType" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="ProductSets">Add to Product Set</label>
|
||||
@foreach (var set in Model.ProductSetsList)
|
||||
{
|
||||
<div class="form-check">
|
||||
<input class="form-check-input"
|
||||
type="checkbox"
|
||||
name="Component.ProductSetIds"
|
||||
value="@set.Value"
|
||||
@(set.Selected ? "checked" : "") />
|
||||
<label class="form-check-label">@set.Text</label>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="submit" value="Create" class="btn btn-primary" />
|
||||
</div>
|
||||
|
||||
@@ -18,20 +18,23 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
public class CreateModel : PageModel
|
||||
{
|
||||
private readonly IComponentAdapter _componentAdapter;
|
||||
private readonly IProductSetAdapter _productSetAdapter;
|
||||
|
||||
public CreateModel(IComponentAdapter componentAdapter)
|
||||
public CreateModel(IComponentAdapter componentAdapter, IProductSetAdapter productSetAdapter)
|
||||
{
|
||||
_componentAdapter = componentAdapter;
|
||||
_productSetAdapter = productSetAdapter;
|
||||
}
|
||||
|
||||
public IEnumerable<SelectListItem>? ComponentTypeList { get; set; }
|
||||
public IEnumerable<SelectListItem>? ProductSetsList { get; set; }
|
||||
|
||||
[BindProperty]
|
||||
public ComponentBindingModel Component { get; set; } = new();
|
||||
|
||||
public IActionResult OnGet()
|
||||
{
|
||||
LoadComponentTypeList();
|
||||
LoadLists();
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -39,7 +42,7 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
LoadComponentTypeList();
|
||||
LoadLists();
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -52,12 +55,12 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
catch (Exception ex)
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, $"Ошибка: {ex.Message}");
|
||||
LoadComponentTypeList();
|
||||
LoadLists();
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadComponentTypeList()
|
||||
private void LoadLists()
|
||||
{
|
||||
ComponentTypeList = Enum.GetValues(typeof(ComponentType))
|
||||
.Cast<ComponentType>()
|
||||
@@ -66,6 +69,12 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
Value = ((int)ct).ToString(),
|
||||
Text = ct.ToString()
|
||||
});
|
||||
var prodSets = _productSetAdapter.GetList();
|
||||
ProductSetsList = prodSets?.Select(c => new SelectListItem
|
||||
{
|
||||
Value = c.Id,
|
||||
Text = $"{c.SetName}"
|
||||
}).ToList() ?? new List<SelectListItem>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,24 @@
|
||||
<dd class="col-sm-10">
|
||||
@Html.DisplayFor(model => model.Component.ComponentType)
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
Product Sets
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
@if (Model.ProductSets != null && Model.ProductSets.Any())
|
||||
{
|
||||
<ul>
|
||||
@foreach (var comp in Model.ProductSets)
|
||||
{
|
||||
<li>@comp.SetName</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Does not appear in Product Sets</span>
|
||||
}
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
@@ -14,25 +14,36 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
{
|
||||
internal class DetailsModel : PageModel
|
||||
{
|
||||
private readonly IComponentAdapter _adapter;
|
||||
private readonly IComponentAdapter _componentAdapter;
|
||||
private readonly IProductSetAdapter _productSetAdapter;
|
||||
|
||||
public DetailsModel(IComponentAdapter adapter)
|
||||
public DetailsModel(IComponentAdapter adapter, IProductSetAdapter productSetAdapter)
|
||||
{
|
||||
_adapter = adapter;
|
||||
_componentAdapter = adapter;
|
||||
_productSetAdapter = productSetAdapter;
|
||||
}
|
||||
|
||||
public ComponentViewModel? Component { get; set; }
|
||||
public List<ProductSetViewModel>? ProductSets { get; set; }
|
||||
|
||||
public IActionResult OnGet(string id)
|
||||
{
|
||||
if (id == null)
|
||||
return NotFound();
|
||||
|
||||
Component = _adapter.GetComponentByData(id);
|
||||
Component = _componentAdapter.GetComponentByData(id);
|
||||
|
||||
if (Component == null)
|
||||
return NotFound();
|
||||
|
||||
var allSets = _productSetAdapter.GetList();
|
||||
if (allSets != null && Component.ProductSetIds.Any())
|
||||
{
|
||||
ProductSets = allSets
|
||||
.Where(c => Component.ProductSetIds.Contains(c.Id))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,20 @@
|
||||
asp-items="Model.ComponentTypeList"></select>
|
||||
<span asp-validation-for="Component.ComponentType" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Product Sets</label>
|
||||
@foreach (var comp in Model.ProductSetSelectList)
|
||||
{
|
||||
<div class="form-check">
|
||||
<input class="form-check-input"
|
||||
type="checkbox"
|
||||
name="Component.ProductSetIds"
|
||||
value="@comp.Value"
|
||||
@(comp.Selected ? "checked" : "") />
|
||||
<label class="form-check-label">@comp.Text</label>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="submit" value="Save" class="btn btn-primary" />
|
||||
</div>
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using YAPContracts.AdapterContracts;
|
||||
using YAPContracts.BindingModels;
|
||||
using YAPContracts.Enums;
|
||||
using YAPDatabase;
|
||||
using YAPDatabase.Models;
|
||||
using YAPWebApplication.Adapters;
|
||||
|
||||
namespace YAPWebApplication.Pages.Views.Components
|
||||
{
|
||||
[Authorize(Roles = "Storekeeper")]
|
||||
internal class EditModel : PageModel
|
||||
{
|
||||
private readonly IComponentAdapter _adapter;
|
||||
private readonly IComponentAdapter _componentAdapter;
|
||||
private readonly IProductSetAdapter _productSetAdapter;
|
||||
|
||||
public EditModel(IComponentAdapter adapter)
|
||||
public EditModel(IComponentAdapter adapter, IProductSetAdapter productSetAdapter)
|
||||
{
|
||||
_adapter = adapter;
|
||||
_componentAdapter = adapter;
|
||||
_productSetAdapter = productSetAdapter;
|
||||
}
|
||||
public IEnumerable<SelectListItem>? ComponentTypeList { get; set; }
|
||||
public List<SelectListItem> ProductSetSelectList { get; set; } = new();
|
||||
|
||||
|
||||
[BindProperty]
|
||||
@@ -35,7 +39,7 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
if (id == null)
|
||||
return NotFound();
|
||||
|
||||
var component = _adapter.GetComponentByData(id);
|
||||
var component = _componentAdapter.GetComponentByData(id);
|
||||
if (component == null)
|
||||
return NotFound();
|
||||
LoadComponentTypeList();
|
||||
@@ -45,7 +49,15 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
Id = component.Id,
|
||||
Name = component.Name,
|
||||
ComponentType = component.ComponentType.ToString(),
|
||||
ProductSetIds = component.ProductSetIds,
|
||||
};
|
||||
var prodSets = _productSetAdapter.GetList();
|
||||
ProductSetSelectList = prodSets?.Select(c => new SelectListItem
|
||||
{
|
||||
Value = c.Id,
|
||||
Text = $"{c.SetName}",
|
||||
Selected = Component.ProductSetIds.Contains(c.Id)
|
||||
}).ToList() ?? new List<SelectListItem>();
|
||||
|
||||
return Page();
|
||||
}
|
||||
@@ -55,17 +67,19 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
LoadComponentTypeList();
|
||||
LoadSetsList();
|
||||
return Page();
|
||||
}
|
||||
try
|
||||
{
|
||||
_adapter.Update(Component);
|
||||
_componentAdapter.Update(Component);
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, $"Ошибка: {ex.Message}");
|
||||
LoadComponentTypeList();
|
||||
LoadSetsList();
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
@@ -80,6 +94,21 @@ namespace YAPWebApplication.Pages.Views.Components
|
||||
Text = ct.ToString()
|
||||
});
|
||||
}
|
||||
private void LoadSetsList()
|
||||
{
|
||||
var prodSets = _productSetAdapter.GetList();
|
||||
if (Component.ProductSetIds == null)
|
||||
{
|
||||
ProductSetSelectList = new List<SelectListItem>();
|
||||
return;
|
||||
}
|
||||
ProductSetSelectList = prodSets?.Select(c => new SelectListItem
|
||||
{
|
||||
Value = c.Id,
|
||||
Text = $"{c.SetName}",
|
||||
Selected = Component.ProductSetIds.Contains(c.Id)
|
||||
}).ToList() ?? new List<SelectListItem>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,6 @@ namespace YAPWebApplication.Pages.Views.ProductSets
|
||||
SetName = productset.SetName,
|
||||
TotalPrice = productset.TotalPrice,
|
||||
ComponentIds = productset.ComponentIds?.ToList() ?? new List<string>(),
|
||||
// COMMENTS!!!
|
||||
};
|
||||
// список для чекбоксов
|
||||
var components = _componentAdapter.GetList(true);
|
||||
|
||||
Reference in New Issue
Block a user