Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
F
final_exam_lyalya_aldamzharova
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Алдамжарова Ляйла
final_exam_lyalya_aldamzharova
Commits
915e8de6
Commit
915e8de6
authored
Nov 06, 2021
by
Алдамжарова Ляйла
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавила функционал для добавления и удаления фото.
Сделала проверку на роль, отобразила фото на странице
parent
7470c80c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
1 deletion
+57
-1
PlaceController.java
.../com/example/final_exam_l/controller/PlaceController.java
+22
-0
PlacePhotoService.java
...a/com/example/final_exam_l/service/PlacePhotoService.java
+8
-0
place.ftlh
src/main/resources/templates/place/place.ftlh
+27
-1
No files found.
src/main/java/com/example/final_exam_l/controller/PlaceController.java
View file @
915e8de6
...
...
@@ -113,12 +113,34 @@ public class PlaceController {
return
"redirect:/"
+
id
;
}
@PreAuthorize
(
"hasAuthority('ADMIN')"
)
@PostMapping
(
"/{id}/review/{reviewId}"
)
public
String
deleteReview
(
@PathVariable
Integer
id
,
@PathVariable
Integer
reviewId
){
reviewService
.
delete
(
reviewId
,
id
);
return
"redirect:/"
+
id
;
}
@PreAuthorize
(
"hasAuthority('ADMIN')"
)
@PostMapping
(
"/{id}/review/{reviewId}"
)
public
String
deletePhoto
(
@PathVariable
Integer
id
,
@PathVariable
Integer
reviewId
){
placePhotoService
.
delete
(
reviewId
);
return
"redirect:/"
+
id
;
}
@PreAuthorize
(
"hasAuthority('USER') || hasAuthority('ADMIN')"
)
@PostMapping
(
"/{id}/photo"
)
public
String
addPhoto
(
@PathVariable
Integer
id
,
RedirectAttributes
attributes
,
Principal
principal
,
@RequestParam
(
"file"
)
MultipartFile
file
){
if
(!
service
.
isPhoto
(
file
)
||
file
.
isEmpty
()){
attributes
.
addFlashAttribute
(
"fileError"
,
"Загрузите фото в формате png или jpg"
);
return
"redirect:/add"
;
}
placePhotoService
.
add
(
principal
.
getName
(),
file
.
getOriginalFilename
(),
service
.
findOne
(
id
));
return
"redirect:/"
+
id
;
}
@GetMapping
(
"/file/{filename:.+}"
)
public
ResponseEntity
<
Resource
>
getFilePic
(
@PathVariable
String
filename
)
{
{
...
...
src/main/java/com/example/final_exam_l/service/PlacePhotoService.java
View file @
915e8de6
...
...
@@ -2,6 +2,7 @@ package com.example.final_exam_l.service;
import
com.example.final_exam_l.entity.Place
;
import
com.example.final_exam_l.entity.PlacePhoto
;
import
com.example.final_exam_l.exception.ResourceNotFoundException
;
import
com.example.final_exam_l.repository.PlacePhotoRepository
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.stereotype.Service
;
...
...
@@ -20,4 +21,11 @@ public class PlacePhotoService {
.
build
());
return
newFile
;
}
public
void
delete
(
Integer
reviewId
)
{
PlacePhoto
placePhoto
=
repository
.
findById
(
reviewId
).
orElseThrow
(()
->
{
return
new
ResourceNotFoundException
(
"Фото"
,
reviewId
);
});
repository
.
delete
(
placePhoto
);
}
}
src/main/resources/templates/place/place.ftlh
View file @
915e8de6
...
...
@@ -10,7 +10,17 @@
<p class="info">Создал ${place.user.login}</p>
<p class="info">Рейтинг ${place.rating}</p>
<p class="info">Отзывов ${place.reviews?size}</p>
<p class="info">Фото ${place.photos?size}</p>
<p class="info">Галерея: </p>
<#list place.photos as c>
<img src="/file/${c.filePath}" alt="Изображение">
<#if user?? && user.role == "ADMIN">
<form action="/${place.id}/photo/${c.id}">
<input type='hidden' value='${_csrf.token}' name='${_csrf.parameterName}'/>
<button type="submit" class="btn btn-danger">X</button>
</form>
</#if>
</#list>
<h4>Отзывы</h4>
<#list place.reviews as r>
<p>${r.description}</p>
...
...
@@ -62,4 +72,20 @@
</#if>
</div>
</#if>
<#if user?? && (user.role == "ADMIN" || user.role == "USER")>
<h3>Добавить фото</h3>
<div class="form">
<form action="/${place.id}/photo" method="post" enctype="multipart/form-data">
<input type='hidden' value='${_csrf.token}' name='${_csrf.parameterName}'/>
<div class="col">
<label for="file" class="form-label fs-4">Изображение</label>
<input type="file" name="file" class="form-control" id="file">
</div>
<button type="submit" class="btn btn-primary mt-1" id="btn">Добавить фото</button>
</form>
<#if fileError??>
<h2>${fileError}</h2>
</#if>
</div>
</#if>
</@main.renderWith>
\ No newline at end of file
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