- Реализовал добавление отзывов о телефоне.

- Переименовал папку Abstracts.
- Вынес маппинг в расширяющие методы.
- Логику из экшна About вынес в сервис.
parent 34e9f927
......@@ -3,7 +3,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Helpers;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Account;
namespace PhoneStore.Controllers
......
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
namespace PhoneStore.Controllers
......
using System;
using System.Linq;
using System.Text.Json;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Models;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Controllers
{
public class FeedbackController : Controller
{
private readonly MobileContext _db;
private readonly UserManager<User> _userManager;
public FeedbackController(MobileContext db, UserManager<User> userManager)
{
_db = db;
_userManager = userManager;
}
// GET
public ActionResult<FeedbackViewModel> Create(FeedbackCreateViewModel model)
{
Feedback feedback = new Feedback
{
CreationDateTime = DateTime.Now,
Text = model.Text,
PhoneId = model.PhoneId,
UserId = int.Parse(_userManager.GetUserId(User))
};
_db.Feedbacks.Add(feedback);
_db.SaveChanges();
Feedback newFeedback = _db.Feedbacks
.Include(f => f.User)
.Include(f => f.Phone)
.First(f => f.Id == feedback.Id);
FeedbackViewModel feedbackViewModel = new FeedbackViewModel
{
Id = newFeedback.Id,
Text = newFeedback.Text,
PhoneId = newFeedback.PhoneId,
UserId = newFeedback.UserId,
CreationDateTime = newFeedback.CreationDateTime
};
return Json(feedbackViewModel);
}
}
}
\ No newline at end of file
......@@ -7,8 +7,10 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Account;
using PhoneStore.ViewModels.PhoneViewModels;
namespace PhoneStore.Controllers
{
......@@ -138,36 +140,17 @@ namespace PhoneStore.Controllers
[HttpGet]
public IActionResult About(int? phoneId)
{
try
{
if (!phoneId.HasValue) return RedirectToAction("Error", "Errors", new {statusCode = 777});
var phone = _db.Phones
.Include(p => p.Brand)
.Include(p => p.Feedbacks)
.ThenInclude(f => f.User)
.FirstOrDefault(p => p.Id == phoneId);
if (phone is null)
return RedirectToAction("Error", "Errors", new {statusCode = 777});
var phoneViewModel = new PhoneViewModel
{
Brand = phone.Brand,
Feedbacks = phone.Feedbacks.Select(f => new FeedbackViewModel
{
Id = f.Id,
Phone = f.Phone,
Text = f.Text,
User = f.User,
CreationDateTime = f.CreationDateTime,
UserId = f.UserId,
PhoneId = f.PhoneId
})
.OrderByDescending(f => f.CreationDateTime)
.ToList(),
Image = phone.Image,
Name = phone.Name,
Price = phone.Price,
Id = phone.Id
};
var phoneViewModel = _phoneService.GetPhoneById((int)phoneId);
return View(phoneViewModel);
}
catch (NullReferenceException)
{
return RedirectToAction("Error", "Errors", new {statusCode = 777});
}
}
}
}
\ No newline at end of file
using PhoneStore.Models;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.PhoneViewModels;
namespace PhoneStore.Helpers
{
......@@ -18,5 +19,19 @@ namespace PhoneStore.Helpers
return phone;
}
public static PhoneViewModel MapToPhoneViewModel(this Phone self, string imagePath = null)
{
PhoneViewModel phone = new PhoneViewModel
{
Name = self.Name,
Price = self.Price,
Brand = self.Brand,
Image = imagePath,
Id = self.Id
};
return phone;
}
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PhoneStore.Models;
using PhoneStore.Services;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Helpers
{
......
using System.Linq;
using PhoneStore.Models;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Account;
namespace PhoneStore.Helpers
{
......@@ -20,5 +21,18 @@ namespace PhoneStore.Helpers
PageViewModel = new PageViewModel(page, count, pageSize)
};
}
public static UserViewModel MapToUserViewModel(
this User self)
{
return new UserViewModel
{
Age = self.Age,
Email = self.Email,
Id = self.Id,
Name = self.Name,
Username = self.UserName
};
}
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Account;
using IdentityResult = PhoneStore.DataObjects.IdentityResult;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
......@@ -82,7 +82,6 @@ namespace PhoneStore.Services
public async Task LogOf()
=> await _signInManager.SignOutAsync();
[HttpGet]
public IEnumerable<User> SearchUsersByAnyTerm(string searchTerm)
{
var resultByName = _usersSearcher.SearchByName(searchTerm);
......
......@@ -3,7 +3,7 @@ using System.Linq;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Enums;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
......@@ -4,7 +4,7 @@ using PhoneStore.DataObjects;
using PhoneStore.Models;
using PhoneStore.ViewModels.Account;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IAccountService
{
......
......@@ -2,7 +2,7 @@ using System.Collections.Generic;
using PhoneStore.Enums;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IBasketService
{
......
using System.Threading.Tasks;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface ICreatable<in T> where T : class
{
......
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IDefaultPhoneImagePathProvider
{
......
using System.Linq;
using System.Threading.Tasks;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IPaginationService<T>
{
......
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.PhoneViewModels;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IPhoneService : ICreatable<PhoneCreateViewModel>
{
PhoneViewModel GetPhoneById(int phoneId);
}
}
\ No newline at end of file
using System.Linq;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IUsersFilter
{
......
using System.Collections.Generic;
using PhoneStore.Models;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IUsersSearcher
{
......
......@@ -2,7 +2,7 @@ using System.Linq;
using PhoneStore.Models;
using Order = PhoneStore.Enums.Order;
namespace PhoneStore.Services.Abstractions
namespace PhoneStore.Services.Interfaces
{
public interface IUsersSortService
{
......
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
......@@ -2,11 +2,14 @@ using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Feedback;
using PhoneStore.ViewModels.PhoneViewModels;
namespace PhoneStore.Services
{
......@@ -50,5 +53,36 @@ namespace PhoneStore.Services
_db.Phones.Add(entity.MapToPhone(imagePath));
await _db.SaveChangesAsync();
}
public PhoneViewModel GetPhoneById(int phoneId)
{
var phone = _db.Phones
.Include(p => p.Brand)
.Include(p => p.Feedbacks)
.ThenInclude(f => f.User)
.FirstOrDefault(p => p.Id == phoneId);
var phoneViewModel = new PhoneViewModel
{
Brand = phone.Brand,
Feedbacks = phone.Feedbacks.Select(f => new FeedbackViewModel
{
Id = f.Id,
Phone = f.Phone.MapToPhoneViewModel(),
Text = f.Text,
User = f.User.MapToUserViewModel(),
CreationDateTime = f.CreationDateTime,
UserId = f.UserId,
PhoneId = f.PhoneId
})
.OrderByDescending(f => f.CreationDateTime)
.ToList(),
Image = phone.Image,
Name = phone.Name,
Price = phone.Price,
Id = phone.Id
};
return phoneViewModel;
}
}
}
\ No newline at end of file
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......
......@@ -2,7 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Services
{
......@@ -16,15 +16,21 @@ namespace PhoneStore.Services
}
public IEnumerable<User> SearchByName(string searchTerm)
=> _db.Users.Where(u => u.Name
=> _db.Users
.Where(u => u.Name
.ToLower()
.Contains(searchTerm
.ToLower()));
.Contains(searchTerm.ToLower()));
public IEnumerable<User> SearchByLogin(string searchTerm)
=> _db.Users.Where(u => u.UserName.ToLower().Contains(searchTerm.ToLower()));
=> _db.Users
.Where(u => u.UserName
.ToLower()
.Contains(searchTerm.ToLower()));
public IEnumerable<User> SearchByEmail(string searchTerm)
=> _db.Users.Where(u => u.Email.ToLower().Contains(searchTerm.ToLower()));
=> _db.Users
.Where(u => u.Email
.ToLower()
.Contains(searchTerm.ToLower()));
}
}
\ No newline at end of file
using System.Linq;
using System.Threading.Tasks;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.Services.Interfaces;
using Order = PhoneStore.Enums.Order;
namespace PhoneStore.Services
......
......@@ -9,7 +9,6 @@ using Microsoft.Extensions.Hosting;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services;
using PhoneStore.Services.Abstractions;
namespace PhoneStore
{
......
namespace PhoneStore.ViewModels.Account
{
public class UserViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public string Username { get; set; }
}
}
\ No newline at end of file
namespace PhoneStore.ViewModels
namespace PhoneStore.ViewModels.Feedback
{
public class FeedbackCreateViewModel
{
public int UserId { get; set; }
public int PhoneId { get; set; }
public string Text { get; set; }
}
......
using System;
using PhoneStore.Models;
using PhoneStore.ViewModels.Account;
using PhoneStore.ViewModels.Account;
using PhoneStore.ViewModels.PhoneViewModels;
namespace PhoneStore.ViewModels
namespace PhoneStore.ViewModels.Feedback
{
public class FeedbackViewModel
{
......@@ -10,7 +12,7 @@ namespace PhoneStore.ViewModels
public int PhoneId { get; set; }
public string Text { get; set; }
public DateTime CreationDateTime { get; set; }
public User User { get; set; }
public Phone Phone { get; set; }
public UserViewModel User { get; set; }
public PhoneViewModel Phone { get; set; }
}
}
\ No newline at end of file
using System.Collections;
using System.Collections.Generic;
using PhoneStore.Models;
......
......@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Models;
namespace PhoneStore.ViewModels
namespace PhoneStore.ViewModels.PhoneViewModels
{
public class PhoneCreateViewModel
{
......
using System.Collections.Generic;
using PhoneStore.Models;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.ViewModels
namespace PhoneStore.ViewModels.PhoneViewModels
{
public class PhoneViewModel
{
......
@model PhoneViewModel
@using Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.PhoneViewModels.PhoneViewModel
@inject UserManager<User> _userManager
@{
ViewBag.Title = "Подробная информация";
Layout = "_Layout";
......@@ -12,4 +13,45 @@
<h5 class="card-title">@Model.Brand.Name</h5>
<p class="card-text">@(Model.Price.ToString("C2"))</p>
</div>
@if (User.Identity.IsAuthenticated)
{
<form id="feedbackForm">
<div class="form-group">
<label for="text">Добавьте отзыв о телефоне</label>
<input type="text" class="form-control" id="text" placeholder="Минимальная длина отзыва 10 символов">
<input type="text" hidden name="phoneId" id="@Model.Id">
<button type="submit">Отправить</button>
</div>
</form>
}
else
{
<a asp-action="Login" asp-controller="Account">Авторизируйтесь для добавления отзыва</a>
}
</div>
<div id="feedbacks">
@foreach (var feedBack in Model.Feedbacks)
{
await Html.RenderPartialAsync("PartialViews/FeedbackPartialView", feedBack);
}
</div>
@section Scripts
{
<script >
$(document).ready(function (){
$("#feedbackForm").on("submit", function (e){
e.preventDefault();
let phoneId = $("input[name=phoneId]");
let text = $("#text");
let feedback = {
text: text.val(),
phoneId: phoneId.attr('id')
};
$.post(`@Url.Action("Create", "Feedback")`, feedback, function (response){
$('#feedbacks').before(response);
})
})
})
</script>
}
@model PhoneCreateViewModel
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
@{
ViewBag.Title = "title";
......
@model PhoneCreateViewModel
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
@{
ViewBag.Title = "Редактирование";
......
@model PhoneCreateViewModel
@model PhoneStore.ViewModels.PhoneViewModels.PhoneCreateViewModel
<div class="mb-3">
<label asp-for="Name" class="form-label">Наименование</label>
<input type="text" class="form-control" asp-for="Name">
......
@model PhoneStore.ViewModels.Feedback.FeedbackViewModel
<div class="card mb-3">
<h5 class="card-header">
@Model.User.Name
</h5>
<div class="card-body">
<h5 class="card-title">
Отзыв
</h5>
<div class="card-text">@Model.Text</div>
</div>
</div>
\ 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