Commit dc4904b2 authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#4 Реализовал футкционал поиска заведений, добавил перечисление рейтинга.

parent e0839a9f
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using CafeCritic.Context; using CafeCritic.Context;
using CafeCritic.Enums;
using CafeCritic.Mappers; using CafeCritic.Mappers;
using CafeCritic.Models; using CafeCritic.Models;
using CafeCritic.Services; using CafeCritic.Services;
...@@ -48,6 +49,7 @@ public class CafeController : Controller ...@@ -48,6 +49,7 @@ public class CafeController : Controller
} }
[HttpPost] [HttpPost]
[Authorize]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
[Display(Name="AddCafe")] [Display(Name="AddCafe")]
public async Task<IActionResult> AddCafeAsync(AddCafeViewModel model) public async Task<IActionResult> AddCafeAsync(AddCafeViewModel model)
...@@ -66,12 +68,42 @@ public class CafeController : Controller ...@@ -66,12 +68,42 @@ public class CafeController : Controller
public IActionResult Details(int id) public IActionResult Details(int id)
{ {
var cafe = _db.Cafes.FirstOrDefault(c => c.Id == id); var cafe = _db.Cafes
if (cafe is null) .Include(c => c.Reviews)
.FirstOrDefault(c => c.Id == id);
_db.Reviews.Include(r => r.User)
.ToList();
if (cafe is not null)
{
var viewModel = CafeMapper.CafeCafeDetailViewModel(cafe);
ViewBag.UserId = _userManager.GetUserId(User);
ViewBag.Rating = new List<Rating>
{ {
Rating.One,
Rating.Two,
Rating.Three,
Rating.Four,
Rating.Five
};
return View(viewModel);
}
return NotFound(); return NotFound();
} }
return View(cafe); [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[Display(Name = "CafeSearch")]
public async Task<IActionResult> CafeSearchAsync(string search)
{
var cafes = await _db.Cafes
.Where(c => c.Title.Contains(search) || c.Description.Contains(search))
.ToListAsync();
var cafeViewModels = cafes.Select(CafeMapper.CafeCafeDetailViewModel).ToList();
return View(cafeViewModels);
} }
} }
\ No newline at end of file
...@@ -14,6 +14,6 @@ public class ValidationController : Controller ...@@ -14,6 +14,6 @@ public class ValidationController : Controller
public bool CheckEmail(string email) => !_db.Users.Any(u => u.Email == email); public bool CheckEmail(string email) => !_db.Users.Any(u => u.Email == email);
public bool CheckCafeTitle(string cafeTitle) => !_db.Cafes.Any(u => u.Title == cafeTitle); public bool CheckCafeTitle(string cafeTitle) => !_db.Cafes.Any(c => c.Title == cafeTitle);
} }
\ No newline at end of file
using System.ComponentModel.DataAnnotations;
namespace CafeCritic.Enums;
public enum Rating
{
[Display(Name = "1")]
One = 1,
[Display(Name = "2")]
Two,
[Display(Name = "3")]
Three,
[Display(Name = "4")]
Four,
[Display(Name = "5")]
Five
}
\ No newline at end of file
...@@ -25,4 +25,17 @@ public class CafeMapper ...@@ -25,4 +25,17 @@ public class CafeMapper
Description = cafe.Description Description = cafe.Description
}; };
} }
public static CafeDetailViewModel CafeCafeDetailViewModel(Cafe cafe)
{
return new CafeDetailViewModel
{
Id = cafe.Id,
Title = cafe.Title,
Image = cafe.Image,
Description = cafe.Description,
Gallery = cafe.Gallery,
Reviews = cafe.Reviews
};
}
} }
\ No newline at end of file
@model List<CafeCritic.ViewModels.CafeDetailViewModel>
@{
ViewBag.Title = "Поиск заведений";
Layout = "_Layout";
}
<div class="container">
@foreach (var cafe in Model)
{
<div class="card mb-3 mx-auto my-0" style="width: 570px;">
<div class="row g-0">
<div class="col-md-4">
<img src="~/@cafe.Image" class="img-fluid card_image" alt="restaurant_image">
</div>
<div class="col-md-8 text-start">
<div class="card-body">
<h5 class="card-title">Название: @cafe.Title</h5>
<ul>
<li>
Оценка: @cafe.Reviews.Count
</li>
<li>Кол-во изображений:
@if (cafe.Gallery != null)
{
@cafe.Gallery.Count
}
</li>
</ul>
</div>
</div>
</div>
</div>
}
</div>
...@@ -25,6 +25,12 @@ ...@@ -25,6 +25,12 @@
</button> </button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1 justify-content-end"> <ul class="navbar-nav flex-grow-1 justify-content-end">
<form class="form-inline my-2 my-lg-0 search_form" asp-action="CafeSearch" asp-controller="Cafe" method="post">
<input class="form-control me-sm-2 search-input" type="search" aria-label="Search" name="search" placeholder="Поиск">
</form>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Cafe" asp-action="Index">Список заведений</a>
</li>
@if (!User.Identity.IsAuthenticated) @if (!User.Identity.IsAuthenticated)
{ {
<li class="nav-item"> <li class="nav-item">
......
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