Skip to content
Snippets Groups Projects
Commit 31076fec authored by dawoo's avatar dawoo
Browse files

Grandma Logic verbessert, Tipp des Tages sowie notifikation über Pflanze in...

Grandma Logic verbessert, Tipp des Tages sowie notifikation über Pflanze in der eigenen Wunschliste, die man jetzt einpflanzen kann, sind jetzt möglich
parent 6c8284c2
No related branches found
No related tags found
1 merge request!23Grandma Logic verbessert, Tipp des Tages sowie notifikation über Pflanze in...
......@@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/randomPlantInfo")
@RequestMapping("/api/v1/randomPlantInfo")
public class GrandmaController {
private final GrandmaService grandmaService;
......@@ -18,8 +18,11 @@ public class GrandmaController {
}
@GetMapping
public ResponseEntity<String> getRandomPlantInfo() {
String randomPlantInfo = grandmaService.getRandomPlantInfo();
return ResponseEntity.ok(randomPlantInfo);
public ResponseEntity<String> getGrowingTipDesTages() {
return ResponseEntity.ok(grandmaService.growingTippDesTages());
}
@GetMapping
public ResponseEntity<String> getPlantsReadyToGrow(){
return ResponseEntity.ok(grandmaService.notifyAboutPlantsReadyToGrow());
}
  • du hast 2 mal @GetMapping hier, das ist ein Problem weil du jetzt für eine URL ("/api/v1/randomPlantInfo") zwei verschiedene Methoden aufrufen willst. Also musst du bei beiden @GetMapping 's noch einen weiteren Identifier hinzufügen. Über die Klasse würde ich vielleicht @RequestMapping("/api/v1/Grandma") schreiben, und bei den beiden Methoden dann @GetMapping("/growingTip") und @GetMapping("/growablePlants"). oder so ähnlich

  • Please register or sign in to reply
}
......@@ -22,7 +22,6 @@ import java.util.List;
@AllArgsConstructor
@Entity
@Table(name = "_user")
@Component
public class User implements UserDetails {
@Id
@GeneratedValue
......
package hdm.mi.growbros.service;
import hdm.mi.growbros.models.WishListEntry;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.repositories.PlantRepository;
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;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
@Service
public class GrandmaService {
private final PlantRepository plantRepository;
public GrandmaService (PlantRepository plantRepository){
this.plantRepository = plantRepository;
}
private final GardenService gardenService;
private final WishListRepository wishListRepository;
public GrandmaService (GardenService gardenService , WishListRepository wishListRepository){
public String getRandomPlantInfo() {
this.gardenService = gardenService;
this.wishListRepository = wishListRepository;
String plantInfo = null;
List<Plant> plants = plantRepository.findAll();
}
public String growingTippDesTages (){
List<Plant> usersPlants = getPlantsInUserGarden();
String tipDesTages;
Random random = new Random();
int randomNumber = random.nextInt(plants.size());
Plant randomPLant = plants.get(randomNumber);
int randomNumber2 = random.nextInt(5);
switch (randomNumber2){
case 0:
plantInfo = "Did u know that " + randomPLant.getName() + "Is a " + randomPLant.getDescription();
break;
case 1:
plantInfo = "Did u know that " + randomPLant.getName() + "is originally from " + randomPLant.getOrigin();
break;
case 2:
plantInfo = "Did u know that " + randomPLant.getName() + "is Planted from " + randomPLant.getPlantWeekStart() + "until " + randomPLant.getPlantWeekEnd();
break;
case 3:
plantInfo = "Did u know that " + randomPLant.getName() + "is Harvested from " + randomPLant.getHarvestWeekStart() + "until " + randomPLant.getHarvestWeekEnd();
break;
case 4:
plantInfo = "Did u know that " + randomPLant.getName() + "is Planted in " + randomPLant.getGrowingTips();
break;
int randomNumber = random.nextInt(usersPlants.size());
tipDesTages = "Tip des Tages für deinen Garten" + usersPlants.get(randomNumber).getGrowingTips();
return tipDesTages;
}
public List<Plant> getPlantsInUserGarden() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
User user = (User) authentication.getPrincipal();
return gardenService.getUserPlants(user, null);
}
return plantInfo;
return null;
  • ich finde, dass null zu returnen immer schwierig ist, vor allem wenn das an den client geht. im optimalfall wäre einfach ein leeres objekt eine alternative. du könntest also im package model.dto eine Klasse bzw. ein Record anlegen, die zb "GrowingTipDto" heißt (DTO steht für Data transfer object, also ein objekt, das man nur erstellt um daten hinzuzufügen, die an den user geschickt werden). Und in dieses Objekt dann den Tip, oder einen leeren String hinzufügen. wenns dazu fragen gibt dann schreib mir

  • Please register or sign in to reply
}
public List<Plant> getPlantsReadyToGrow(){
LocalDate today = LocalDate.now();
int weekNumber = today.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
User user = (User) authentication.getPrincipal();
List<WishListEntry> wishListEntries = wishListRepository.findByCurrentPlantingWeek(user,weekNumber);
List<Plant> plantsReadyToGrow = new ArrayList<>();
for (WishListEntry entry : wishListEntries) {
plantsReadyToGrow.add(entry.getPlant());
return plantsReadyToGrow; }
}
return null;
  • hier das gleiche, null returnen ist nie gut. wenns keine pflanzen gibt, die man aktuell anbauen kann, dann gib lieber eine leere liste zurück

  • Please register or sign in to reply
}
public String notifyAboutPlantsReadyToGrow () {
List<Plant> plantsReadyToGrow = getPlantsReadyToGrow();
if (plantsReadyToGrow.isEmpty()) {
return null;
}
StringBuilder notification = new StringBuilder("Diese Pflanzen in deiner Wunschliste wären bereit zum Einpflanzen: ");
int size = plantsReadyToGrow.size();
for (int i = 0; i < size; i++) {
notification.append(plantsReadyToGrow.get(i).getName());
if (i < size - 2) {
notification.append(", ");
} else if (i == size - 2) {
notification.append(" und ");
}
}
return notification.toString();
  • für die notification eventuell auch ein DTO erstellen, is aber nur ein vorschlag das wäre wahrscheinlich nicht so relevant

  • Please register or sign in to reply
}
}
  • Owner

    und methoden, die du nur innerhalb vom service benutzt, am besten private machen ;)

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