Skip to content

Commit

Permalink
Merge pull request #53 from jkuznik/Task-44-Active-events-search-fron…
Browse files Browse the repository at this point in the history
…tend

Task-44-Active-events-search-frontend
  • Loading branch information
Slawek84PL authored Nov 23, 2024
2 parents 22f340c + 033e837 commit 3f0628f
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterDTO;

import java.util.List;
import java.util.Optional;

@Component
@Validated
@RequiredArgsConstructor
Expand All @@ -18,4 +21,8 @@ public class DisasterAlertFacade {
public DisasterDTO createDisaster(@NotNull @Valid DisasterAddDTO disasterAddDTO, @NotNull @NotBlank String source) {
return disasterService.createDisaster(disasterAddDTO, source);
}

public List<DisasterDTO> interestingDisasters(Optional<DisasterType> type, Optional<String> location) {
return disasterService.interestingDisasters(type, location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

interface DisasterRepository extends JpaRepository<Disaster, UUID> {
Optional<Disaster> findById(UUID id);
Optional<Disaster> findFirstByTypeAndLocationAndStatus(DisasterType type, String location, DisasterStatus status);
List<Disaster> findAllByStatus(DisasterStatus status);
List<Disaster> findAllByType(DisasterType type);
List<Disaster> findAllByLocation(String location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterDTO;

import java.util.List;
import java.util.Optional;

@Validated
interface DisasterService {
DisasterDTO createDisaster(@NotNull @Valid DisasterAddDTO disasterAddDTO, @NotNull @NotBlank String source);
Optional<DisasterDTO> getActiveDisasterForTypeAndLocation(@NotNull @Valid DisasterType type, String location);
Optional<DisasterDTO> getActiveDisasterForTypeAndLocation(@NotNull @Valid DisasterType type, @NotNull @NotBlank String location);

List<DisasterDTO> interestingDisasters(Optional<DisasterType> type, Optional<String> location);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service(value = "prototype")
@RequiredArgsConstructor
Expand All @@ -24,6 +27,7 @@ class DisasterServiceImpl implements DisasterService {
public DisasterDTO createDisaster(DisasterAddDTO disasterAddDTO, String source) {
Disaster disaster = mapper.mapDisasterAddDtoToDisaster(disasterAddDTO);
disaster.setSource(source);
//TODO: trzeba dopracować promt do czata bo przy obecnym trudno osiągnąć wynik pozwalający na uznanie zdarzenia za prawdziwe
// if (riskAssessment.assessRisk(disasterAddDTO)) {
// disaster.setStatus(DisasterStatus.ACTIVE);
// disasterRepository.save(disaster);
Expand Down Expand Up @@ -60,4 +64,31 @@ public Optional<DisasterDTO> getActiveDisasterForTypeAndLocation(DisasterType ty
return disasterRepository.findFirstByTypeAndLocationAndStatus(type, location, DisasterStatus.ACTIVE)
.map(mapper::mapDisasterToDisasterDto);
}

@Override
public List<DisasterDTO> interestingDisasters(Optional<DisasterType> type, Optional<String> location) {
//TODO: poniższa logika jest nie optymalna, do poprawy po zakończeniu konkursu

List<DisasterDTO> interestedDisasters = disasterRepository.findAllByStatus(DisasterStatus.ACTIVE).stream()
.map(mapper::mapDisasterToDisasterDto)
.collect(Collectors.toList());

if (type.isPresent()) {
List<DisasterDTO> byType = disasterRepository.findAllByType(type.get()).stream()
.map(mapper::mapDisasterToDisasterDto)
.collect(Collectors.toList());

interestedDisasters.retainAll(byType);
}

if (location.isPresent()) {
List<DisasterDTO> byLocation = disasterRepository.findAllByLocation(location.get()).stream()
.map(mapper::mapDisasterToDisasterDto)
.collect(Collectors.toList());

interestedDisasters.retainAll(byLocation);
}

return interestedDisasters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record DisasterDTO(UUID id,
String description,
String source,
String location,
LocalDateTime creationDate,
LocalDateTime createDate,
LocalDateTime disasterEndTime,
DisasterStatus status,
UUID userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pl.ateam.disasteralerts.disasteralert.DisasterAlertFacade;
import pl.ateam.disasteralerts.disasteralert.DisasterStatus;
import pl.ateam.disasteralerts.disasteralert.DisasterType;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO;
import pl.ateam.disasteralerts.disasteralert.dto.DisasterDTO;
import pl.ateam.disasteralerts.security.AppUser;
import pl.ateam.disasteralerts.util.CitiesInPoland;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Controller
@RequiredArgsConstructor
@RequestMapping("/disasters")
Expand Down Expand Up @@ -56,6 +63,43 @@ public String createDisaster(Model model, @AuthenticationPrincipal AppUser userD
return "redirect:/disasters/add";
}

@GetMapping("list")
public String showDisasterList(Model model, @AuthenticationPrincipal AppUser userDetails) {
baseModel(model, userDetails);

if (!model.containsAttribute("list")) {
model.addAttribute("list", getDisasterDTOS("Wszystkie", "Wszystkie"));
}
return "listDisasters";
}

@PostMapping("list")
public String filterList(@RequestParam(name = "disasterType") String disasterType,
@RequestParam(name = "city") String city,
RedirectAttributes redirectAttributes) {

redirectAttributes.addFlashAttribute("list", getDisasterDTOS(disasterType, city));
return "redirect:/disasters/list";
}

private List<DisasterDTO> getDisasterDTOS(String disasterType, String city) {
Optional<DisasterType> type;
if (disasterType.isEmpty() || disasterType.equals("Wszystkie")){
type = Optional.empty();
} else {
type = Optional.of(DisasterType.valueOf(disasterType));
}

Optional<String> location;
if (city.isEmpty() || city.equals("Wszystkie")){
location = Optional.empty();
} else {
location = Optional.of(city);
}

return disasterAlertFacade.interestingDisasters(type, location);
}

private void baseModel(Model model, AppUser userDetails) {
model.addAttribute("userId", userDetails.getUserDTO().id());
model.addAttribute("disasterType", DisasterType.values());
Expand Down
Binary file added src/main/resources/static/images/favicon.ico
Binary file not shown.
37 changes: 24 additions & 13 deletions src/main/resources/templates/addDisaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

<head th:replace="~{fragments :: head('Dodaj Zdarzenie')}"></head>

<body>
<body class="bg-body-tertiary">
<nav th:replace="~{fragments :: nav}"></nav>

<div class="d-flex vh-100 align-items-center justify-content-center bg-body-tertiary">
<div class="container col-xl-10 col-xxl-8 px-4 py-5">
<div class="row align-items-center g-lg-5 py-5">
<div class="col-lg-7 text-center text-lg-start">
<h1 class="display-4 fw-bold lh-1 text-body-emphasis mb-3">Dodaj Zdarzenie</h1>
<p class="col-lg-10 fs-4">Wprowadź informacje o nowym zdarzeniu</p>
</div>
<div class="col-md-10 mx-auto col-lg-5">
<div th:replace="~{fragments :: message}"></div>
<form action="/disasters/add" method="post" th:object="${disasterAddDTO}" class="p-4 p-md-5 rounded-3 bg-body-tertiary">
<div class="px-4 py-5 mt-5 text-center bg-white shadow-sm">
<h1 class="display-5 fw-bold text-body-emphasis">Dodaj <span class="text-primary">Zdarzenie</span></h1>
<div class="col-lg-6 mx-auto">
<p class="lead">Wprowadź informacje o nowym zdarzeniu. Pamiętaj, że fałszywe zgłoszenia zostaną wykryte.</p>
</div>
</div>
<div>
<div class="container col-xl-10 col-xxl-10 px-4 ">
<div class="row g-lg-5 py-5">
<div class="col-md-10 mx-auto col-lg-10">
<form action="/disasters/add" method="post" th:object="${disasterAddDTO}" class="p-4 p-md-2 rounded-3 bg-body-tertiary">
<div class="form-floating mb-3">
<select th:value="${disasterTypSelected}" name="type" class="form-select" id="type" required>
<option th:if="${disasterTypSelected == null}" value="" disabled selected>Wybierz typ zdarzenia</option>
Expand All @@ -37,7 +37,18 @@ <h1 class="display-4 fw-bold lh-1 text-body-emphasis mb-3">Dodaj Zdarzenie</h1>
<input th:value="${userId}" name="userId" type="hidden" class="form-control" id="userId" required readonly>
</div>

<button class="w-100 btn btn-lg btn-success" type="submit">Dodaj Zdarzenie</button>
<button class="w-100 btn btn-lg btn-primary" type="submit">Dodaj Zdarzenie</button>
<div id="toastMessage" class="toast rounded align-items-center border-0 position-fixed bottom-0 end-0 m-3 show" role="alert" aria-live="assertive" aria-atomic="true" th:if="${message}">
<div class="toast-header">
<svg class="bd-placeholder-img rounded me-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" preserveAspectRatio="xMidYMid slice" focusable="false"><rect width="100%" height="100%" fill="#007aff"/></svg>
<strong class="me-auto">Udało się!</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="d-flex text-secondary bg-light px-3 py-1 fw-light">
<div th:replace="~{fragments :: message}" class="toast-body text-white">
</div>
</div>
</div>
</form>
</div>
</div>
Expand Down
Loading

0 comments on commit 3f0628f

Please sign in to comment.