refactor:

-Определил общий интерфейс для старницы создания и изменения телефона.
- Исправил валидацию названия телефона при редактировании модели.
parent 8fdac13f
......@@ -50,7 +50,7 @@ namespace PhoneStore.Controllers
{
order.UserId = int.Parse(_userManager.GetUserId(User));
order.User = null;
_orderService.Create(order.MapToOrderViewModel());
_orderService.Create(order.MapToOrder());
return RedirectToAction("Index");
}
......
......@@ -15,8 +15,15 @@ namespace PhoneStore.Controllers
}
[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
.AsEnumerable()
.Any(p => p.Name.Equals(name, StringComparison.CurrentCulture));
......
......@@ -47,7 +47,7 @@ namespace PhoneStore.Controllers
[HttpGet]
public IActionResult Create()
{
PhoneCreateViewModel model = new PhoneCreateViewModel
IPhoneEditable model = new PhoneCreateViewModel
{
Brands = _db.Brands.ToList()
};
......@@ -111,7 +111,7 @@ namespace PhoneStore.Controllers
if (phone is null)
return RedirectToAction("Error", "Errors", new {statusCode = 777});
PhoneCreateViewModel model = new PhoneCreateViewModel
IPhoneEditable model = new PhoneEditViewModel
{
Id = phone.Id,
Name = phone.Name,
......
......@@ -18,7 +18,7 @@ namespace PhoneStore.Mappers
Id = self.Id
};
}
public static Order MapToOrder(this OrderViewModel self)
{
return new Order
......
......@@ -20,8 +20,4 @@
<_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages" />
</ItemGroup>
</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;
namespace PhoneStore.ViewModels.PhoneViewModels
{
public class PhoneCreateViewModel : BaseEntity
public class PhoneCreateViewModel : BaseEntity, IPhoneEditable
{
[Required(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 Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.PhoneViewModels.PhoneViewModel
@inject IFeedbackRepository _feedbackRepository;
@inject UserManager<User> _userManager;
@model PhoneViewModel
@inject IFeedbackRepository FeedbackRepository;
@inject UserManager<User> UserManager;
@{
ViewBag.Title = "Подробная информация";
Layout = "_Layout";
......@@ -18,7 +18,7 @@
</div>
@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">
<div class="form-group">
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
@model IPhoneEditable
@{
ViewBag.Title = "title";
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
@model IPhoneEditable
@{
ViewBag.Title = "Редактирование";
......
......@@ -4,7 +4,7 @@
ViewBag.Title = "Смартфоны";
Layout = "_Layout";
}
@if (Model.Phones.Count() == 0)
@if (!Model.Phones.Any())
{
<h2>Список пуст.</h2>
}
......
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
@model PhoneStore.ViewModels.PhoneViewModels.IPhoneEditable
<div class="mb-3">
<label asp-for="Name" class="form-label">Наименование</label>
<input type="text" class="form-control" asp-for="Name">
......
@using Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.Feedback.FeedbackViewModel
@model FeedbackViewModel
@inject UserManager<User> UserManager;
<div id="@Model.Id" class="card mb-3">
......
......@@ -2,4 +2,6 @@
@using PhoneStore.Models
@using PhoneStore.ViewModels
@using PhoneStore.ViewModels.Account
@using PhoneStore.ViewModels.PhoneViewModels
@using PhoneStore.ViewModels.Feedback
@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