Skip to content

Commit

Permalink
Merge branch 'master' into task-35-update-front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiHeike authored Nov 24, 2024
2 parents efc7ef1 + 3f0628f commit 2ee42c7
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 87 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.
4 changes: 4 additions & 0 deletions src/main/resources/templates/addDisaster.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

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

<div class="px-4 py-5 mt-md-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>
Expand All @@ -14,7 +16,9 @@ <h1 class="display-5 fw-bold text-body-emphasis">Dodaj <span class="text-primary
<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-8">

<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>
Expand Down
149 changes: 87 additions & 62 deletions src/main/resources/templates/fragments.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">

<head th:fragment="head(pageTitle)">
<meta charset="UTF-8">
<title th:text="*{pageTitle}">DisasterAlerts</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
<body>
<head th:fragment="head(pageTitle)">
<meta charset="UTF-8">
<title th:text="*{pageTitle}">DisasterAlerts</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<link rel="icon" href="/images/favicon.ico" type="image/x-icon">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</head>
<body>

<nav th:fragment="nav" class="navbar navbar-dark bg-dark text-sm-center" aria-label="First navbar example">
<div class="container-fluid">
<a sec:authorize="isAnonymous()" class="navbar-brand" href="/">
<img class="d-inline-block me-3" style="max-height: 3.5em;" src="/images/disaster_alerts_logo.bmp" alt="logo" />
<p class="d-inline-block display-6 align-middle">Disaster alerts</p>
</a>
<a sec:authorize="isAuthenticated()" class="navbar-brand" href="/disasters/add">
<img class="d-inline-block me-3" style="max-height: 3.5em;" src="/images/disaster_alerts_logo.bmp" alt="logo" />
<p class="d-inline-block display-6 align-middle">Disaster alerts</p>
</a>
<nav th:fragment="nav" class="navbar navbar-expand-sm bg-dark navbar-dark" aria-label="First navbar example">
<div class="container-fluid">
<a sec:authorize="isAnonymous()" class="navbar-brand" href="/">
<img class="d-inline-block me-3" style="max-height: 3.5em;" src="/images/disaster_alerts_logo.bmp"
alt="logo"/>
<p class="d-inline-block display-6 align-middle">Disaster alerts</p>
</a>
<a sec:authorize="isAuthenticated()" class="navbar-brand" href="/disasters/add">
<img class="d-inline-block me-3" style="max-height: 3.5em;" src="/images/disaster_alerts_logo.bmp"
alt="logo"/>
<p class="d-inline-block display-6 align-middle">Disaster alerts</p>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse nav justify-content-end" id="collapsibleNavbar">
<div sec:authorize="isAnonymous()">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="/">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/signup">Register</a>
</li>
</ul>
</div>
<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link text-light" href="/">Login</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" href="/signup">Register</a>
</li>
</ul>
</div>
<div sec:authorize="isAuthenticated()">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="/logout">Logout</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/users/edit">Edytuj dane</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/disasters/add">Dodaj zdarzenie</a>
</li>
</ul>
</div>
<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link text-light" href="/disasters/add">Dodaj zdarzenie</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" href="/disasters/list">Sprawdź zdarzenia</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" href="/users/edit">Edytuj dane</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" href="/logout">Logout</a>
</li>
</ul>
</div>
</div>
</nav>

Expand All @@ -61,21 +75,18 @@
</div>
</div>
</div>
</nav>

<div th:fragment="citiesList" class="col-12">
<div class="text-danger" th:if="${#fields.errors('location')}" th:errors="*{location}"></div>
<h2>Wybierz miejscowość:</h2>
<div class="form-group">
<input th:value="${selectedLocation}" type="text" class="form-control" id="cityInput" placeholder="Zacznij wpisywać" onkeyup="filterCities()">
<div th:fragment="message" class="px-2 text-center">
<p class="h5 text-center" th:if="${message} != null" th:text="${message}"></p>
</div>

<select class="form-select" id="location" name="location" size="5">
<option th:each="location : ${cities}"
th:value="${location}"
th:text="${location}"
th:selected="${location == selectedLocation}">
</option>
</select>
</div>
<div th:fragment="citiesList" class="col-12">
<div class="text-danger" th:if="${#fields.errors('location')}" th:errors="*{location}"></div>
<h4 class="text-center">Wybierz miejscowość:</h4>
<div class="form-group">
<input th:value="${selectedLocation}" type="text" class="form-control my-2" id="cityInput"
placeholder="Zacznij wpisywać" onkeyup="filterCities()">



Expand All @@ -84,18 +95,32 @@ <h2>Wybierz miejscowość:</h2>
let input = document.getElementById('cityInput').value.toLowerCase();
let dropdown = document.getElementById('location');
let options = dropdown.getElementsByTagName('option');
<select class="form-select" id="location" name="location" size="5">
<option th:each="location : ${cities}"
th:value="${location}"
th:text="${location}"
th:selected="${location == selectedLocation}">
</option>
</select>
</div>

for (let i = 0; i < options.length; i++) {
let txtValue = options[i].textContent || options[i].innerText;
if (txtValue.toLowerCase().indexOf(input) > -1) {
options[i].style.display = "";
} else {
options[i].style.display = "none";
}
<script>
function filterCities() {
let input = document.getElementById('cityInput').value.toLowerCase();
let dropdown = document.getElementById('location');
let options = dropdown.getElementsByTagName('option');

for (let i = 0; i < options.length; i++) {
let txtValue = options[i].textContent || options[i].innerText;
if (txtValue.toLowerCase().indexOf(input) > -1) {
options[i].style.display = "";
} else {
options[i].style.display = "none";
}
}
</script>
</div>
}
</script>
</div>

</body>
</html>
Loading

0 comments on commit 2ee42c7

Please sign in to comment.