refactor: убрал лишний код.

parent 92307b0f
......@@ -48,8 +48,15 @@ namespace PhoneStore.Controllers
public IActionResult Create(OrderViewModel order)
{
order.UserId = int.Parse(_userManager.GetUserId(User));
order.User = null;
_orderService.Create(order.MapToOrderViewModel());
return RedirectToAction("Index");
}
public IActionResult GetAllByUserId(int id)
{
var model = _orderService.GetByUserId(id);
return Json(model);
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Services.Interfaces;
namespace PhoneStore.Controllers
{
public class PersonalAreaController : Controller
{
private readonly IFeedbackService _feedbackService;
public PersonalAreaController(IFeedbackService feedbackService)
{
_feedbackService = feedbackService;
}
[HttpGet]
public IActionResult Index(int userId)
{
var model = _feedbackService.GetPersonalViewModel(userId);
return View(model);
}
}
}
\ No newline at end of file
......@@ -13,8 +13,8 @@ namespace PhoneStore.Mappers
Text = self.Text,
PhoneId = self.PhoneId,
UserId = self.UserId,
User = self.User.MapToUserViewModel(),
Phone = self.Phone.MapToPhoneViewModel(),
User = self.User?.MapToUserViewModel(),
Phone = self.Phone?.MapToPhoneViewModel(),
CreationDateTime = self.CreationDateTime
};
return feedbackViewModel;
......
......@@ -28,7 +28,7 @@ namespace PhoneStore.Mappers
Brand = self.Brand,
Image = self.Image,
Id = self.Id,
Feedbacks = self.Feedbacks.Select(f => f.MapToFeedbackViewModel()),
Feedbacks = self.Feedbacks?.Select(f => f.MapToFeedbackViewModel()),
BrandId = self.BrandId
};
......
......@@ -10,6 +10,6 @@ namespace PhoneStore.Models
public int BrandId { get; set; }
public Brand Brand { get; set; }
public string Image { get; set; }
public List<Feedback> Feedbacks { get; set; }
public IEnumerable<Feedback> Feedbacks { get; set; }
}
}
\ No newline at end of file
......@@ -7,5 +7,7 @@ namespace PhoneStore.Models
{
public string Name { get; set; }
public int Age { get; set; }
public IEnumerable<Feedback> Feedbacks { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.17" />
<PackageReference Include="FluentValidation" Version="11.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
......@@ -18,4 +19,8 @@
<_ContentIncludedByDefault Remove="wwwroot\Files\book.pdf" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages" />
</ItemGroup>
</Project>
......@@ -15,9 +15,6 @@ namespace PhoneStore.Repositories
_db = db;
}
public IEnumerable<Feedback> GetAllByUserId(int userId)
=> _db.Feedbacks.Where(f => f.UserId == userId);
public Feedback GetById(int id)
{
return _db.Feedbacks
......@@ -41,7 +38,10 @@ namespace PhoneStore.Repositories
_db.SaveChanges();
}
public IEnumerable<Order> GetByUserId(int id)
=> _db.Orders.Where(o => o.UserId == id);
public IEnumerable<Feedback> GetByUserId(int id)
=> _db.Feedbacks
.Include(f => f.Phone)
.Include(f => f.User)
.Where(o => o.UserId == id);
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ using PhoneStore.Repositories.Interfaces.Base;
namespace PhoneStore.Repositories.Interfaces
{
public interface IFeedbackRepository : IBaseOperations<Feedback>, IByUserIdProvider<Order>
public interface IFeedbackRepository : IBaseOperations<Feedback>, IByUserIdProvider<Feedback>
{
bool CheckFeedbackExists(int userId, int phoneId);
}
......
......@@ -3,6 +3,7 @@ using System.Linq;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Models;
using PhoneStore.Repositories.Interfaces;
using PhoneStore.Repositories.Interfaces.Base;
namespace PhoneStore.Repositories
{
......@@ -30,6 +31,8 @@ namespace PhoneStore.Repositories
public Order GetById(int id)
=> _db.Orders
.Include(o => o.Phone)
.ThenInclude(p => p.Brand)
.Include(p => p.Phone.Feedbacks)
.Include(o => o.User)
.FirstOrDefault(o => o.Id == id);
......@@ -37,10 +40,13 @@ namespace PhoneStore.Repositories
{
return _db.Orders
.Include(p => p.Phone)
.ThenInclude(p => p.Brand)
.Include(p => p.Phone.Feedbacks)
.Include(p => p.User)
.ToList();
}
public IEnumerable<Order> GetByUserId(int id)
=> _db.Orders
.Include(o => o.User)
......
using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Identity;
using PhoneStore.Mappers;
using PhoneStore.Models;
using PhoneStore.Repositories.Interfaces;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Services
......@@ -13,13 +15,15 @@ namespace PhoneStore.Services
{
private readonly UserManager<User> _userManager;
private readonly IFeedbackRepository _feedbackRepository;
private readonly MobileContext _db;
public FeedbackService(
UserManager<User> userManager,
IFeedbackRepository feedbackRepository)
IFeedbackRepository feedbackRepository, MobileContext db)
{
_userManager = userManager;
_feedbackRepository = feedbackRepository;
_db = db;
}
public FeedbackViewModel Create(FeedbackCreateViewModel model, ClaimsPrincipal user)
......@@ -59,5 +63,18 @@ namespace PhoneStore.Services
.GetById(id)
.MapToFeedbackViewModel();
}
public PersonalAreaViewModel GetPersonalViewModel(int userId)
{
PersonalAreaViewModel model = new PersonalAreaViewModel
{
Feedbacks = _feedbackRepository
.GetByUserId(userId)
.Select(f => f.MapToFeedbackViewModel()),
User = _db.Users.FirstOrDefault(u => u.Id == userId).MapToUserViewModel()
};
return model;
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Security.Claims;
using PhoneStore.Models;
using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Services.Interfaces
......@@ -9,5 +11,7 @@ namespace PhoneStore.Services.Interfaces
FeedbackViewModel Create(FeedbackCreateViewModel model, ClaimsPrincipal user);
FeedbackViewModel Update(FeedbackEditViewModel model);
FeedbackViewModel GetById(int id);
PersonalAreaViewModel GetPersonalViewModel(int userId);
}
}
\ No newline at end of file
......@@ -8,7 +8,6 @@ namespace PhoneStore.Services.Interfaces
{
IEnumerable<OrderViewModel> GetAll();
void Create(Order order);
OrderViewModel GetById(int id);
IEnumerable<OrderViewModel> GetByUserId(int id);
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using PhoneStore.Helpers;
using PhoneStore.Models;
......@@ -24,10 +25,9 @@ namespace PhoneStore
services.AddDbContext<MobileContext>(options => options.UseNpgsql(connection))
.AddIdentity<User, Role>()
.AddEntityFrameworkStores<MobileContext>();
services.AddControllersWithViews();
//Можно так подключать сервисы
services.AddControllersWithViews().AddNewtonsoftJson(o =>
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
services.AddApplicationServices(Configuration);
//А можно так
RepositoryConnector.AddRepositories(services);
services.AddValidationServices();
services.AddCors();
......
using System.Collections.Generic;
using PhoneStore.ViewModels.Account;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.ViewModels
{
public class PersonalAreaViewModel
{
public UserViewModel User { get; set; }
public IEnumerable<FeedbackViewModel> Feedbacks { get; set; }
}
}
\ No newline at end of file
@model PhoneStore.ViewModels.PersonalAreaViewModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
<h2 userId="@Model.User.Id">Профиль пользователя @Model.User.Name</h2>
<div class="tabs">
<div class="tab-items row w-50 m-auto">
<a href="#tab-feedbacks" class="active tab-pane col-md-6">Отзывы</a>
<a href="#tab-orders" class="tab-pane col-md-6">Заказы</a>
</div>
<div class="tab-contents">
<div id="tab-feedbacks" class="tab-content">
@foreach (var feedback in Model.Feedbacks)
{
await Html.RenderPartialAsync("PartialViews/FeedbackPartialView", feedback);
}
</div>
<div id="tab-orders" class="tab-content">
<table style="width: 100%">
<thead>
<tr>
<th>Id</th>
<th>Адрес</th>
<th>Контактный телефон</th>
<th>Имя пользователя</th>
<th>Название телефона</th>
</tr>
</thead>
<tbody id="orders">
</tbody>
</table>
</div>
</div>
</div>
@section Scripts
{
<script>
$('.tab-pane').click((e) => {
e.preventDefault;
let userId = $('h2').attr('userId');
fetch('https://localhost:5001/Orders/GetAllByUserId/' + userId)
.then((response) => {
return response.json();
})
.then((orders) => {
if (orders.length === 0){
$(this).html("<h3>Заказов нет</h3>");
console.log("orders - 0")
}
else {
let content;
$.each(orders, (order) => {
content += `<tr>
<td>order.id</td>
<td>order.address</td>
<td>order.contact.Phone</td>
<td>order.user.Name</td>
<td>order.phone.Name</td>
</tr>`
});
console.log("content " + content);
$('#orders').html(content);
}
});
});
let makeTabs = () => {
let tabs = $('.tabs').find('.tab-items a');
console.log(tabs);
$.each(tabs, (index, item) => {
if ($(item).hasClass('active')){
console.log(item)
let activeTabContent = $(this).attr('href');
$(this).parents(".tabs").find(activeTabContent).show();
}
});
$(tabs).click((e) => {
console.log("click tab");
e.preventDefault();
if (!$(this).hasClass("active")){
let tabContent = $(this).attr('href');
$(this).siblings('.tab-items').removeClass('active');
$(this).addClass('active');
$(this).closest('.tabs').find('.tab-content').hide();
$(this).closest('.tabs').find(tabContent).show();
}
})
}
makeTabs();
</script>
}
<!DOCTYPE html>
@using Microsoft.AspNetCore.Identity
@inject UserManager<User> UserManager
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
......@@ -35,7 +37,7 @@
<div class="login_group">
@if(User.Identity.IsAuthenticated)
{
<p>@User.Identity.Name</p>
<a asp-action="Index" asp-controller="PersonalArea" asp-route-userId="@UserManager.GetUserId(User)">@User.Identity.Name</a>
<form method="post" asp-controller="Account" asp-action="LogOff">
<button class="btn btn-outline-warning" type="submit">Выход</button>
......
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=54320;database=lesson63;Username=postgres;Password=postgres;"
"DefaultConnection": "Server=localhost;Port=54320;database=lesson68;Username=postgres;Password=postgres;"
},
"Logging": {
"LogLevel": {
......
......@@ -77,3 +77,36 @@ body {
.w_15 {
width: 15%;
}
.tabs{
padding: 40px;
}
.tab-items{
margin-bottom: -1px;
position: relative;
z-index: 10;
}
.tab-items a{
display: inline-block;
padding: 1px 25px;
border: 1px solid gold;
border-bottom: none;
background: #c69500;
color: black;
text-decoration: none;
}
.tab-items a.active{
color: #9fcdff;
background: #fff;
}
.tabs .tab-content{
padding: 20px;
border: 1px solid silver;
/*display: none;*/
position: relative;
z-index: 9;
}
\ 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