Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
CafeCritic
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
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
Yevgeniy Agrafenin
CafeCritic
Commits
d786fb76
Commit
d786fb76
authored
Nov 20, 2023
by
Yevgeniy Agrafenin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
Добавил функционал загрузки файла для галереи заведения. Добавид расширение enum.
parent
78116f17
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
188 additions
and
34 deletions
+188
-34
CafeController.cs
CafeCritic/Controllers/CafeController.cs
+34
-1
AddReviewDto.cs
CafeCritic/Services/AddReviewDto.cs
+7
-0
ExtensionsEnum.cs
CafeCritic/Services/Extensions/ExtensionsEnum.cs
+17
-0
FileUpload.cs
CafeCritic/Services/FileUpload.cs
+2
-2
Details.cshtml
CafeCritic/Views/Cafe/Details.cshtml
+101
-30
Index.cshtml
CafeCritic/Views/Cafe/Index.cshtml
+27
-1
No files found.
CafeCritic/Controllers/CafeController.cs
View file @
d786fb76
...
@@ -30,7 +30,8 @@ public class CafeController : Controller
...
@@ -30,7 +30,8 @@ public class CafeController : Controller
int
pageSize
=
10
;
int
pageSize
=
10
;
IQueryable
<
Cafe
>
source
=
_db
.
Cafes
;
IQueryable
<
Cafe
>
source
=
_db
.
Cafes
;
var
count
=
await
source
.
CountAsync
();
var
count
=
await
source
.
CountAsync
();
var
items
=
await
source
.
Skip
((
page
-
1
)
*
pageSize
).
Take
(
pageSize
).
ToListAsync
();
var
items
=
await
source
.
Skip
((
page
-
1
)
*
pageSize
).
Take
(
pageSize
)
.
Include
(
c
=>
c
.
Reviews
).
ToListAsync
();
PageViewModel
pageViewModel
=
new
PageViewModel
(
count
,
page
,
pageSize
);
PageViewModel
pageViewModel
=
new
PageViewModel
(
count
,
page
,
pageSize
);
CafeViewModel
viewModel
=
new
CafeViewModel
CafeViewModel
viewModel
=
new
CafeViewModel
{
{
...
@@ -106,4 +107,36 @@ public class CafeController : Controller
...
@@ -106,4 +107,36 @@ public class CafeController : Controller
var
cafeViewModels
=
cafes
.
Select
(
CafeMapper
.
CafeCafeDetailViewModel
).
ToList
();
var
cafeViewModels
=
cafes
.
Select
(
CafeMapper
.
CafeCafeDetailViewModel
).
ToList
();
return
View
(
cafeViewModels
);
return
View
(
cafeViewModels
);
}
}
[
HttpGet
]
[
Authorize
]
[
Display
(
Name
=
"RemoveReview"
)]
public
async
Task
<
IActionResult
>
RemoveReviewAsync
(
int
cafeId
)
{
var
review
=
await
_db
.
Reviews
.
FirstOrDefaultAsync
(
r
=>
r
.
UserId
==
_userManager
.
GetUserId
(
User
)
&&
r
.
CafeId
==
cafeId
);
if
(
review
is
not
null
)
{
_db
.
Reviews
.
Remove
(
review
);
await
_db
.
SaveChangesAsync
();
return
RedirectToAction
(
"Details"
,
new
{
id
=
cafeId
});
}
return
NotFound
();
}
[
HttpPost
]
[
Authorize
]
public
async
Task
<
ActionResult
>
Add
([
FromForm
]
AddReviewDto
dto
)
{
var
path
=
await
FileUpload
.
Upload
(
Guid
.
NewGuid
().
ToString
(),
dto
.
Photo
);
var
cafe
=
await
_db
.
Cafes
.
FindAsync
(
dto
.
CafeId
);
cafe
.
Gallery
.
Add
(
path
);
await
_db
.
SaveChangesAsync
();
return
Ok
(
new
{
success
=
true
});
}
}
}
\ No newline at end of file
CafeCritic/Services/AddReviewDto.cs
0 → 100644
View file @
d786fb76
namespace
CafeCritic.Services
;
public
class
AddReviewDto
{
public
IFormFile
Photo
{
get
;
set
;
}
public
int
CafeId
{
get
;
set
;
}
}
\ No newline at end of file
CafeCritic/Services/Extensions/ExtensionsEnum.cs
0 → 100644
View file @
d786fb76
using
System.ComponentModel.DataAnnotations
;
using
System.Reflection
;
using
CafeCritic.Enums
;
namespace
CafeCritic.Services
;
public
static
class
ExtensionsEnum
{
public
static
string
GetDisplayName
(
this
Rating
rating
)
{
var
displayAttribute
=
rating
.
GetType
()
.
GetMember
(
rating
.
ToString
())[
0
]
.
GetCustomAttributes
(
typeof
(
DisplayAttribute
),
false
)[
0
]
as
DisplayAttribute
;
return
displayAttribute
.
Name
;
}
}
\ No newline at end of file
CafeCritic/Services/FileUpload.cs
View file @
d786fb76
...
@@ -2,7 +2,7 @@ namespace CafeCritic.Services;
...
@@ -2,7 +2,7 @@ namespace CafeCritic.Services;
public
class
FileUpload
public
class
FileUpload
{
{
public
static
async
Task
<
string
>
Upload
(
string
?
name
,
IFormFile
?
file
)
public
static
async
Task
<
string
>
Upload
(
string
?
name
,
IFormFile
file
)
{
{
name
=
name
?.
Replace
(
' '
,
'_'
);
name
=
name
?.
Replace
(
' '
,
'_'
);
var
basePath
=
Path
.
Combine
(
"wwwroot"
,
"uploads"
);
var
basePath
=
Path
.
Combine
(
"wwwroot"
,
"uploads"
);
...
@@ -10,7 +10,7 @@ public class FileUpload
...
@@ -10,7 +10,7 @@ public class FileUpload
if
(!
Directory
.
Exists
(
basePath
))
if
(!
Directory
.
Exists
(
basePath
))
Directory
.
CreateDirectory
(
basePath
);
Directory
.
CreateDirectory
(
basePath
);
var
extension
=
Path
.
GetExtension
(
file
!
.
FileName
);
var
extension
=
Path
.
GetExtension
(
file
.
FileName
);
var
filePath
=
Path
.
Combine
(
basePath
,
$"
{
name
}{
extension
}
"
);
var
filePath
=
Path
.
Combine
(
basePath
,
$"
{
name
}{
extension
}
"
);
await
using
var
stream
=
File
.
Create
(
filePath
);
await
using
var
stream
=
File
.
Create
(
filePath
);
...
...
CafeCritic/Views/Cafe/Details.cshtml
View file @
d786fb76
...
@@ -4,6 +4,19 @@
...
@@ -4,6 +4,19 @@
@{
@{
ViewBag.Title = "Подробнее";
ViewBag.Title = "Подробнее";
Layout = "_Layout";
Layout = "_Layout";
var isReview = false;
double rating = 0;
foreach (var review in Model.Reviews)
if (review.UserId == ViewBag.UserId)
{
isReview = true;
}
foreach (var review in Model.Reviews)
{
rating += review.Rating;
}
rating = Math.Round(rating / Model.Reviews.Count, 1);
}
}
<h2>Подробнее о заведении</h2>
<h2>Подробнее о заведении</h2>
...
@@ -38,9 +51,12 @@
...
@@ -38,9 +51,12 @@
{
{
<h4>В галерее пока нет фото</h4>
<h4>В галерее пока нет фото</h4>
}
}
<div id="comments">
</div>
</div>
</div>
<div>
<div>
<h4>Рейтинг:</h4>
<h4>Рейтинг:
@rating
</h4>
</div>
</div>
<div>
<div>
<h4>Отзывы:</h4>
<h4>Отзывы:</h4>
...
@@ -61,42 +77,64 @@
...
@@ -61,42 +77,64 @@
</tr>
</tr>
}
}
</table>
</table>
<div class="d-flex justify-content-center">
@if (!isReview)
<div class="card card-width">
{
<div class="card-body d-flex justify-content-center">
<div class="d-flex justify-content-center">
@foreach (var review in Model.Reviews)
<div class="card card-width">
{
<div class="card-body d-flex justify-content-center">
if (review.UserId != ViewBag.UserId)
@foreach (var review in Model.Reviews)
{
<form id="addUser" enctype="multipart/form-data">
<div class="text-center">
<h6>Оставить отзыв</h6>
</div>
<div>
<input type="text" class="form-control my-2 text-dark" name="answer" id="writeAnswer" placeholder="Отзыв">
</div>
<select class="form-control" id="rating">
@foreach (var rating in ViewBag.Rating)
{
<option value="@rating">@rating</option>
}
</select>
<input type="button" id="submit" class="btn btn-info w-100 mt-3 text-white" value="Введите ответ"/>
</form>
}
else
{
{
<p>Вы уже оставляли комментарий</p>
if (review.UserId != ViewBag.UserId)
{
<form id="addUser" enctype="multipart/form-data">
<div class="text-center">
<h6>Оставить отзыв</h6>
</div>
<div>
<input type="text" class="form-control my-2 text-dark" name="answer" id="writeAnswer" placeholder="Отзыв">
</div>
<select class="form-control" id="rating">
@foreach (var rating in ViewBag.Rating)
{
<option value="@rating">@rating.GetDisplayName()</option>
}
</select>
<input type="button" id="submit" class="btn btn-info w-100 mt-3 text-white" value="Введите ответ"/>
</form>
}
}
}
}
</div>
</div>
</div>
</div>
</div>
}
else
{
<div class="d-flex justify-content-center">
<div class="card card_width">
<div class="card-body d-flex justify-content-center">
<a asp-action="RemoveReview" asp-route-cafeId="@Model.Id" class="btn btn-danger w-100">Удалить отзыв</a>
</div>
</div>
</div>
}
</div>
<div class="d-flex justify-content-center mt-3">
<div class="card card_width">
<div class="card-body d-flex justify-content-center">
<form id="form" enctype="multipart/form-data">
<div class="text-center">
<h6>Добавить изображение</h6>
</div>
<div>
<input class="form-control my-2" type="file" name="photo" id="photo" accept="image/*">
</div>
<input type="button" onclick="submitForm()" class="btn btn-info w-100 mt-3 text-white" value="Отправить"/>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<a asp-controller="Cafe" asp-action="Index">Список заведений.</a>
<a asp-controller="Cafe" asp-action="Index">Список заведений.</a>
@section Scripts
@section Scripts
{
{
...
@@ -118,5 +156,38 @@
...
@@ -118,5 +156,38 @@
window.scrollTo(0, scrollHeight);
window.scrollTo(0, scrollHeight);
});
});
});
});
function submitForm(){
let fileInput = $('#photo')[0];
if (fileInput.files.length > 0){
let formData = new FormData();
formData.append('photo', fileInput.files[0]);
formData.append('cafeId', @Model.Id);
$.ajax({
url: '@Url.Action("Add")',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (data){
console.log('данные загружены', data);
let divForPhoto = document.createElement('div');
divForPhoto.setAttribute("class", "col-3 pe-0 ps-0 m-0");
divForPhoto.setAttribute("id", "comments");
divForPhoto.innerHTML = ('<img width="190px" height="200px" alt="post_image" src=' + '/' + data.path + '>');
document.getElementById('comments').appendChild(divForPhoto);
},
error: function (){
console.error('Ошика загрузки данных')
}
});
}else{
console.error('Данные некорректны');
}
}
</script>
</script>
}
}
CafeCritic/Views/Cafe/Index.cshtml
View file @
d786fb76
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
@{
@{
ViewBag.Title = "title";
ViewBag.Title = "title";
Layout = "_Layout";
Layout = "_Layout";
double result = 0;
}
}
@if (Model.Cafes.Count() == 0)
@if (Model.Cafes.Count() == 0)
...
@@ -20,8 +21,33 @@ else
...
@@ -20,8 +21,33 @@ else
<img src="@m.Image" class="card-img-top" alt="PhotoCafe">
<img src="@m.Image" class="card-img-top" alt="PhotoCafe">
</a>
</a>
<div class="card-body">
<div class="card-body">
<a asp-action="Details" asp-route-id="@m.Id">@m.Title</a>
<h4 class="card-title">
<a asp-action="Details" asp-route-id="@m.Id">@m.Title</a>
</h4>
</div>
</div>
<ul>
<li>
@foreach (var cafeReview in m.Reviews)
{
result += cafeReview.Rating;
}
@if (m.Reviews.Count != 0)
{
<p>Оценка: @Math.Round(result / m.Reviews.Count, 1)</p>
}
else
{
<p>Оценка: Отзывов пока нет.</p>
}
</li>
<li>
Кол-во изображений:
@if (m.Gallery != null)
{
@m.Gallery.Count
}
</li>
</ul>
</div>
</div>
}
}
</table>
</table>
...
...
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