Skip to content
Snippets Groups Projects
Commit 761f616a authored by dawoo's avatar dawoo
Browse files

WateringService in den GardenService integriert.

methode angepasst.
NoCurrentUserException.java gelöscht.
parent faa1e536
No related branches found
No related tags found
1 merge request!25merge WateringLogik into main
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.user.User;
import hdm.mi.growbros.service.GardenService;
......@@ -59,4 +60,14 @@ public class GardenController {
) {
return gardenService.clearGarden(user);
}
@PostMapping ("/getPlantsNeedingWatering")
public ResponseEntity<String> getPlantsNeedingWatring (@AuthenticationPrincipal User user){
return ResponseEntity.ok(gardenService.notifyAboutPlantsNeedingWatering(user));
}
@GetMapping("/setNewWateringFrequency")
public void setNewWateringService(@RequestParam("wateringFrequency") int wateringFrequency , @RequestBody GardenEntry gardenEntry){
gardenEntry.setWateringFrequency(wateringFrequency);
}
}
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.models.user.User;
import hdm.mi.growbros.service.GrandmaService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -17,12 +19,13 @@ public class GrandmaController {
this.grandmaService = grandmaService;
}
@GetMapping("/getgrowingTips")
public ResponseEntity<String> getGrowingTipDesTages() {
return ResponseEntity.ok(grandmaService.growingTippDesTages());
@PostMapping ("/getGrowingTips")
public ResponseEntity<String> getGrowingTipDesTages(@AuthenticationPrincipal User user) {
return ResponseEntity.ok(grandmaService.growingTippDesTages(user));
}
@GetMapping("/getGrowablePlants")
public ResponseEntity<String> getPlantsReadyToGrow(){
return ResponseEntity.ok(grandmaService.notifyAboutPlantsReadyToGrow());
@PostMapping("/getGrowablePlants")
public ResponseEntity<String> getPlantsReadyToGrow(@AuthenticationPrincipal User user){
return ResponseEntity.ok(grandmaService.notifyAboutPlantsReadyToGrow(user));
}
}
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.exceptions.NoCurrentUserException;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.service.WateringService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/Watering")
public class WateringController {
private final WateringService wateringService;
public WateringController (WateringService wateringService){
this.wateringService = wateringService;
}
@GetMapping("/getPlantsNeedingWatering")
public ResponseEntity<String> getPlantsNeedingWatring (){
try {
return ResponseEntity.ok(wateringService.notifyAboutPlantsNeedingWatering());
}catch (NoCurrentUserException noCurrentUserException) {
return ResponseEntity.ok("Kein User ist eingeloggt");
}
}
@GetMapping("/setNewWateringFrequency")
public void setNewWateringService(@RequestParam("wateringFrequency") int wateringFrequency , @RequestParam("GardenEntry")GardenEntry gardenEntry){
wateringService.setWateringFrequency(wateringFrequency,gardenEntry);
}
}
package hdm.mi.growbros.exceptions;
public class NoCurrentUserException extends Exception {
public NoCurrentUserException(String message) {
super(message);
}
}
......@@ -33,8 +33,8 @@ public class GardenEntry {
private LocalDate timeSinceLastWatering;
private int wateringFrequency = 0 ; /*
@Column(columnDefinition = "integer default 0")
private int wateringFrequency; /*
hier Würde der user seine beliebige bewässerungsfrequenz
*/
......@@ -54,16 +54,8 @@ public class GardenEntry {
public int hashCode() {
return Objects.hash(id, plant);
}
public void setTimeSinceLastWatering (LocalDate localDate){
this.timeSinceLastWatering = localDate;
}
public int getWateringFrequency (){
return this.wateringFrequency;
}
public LocalDate getLastWateringDate (){
return this.timeSinceLastWatering;
}
}
......@@ -12,6 +12,8 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
......@@ -84,4 +86,81 @@ public class GardenService {
public int countByUser(User user) {
return gardenRepository.countByUser(user);
}
public List<GardenEntry> getGardenEntriesNeedingWatering(User user) {
List<GardenEntry> plantsNeedingWatering = new ArrayList<>();
List<GardenEntry> plantsInUsersGarden = gardenRepository.findByUser(user);
if(plantsInUsersGarden.isEmpty()){
return plantsNeedingWatering;
}
for( int index = 1 ; index < plantsInUsersGarden.size();index++){
if(plantsInUsersGarden.get(index).getWateringFrequency() == 0 ){
switch (plantsInUsersGarden.get(index).getPlant().getWaterDemand()){
case DRY -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getTimeSinceLastWatering()) > 14) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
case WET -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getTimeSinceLastWatering()) > 7) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
case VERY_WET -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getTimeSinceLastWatering()) > 3) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
}
}else if (getDaysSinceLastWatering(plantsInUsersGarden.get(index).getTimeSinceLastWatering()) > plantsNeedingWatering.get(index).getWateringFrequency()){
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
return plantsNeedingWatering;
}
public String notifyAboutPlantsNeedingWatering(User user) {
List<GardenEntry> plantsNeedingWatering = getGardenEntriesNeedingWatering(user);
StringBuilder notification = new StringBuilder("Diese Pflanzen in deinem Garten brauchen Wasser");
if(!plantsNeedingWatering.isEmpty()){
int size = plantsNeedingWatering.size();
for (int i = 0; i < size; i++) {
notification.append(plantsNeedingWatering.get(i).getPlant().getName());
if (i < size - 2) {
notification.append(", ");
} else if (i == size - 2) {
notification.append(" und ");
}
}
}
return String.valueOf(notification);
}
private long getDaysSinceLastWatering(LocalDate lastWateringDate) {
if (lastWateringDate != null) {
LocalDate currentDate = LocalDate.now();
return ChronoUnit.DAYS.between(lastWateringDate, currentDate);
}
return 0;
}
}
package hdm.mi.growbros.service;
import hdm.mi.growbros.exceptions.NoCurrentUserException;
import hdm.mi.growbros.models.WishListEntry;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.user.User;
import hdm.mi.growbros.repositories.WishListRepository;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
......@@ -23,18 +20,20 @@ public class GrandmaService {
private final WishListRepository wishListRepository;
private final PlantNotificationUtils plantNotificationUtils;
public GrandmaService(WishListRepository wishListRepository, PlantNotificationUtils plantNotificationUtils) {
private final GardenService gardenService;
public GrandmaService(WishListRepository wishListRepository , GardenService gardenService) {
this.wishListRepository = wishListRepository;
this.plantNotificationUtils = plantNotificationUtils;
this.gardenService = gardenService;
}
public String growingTippDesTages() {
List<Plant> usersPlants = plantNotificationUtils.getPlantsInUserGarden();
public String growingTippDesTages(User user) {
List<Plant> usersPlants = gardenService.getUserPlants(user , null);
String tipDesTages;
Random random = new Random();
int randomNumber = random.nextInt(usersPlants.size());
......@@ -43,9 +42,8 @@ public class GrandmaService {
}
public String notifyAboutPlantsReadyToGrow() throws NoCurrentUserException {
try {
List<Plant> plantsReadyToGrow = getPlantsReadyToGrow();
public String notifyAboutPlantsReadyToGrow(User user){
List<Plant> plantsReadyToGrow = getPlantsReadyToGrow(user);
if (plantsReadyToGrow.isEmpty()) {
return "";
}
......@@ -60,28 +58,24 @@ public class GrandmaService {
}
}
return notification.toString();
}catch (NoCurrentUserException noCurrentUserException){
return "";
}
}
private List<Plant> getPlantsReadyToGrow() throws NoCurrentUserException {
private List<Plant> getPlantsReadyToGrow( User user) {
LocalDate today = LocalDate.now();
int weekNumber = today.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear());
List<Plant> plantsReadyToGrow = new ArrayList<>();
try {
User user = plantNotificationUtils.getCurrentUser();
List<WishListEntry> wishListEntries = wishListRepository.findByCurrentPlantingWeek(user, weekNumber);
for (WishListEntry entry : wishListEntries) {
plantsReadyToGrow.add(entry.getPlant());
}
return plantsReadyToGrow;
} catch (NoCurrentUserException noCurrentUserException) {
throw new NoCurrentUserException("kein Nutzer ist angemeldet");
List<WishListEntry> wishListEntries = wishListRepository.findByCurrentPlantingWeek(user, weekNumber);
for (WishListEntry entry : wishListEntries) {
plantsReadyToGrow.add(entry.getPlant());
}
return plantsReadyToGrow;
}
}
package hdm.mi.growbros.service;
import hdm.mi.growbros.exceptions.NoCurrentUserException;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.user.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class PlantNotificationUtils {
private final GardenService gardenService;
public PlantNotificationUtils(GardenService gardenService) {
this.gardenService = gardenService;
}
public List<Plant> getPlantsInUserGarden() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
User user = (User) authentication.getPrincipal();
return gardenService.getUserPlants(user, null);
}
return new ArrayList<>();
}
public User getCurrentUser () throws NoCurrentUserException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
return (User) authentication.getPrincipal();
}else {
throw new NoCurrentUserException("Kein Nutzer ist angemeldet");
}
}
}
package hdm.mi.growbros.service;
import hdm.mi.growbros.exceptions.NoCurrentUserException;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.repositories.GardenRepository;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
@Service
public class WateringService {
private final PlantNotificationUtils plantNotificationUtils;
private final GardenRepository gardenRepository;
public WateringService(PlantNotificationUtils plantNotificationUtils , GardenRepository gardenRepository){
this.gardenRepository = gardenRepository;
this.plantNotificationUtils = plantNotificationUtils;
}
public List<GardenEntry> getGardenEntriesNeedingWatering() throws NoCurrentUserException {
List<GardenEntry> plantsNeedingWatering = new ArrayList<>();
try {
List<GardenEntry> plantsInUsersGarden = gardenRepository.findByUser(plantNotificationUtils.getCurrentUser());
if(!plantsInUsersGarden.isEmpty()){
for( int index = 1 ; index < plantsInUsersGarden.size();index++){
if(plantsInUsersGarden.get(index).getWateringFrequency() == 0 ){
switch (plantsInUsersGarden.get(index).getPlant().getWaterDemand()){
case DRY -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getLastWateringDate()) > 14) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
case WET -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getLastWateringDate()) > 7) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
case VERY_WET -> {
if(getDaysSinceLastWatering(plantsInUsersGarden.get(index).getLastWateringDate()) > 3) {
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
}
}else if (getDaysSinceLastWatering(plantsInUsersGarden.get(index).getLastWateringDate()) > plantsNeedingWatering.get(index).getWateringFrequency()){
plantsNeedingWatering.add(plantsInUsersGarden.get(index));
}
}
}
return plantsNeedingWatering;
}catch (NoCurrentUserException noCurrentUserException){
throw new NoCurrentUserException("Kein User ist eingeloggt");
}
}
public String notifyAboutPlantsNeedingWatering() throws NoCurrentUserException {
List<GardenEntry> plantsNeedingWatering = getGardenEntriesNeedingWatering();
StringBuilder notification = new StringBuilder("Diese Pflanzen in deinem Garten brauchen Wasser");
if(!plantsNeedingWatering.isEmpty()){
int size = plantsNeedingWatering.size();
for (int i = 0; i < size; i++) {
notification.append(plantsNeedingWatering.get(i).getPlant().getName());
if (i < size - 2) {
notification.append(", ");
} else if (i == size - 2) {
notification.append(" und ");
}
}
}
return String.valueOf(notification);
}
private long getDaysSinceLastWatering(LocalDate lastWateringDate) {
if (lastWateringDate != null) {
LocalDate currentDate = LocalDate.now();
return ChronoUnit.DAYS.between(lastWateringDate, currentDate);
}
return 0;
}
public void setWateringFrequency ( int wateringFrequenncy , GardenEntry gardenEntry) {
gardenEntry.setWateringFrequency(wateringFrequenncy);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment