Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
Lesson49
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Пасько Виталий
Lesson49
Commits
66021ec2
Commit
66021ec2
authored
Aug 10, 2022
by
Пасько Виталий
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- реализовал редактирование отзывов о телефонах.
parent
0402a7a2
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
140 additions
and
26 deletions
+140
-26
FeedbackController.cs
PhoneStore/Controllers/FeedbackController.cs
+14
-5
FeedbackService.cs
PhoneStore/Services/FeedbackService.cs
+14
-0
IFeedbackService.cs
PhoneStore/Services/Interfaces/IFeedbackService.cs
+1
-0
FeedbackEditViewModel.cs
PhoneStore/ViewModels/Feedback/FeedbackEditViewModel.cs
+8
-0
About.cshtml
PhoneStore/Views/Phones/About.cshtml
+57
-20
EditFeedbackModalPartial.cshtml
...Views/Shared/PartialViews/EditFeedbackModalPartial.cshtml
+24
-0
FeedbackPartialView.cshtml
...tore/Views/Shared/PartialViews/FeedbackPartialView.cshtml
+20
-1
site.js
PhoneStore/wwwroot/js/site.js
+2
-0
No files found.
PhoneStore/Controllers/FeedbackController.cs
View file @
66021ec2
...
...
@@ -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
PhoneStore/Services/FeedbackService.cs
View file @
66021ec2
...
...
@@ -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
PhoneStore/Services/Interfaces/IFeedbackService.cs
View file @
66021ec2
...
...
@@ -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
PhoneStore/ViewModels/Feedback/FeedbackEditViewModel.cs
0 → 100644
View file @
66021ec2
namespace
PhoneStore.ViewModels.Feedback
{
public
class
FeedbackEditViewModel
{
public
int
Id
{
get
;
set
;
}
public
string
Text
{
get
;
set
;
}
}
}
\ No newline at end of file
PhoneStore/Views/Phones/About.cshtml
View file @
66021ec2
...
...
@@ -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>
}
PhoneStore/Views/Shared/PartialViews/EditFeedbackModalPartial.cshtml
0 → 100644
View file @
66021ec2
<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">×</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
PhoneStore/Views/Shared/PartialViews/FeedbackPartialView.cshtml
View file @
66021ec2
@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
PhoneStore/wwwroot/js/site.js
View file @
66021ec2
...
...
@@ -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'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment