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

parent 0402a7a2
...@@ -29,11 +29,11 @@ namespace PhoneStore.Controllers ...@@ -29,11 +29,11 @@ namespace PhoneStore.Controllers
public async Task<IActionResult> Create(FeedbackCreateViewModel model) public async Task<IActionResult> Create(FeedbackCreateViewModel model)
{ {
var validationResult = await _feedbackValidation.ValidateAsync(model); var validationResult = await _feedbackValidation.ValidateAsync(model);
if (!validationResult.IsValid) // if (!validationResult.IsValid)
{ // {
//TODO: сформировать сообщение // //TODO: сформировать сообщение
return NotFound(); // return NotFound();
} // }
var feedbackViewModel = _feedbackService.Create(model, User); var feedbackViewModel = _feedbackService.Create(model, User);
return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel); return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel);
} }
...@@ -50,5 +50,14 @@ namespace PhoneStore.Controllers ...@@ -50,5 +50,14 @@ namespace PhoneStore.Controllers
}; };
return Json(model); 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 ...@@ -42,5 +42,19 @@ namespace PhoneStore.Services
return feedbackViewModel; 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 ...@@ -6,5 +6,6 @@ namespace PhoneStore.Services.Interfaces
public interface IFeedbackService public interface IFeedbackService
{ {
FeedbackViewModel Create(FeedbackCreateViewModel model, ClaimsPrincipal user); 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
...@@ -35,3 +35,40 @@ ...@@ -35,3 +35,40 @@
await Html.RenderPartialAsync("PartialViews/FeedbackPartialView", feedBack); await Html.RenderPartialAsync("PartialViews/FeedbackPartialView", feedBack);
} }
</div> </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 @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"> <h5 class="card-header">
@Model.User.Name @Model.User.Name
</h5> </h5>
...@@ -9,5 +11,22 @@ ...@@ -9,5 +11,22 @@
Отзыв Отзыв
</h5> </h5>
<div class="card-text">@Model.Text</div> <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>
</div> </div>
\ No newline at end of file
...@@ -36,6 +36,8 @@ $(document).ready(function () { ...@@ -36,6 +36,8 @@ $(document).ready(function () {
e.preventDefault(); e.preventDefault();
let phoneId = $("input[name=phoneId]"); let phoneId = $("input[name=phoneId]");
let text = $("#text"); let text = $("#text");
console.log(text.val())
console.log(phoneId.attr('id'))
let feedback = { let feedback = {
text: text.val(), text: text.val(),
phoneId: phoneId.attr('id') 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