refactor:

-Определил общий интерфейс для старницы создания и изменения телефона.
- Исправил валидацию названия телефона при редактировании модели.
parent 8fdac13f
...@@ -50,7 +50,7 @@ namespace PhoneStore.Controllers ...@@ -50,7 +50,7 @@ namespace PhoneStore.Controllers
{ {
order.UserId = int.Parse(_userManager.GetUserId(User)); order.UserId = int.Parse(_userManager.GetUserId(User));
order.User = null; order.User = null;
_orderService.Create(order.MapToOrderViewModel()); _orderService.Create(order.MapToOrder());
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
......
...@@ -15,8 +15,15 @@ namespace PhoneStore.Controllers ...@@ -15,8 +15,15 @@ namespace PhoneStore.Controllers
} }
[AcceptVerbs("GET", "POST")] [AcceptVerbs("GET", "POST")]
public bool CheckName(string name) public bool CheckName(string name, int? id)
{ {
if (id.HasValue)
{
return !_db.Phones
.AsEnumerable()
.Any(p => p.Name.Equals(name, StringComparison.CurrentCulture) && p.Id != id.Value);
}
return !_db.Phones return !_db.Phones
.AsEnumerable() .AsEnumerable()
.Any(p => p.Name.Equals(name, StringComparison.CurrentCulture)); .Any(p => p.Name.Equals(name, StringComparison.CurrentCulture));
......
...@@ -47,7 +47,7 @@ namespace PhoneStore.Controllers ...@@ -47,7 +47,7 @@ namespace PhoneStore.Controllers
[HttpGet] [HttpGet]
public IActionResult Create() public IActionResult Create()
{ {
PhoneCreateViewModel model = new PhoneCreateViewModel IPhoneEditable model = new PhoneCreateViewModel
{ {
Brands = _db.Brands.ToList() Brands = _db.Brands.ToList()
}; };
...@@ -111,7 +111,7 @@ namespace PhoneStore.Controllers ...@@ -111,7 +111,7 @@ namespace PhoneStore.Controllers
if (phone is null) if (phone is null)
return RedirectToAction("Error", "Errors", new {statusCode = 777}); return RedirectToAction("Error", "Errors", new {statusCode = 777});
PhoneCreateViewModel model = new PhoneCreateViewModel IPhoneEditable model = new PhoneEditViewModel
{ {
Id = phone.Id, Id = phone.Id,
Name = phone.Name, Name = phone.Name,
......
...@@ -20,8 +20,4 @@ ...@@ -20,8 +20,4 @@
<_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" /> <_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Pages" />
</ItemGroup>
</Project> </Project>
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Models;
namespace PhoneStore.ViewModels.PhoneViewModels
{
public interface IPhoneEditable
{
public int Id { get; set; }
[Required(ErrorMessage = "Поле обязательно для заполнения")]
[Remote("CheckName", "PhoneValidator", ErrorMessage = "Имя занято", AdditionalFields = nameof(Id))]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Минимальная длина 3 символа, максимальная - 50")]
public string Name { get; set; }
[Required(ErrorMessage = "Поле обязательно для заполнения")]
[Range(1000, 50000, ErrorMessage = "Значение не валидно, вводить можно знаение от 1000 до 5000")]
public decimal? Price { get; set; }
public int BrandId { get; set; }
public List<Brand> Brands { get; set; }
public IFormFile File { get; set; }
public string Image { get; set; }
}
}
\ No newline at end of file
...@@ -6,7 +6,7 @@ using PhoneStore.Models; ...@@ -6,7 +6,7 @@ using PhoneStore.Models;
namespace PhoneStore.ViewModels.PhoneViewModels namespace PhoneStore.ViewModels.PhoneViewModels
{ {
public class PhoneCreateViewModel : BaseEntity public class PhoneCreateViewModel : BaseEntity, IPhoneEditable
{ {
[Required(ErrorMessage = "Поле обязательно для заполнения")] [Required(ErrorMessage = "Поле обязательно для заполнения")]
[Remote("CheckName", "PhoneValidator", ErrorMessage = "Имя занято")] [Remote("CheckName", "PhoneValidator", ErrorMessage = "Имя занято")]
......
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using PhoneStore.Models;
namespace PhoneStore.ViewModels.PhoneViewModels
{
public class PhoneEditViewModel : BaseEntity, IPhoneEditable
{
public string Name { get; set; }
public decimal? Price { get; set; }
public int BrandId { get; set; }
public List<Brand> Brands { get; set; }
public IFormFile File { get; set; }
public string Image { get; set; }
}
}
\ No newline at end of file
@using PhoneStore.Repositories.Interfaces @using PhoneStore.Repositories.Interfaces
@using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.PhoneViewModels.PhoneViewModel @model PhoneViewModel
@inject IFeedbackRepository _feedbackRepository; @inject IFeedbackRepository FeedbackRepository;
@inject UserManager<User> _userManager; @inject UserManager<User> UserManager;
@{ @{
ViewBag.Title = "Подробная информация"; ViewBag.Title = "Подробная информация";
Layout = "_Layout"; Layout = "_Layout";
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
</div> </div>
@if (User.Identity.IsAuthenticated) @if (User.Identity.IsAuthenticated)
{ {
if (!_feedbackRepository.CheckFeedbackExists(int.Parse(_userManager.GetUserId(User)), Model.Id)) if (!FeedbackRepository.CheckFeedbackExists(int.Parse(UserManager.GetUserId(User)), Model.Id))
{ {
<form class="mb-3" id="feedbackForm"> <form class="mb-3" id="feedbackForm">
<div class="form-group"> <div class="form-group">
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel @model IPhoneEditable
@{ @{
ViewBag.Title = "title"; ViewBag.Title = "title";
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel @model IPhoneEditable
@{ @{
ViewBag.Title = "Редактирование"; ViewBag.Title = "Редактирование";
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
ViewBag.Title = "Смартфоны"; ViewBag.Title = "Смартфоны";
Layout = "_Layout"; Layout = "_Layout";
} }
@if (Model.Phones.Count() == 0) @if (!Model.Phones.Any())
{ {
<h2>Список пуст.</h2> <h2>Список пуст.</h2>
} }
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel @model PhoneStore.ViewModels.PhoneViewModels.IPhoneEditable
<div class="mb-3"> <div class="mb-3">
<label asp-for="Name" class="form-label">Наименование</label> <label asp-for="Name" class="form-label">Наименование</label>
<input type="text" class="form-control" asp-for="Name"> <input type="text" class="form-control" asp-for="Name">
......
@using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.Feedback.FeedbackViewModel @model FeedbackViewModel
@inject UserManager<User> UserManager; @inject UserManager<User> UserManager;
<div id="@Model.Id" class="card mb-3"> <div id="@Model.Id" class="card mb-3">
......
...@@ -2,4 +2,6 @@ ...@@ -2,4 +2,6 @@
@using PhoneStore.Models @using PhoneStore.Models
@using PhoneStore.ViewModels @using PhoneStore.ViewModels
@using PhoneStore.ViewModels.Account @using PhoneStore.ViewModels.Account
@using PhoneStore.ViewModels.PhoneViewModels
@using PhoneStore.ViewModels.Feedback
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
\ No newline at end of file
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