Добавила функционал для добавления и удаления фото.

Сделала проверку на роль, отобразила фото на странице
parent 7470c80c
......@@ -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) {
{
......
......@@ -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);
}
}
......@@ -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
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