- реализовал редактирование отзывов о телефонах.

parent 0402a7a2
......@@ -29,11 +29,11 @@ namespace PhoneStore.Controllers
public async Task<IActionResult> Create(FeedbackCreateViewModel model)
{
var validationResult = await _feedbackValidation.ValidateAsync(model);
if (!validationResult.IsValid)
{
//TODO: сформировать сообщение
return NotFound();
}
// if (!validationResult.IsValid)
// {
// //TODO: сформировать сообщение
// return NotFound();
// }
var feedbackViewModel = _feedbackService.Create(model, User);
return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel);
}
......@@ -50,5 +50,14 @@ namespace PhoneStore.Controllers
};
return Json(model);
}
[HttpPost]
public IActionResult Update([FromBody]FeedbackEditViewModel model)
{
var feedbackViewModel = _feedbackService.Update(model);
if (feedbackViewModel is null)
return RedirectToAction("Error", "Errors", new {statusCode = 777});
return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel);
}
}
}
\ No newline at end of file
......@@ -42,5 +42,19 @@ namespace PhoneStore.Services
return feedbackViewModel;
}
public FeedbackViewModel Update(FeedbackEditViewModel model)
{
var feedback = _db.Feedbacks
.Include(f => f.User)
.Include(f => f.Phone)
.FirstOrDefault(f => f.Id == model.Id);
if (feedback is null)
return null;
feedback.Text = model.Text;
_db.Update(feedback);
_db.SaveChanges();
return feedback.MapToFeedbackViewModel();
}
}
}
\ No newline at end of file
......@@ -6,5 +6,6 @@ namespace PhoneStore.Services.Interfaces
public interface IFeedbackService
{
FeedbackViewModel Create(FeedbackCreateViewModel model, ClaimsPrincipal user);
FeedbackViewModel Update(FeedbackEditViewModel model);
}
}
\ No newline at end of file
namespace PhoneStore.ViewModels.Feedback
{
public class FeedbackEditViewModel
{
public int Id { get; set; }
public string Text { get; set; }
}
}
\ No newline at end of file
......@@ -8,25 +8,25 @@
<div class="card mb-3">
<div class="container">
<img class="card-img-top w-50" src="~/@Model.Image" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">@Model.Brand.Name</h5>
<p class="card-text">@(Model.Price.ToString("C2"))</p>
</div>
@if (User.Identity.IsAuthenticated)
{
<form class="mb-3" 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 class="my-3 btn btn-outline-warning" type="submit">Отправить</button>
</div>
</form>
}
else
{
<a asp-action="Login" asp-controller="Account">Авторизируйтесь для добавления отзыва</a>
}
<div class="card-body">
<h5 class="card-title">@Model.Brand.Name</h5>
<p class="card-text">@(Model.Price.ToString("C2"))</p>
</div>
@if (User.Identity.IsAuthenticated)
{
<form class="mb-3" 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 class="my-3 btn btn-outline-warning" type="submit">Отправить</button>
</div>
</form>
}
else
{
<a asp-action="Login" asp-controller="Account">Авторизируйтесь для добавления отзыва</a>
}
</div>
</div>
<div id="feedbacks">
......@@ -34,4 +34,41 @@
{
await Html.RenderPartialAsync("PartialViews/FeedbackPartialView", feedBack);
}
</div>
\ No newline at end of file
</div>
@{
await Html.RenderPartialAsync("PartialViews/EditFeedbackModalPartial");
}
@section Scripts
{
<script >
$('#exampleModal').on('show.bs.modal', function (event) {
const button = $(event.relatedTarget);
const oldText = button.attr('text');
const username = button.attr('username');
const feedbackId = button.attr('feedbackId');
const modal = $(this);
modal.find('.modal-title').text('Редактируете от имени: ' + username);
const textarea = modal.find('.modal-body textarea');
textarea.text(oldText);
console.log(textarea.text())
console.log(feedbackId)
$('#feedback-edit-form').on('submit', function (submitEvent){
submitEvent.preventDefault();
$('#exampleModal').modal('hide');
fetch('https://localhost:5001/feedback/update', {
method: 'post',
body: JSON.stringify({id: feedbackId, text: textarea.val()}),
headers: {'content-type': 'application/json'}
}).then(response => {
console.log(response);
return response.text();
}).then(text => {
$(`div[id=${feedbackId}]`).html(text);
}).catch((error) => {
alert(error);
});
});
});
</script>
}
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Редактировать отзыв</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form id="feedback-edit-form">
<div class="form-group">
<label for="message-text" class="col-form-label">Внесите изменения:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Отмена</button>
<button type="submit" class="btn btn-primary">Редактировать</button>
</div>
</form>
</div>
</div>
</div>
</div>
\ No newline at end of file
@using Microsoft.AspNetCore.Identity
@model PhoneStore.ViewModels.Feedback.FeedbackViewModel
@inject UserManager<User> _userManager;
<div class="card mb-3">
<div id="@Model.Id" class="card mb-3">
<h5 class="card-header">
@Model.User.Name
</h5>
......@@ -9,5 +11,22 @@
Отзыв
</h5>
<div class="card-text">@Model.Text</div>
@if (User.Identity.IsAuthenticated)
{
if (int.Parse(_userManager.GetUserId(User)) == Model.UserId)
{
<button type="button"
class="btn btn-outline-warning"
data-toggle="modal"
data-target="#exampleModal"
text="@Model.Text"
feedbackId="@Model.Id"
username="@Model.User.Name"
>
Редактировать
</button>
}
}
</div>
</div>
\ No newline at end of file
......@@ -36,6 +36,8 @@ $(document).ready(function () {
e.preventDefault();
let phoneId = $("input[name=phoneId]");
let text = $("#text");
console.log(text.val())
console.log(phoneId.attr('id'))
let feedback = {
text: text.val(),
phoneId: phoneId.attr('id')
......
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