Skip to content
Snippets Groups Projects
Commit f1e13b4d authored by Lukas Karsch's avatar Lukas Karsch
Browse files

#3

started implementation of garden controller and service. GardenEntry model.
parent 5af79d4c
No related branches found
No related tags found
1 merge request!12#3 merge fully implemented garden branch into main
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.service.GardenService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/garden")
@RequiredArgsConstructor
public class GardenController {
private final GardenService gardenService;
@GetMapping("/")
public List<GardenEntry> getPlantsInGarden(@RequestParam(value = "sort", required = false) String sort) {
System.out.println(sort);
return null;
}
@PostMapping("/add/{plantId}")
public void addPlantToGarden(@PathVariable Long plantId) {
gardenService.addPlantToGarden(plantId, 0L);
}
@DeleteMapping("/remove/{entryId}")
public void removePlantFromGarden(@PathVariable Long entryId) {
gardenService.removeFromGarden(entryId);
}
@DeleteMapping("/remove/all")
public Long removeAllPlantsFromGarden() {
return gardenService.clearGarden();
}
}
package hdm.mi.growbros.exceptions;
public class PlantNotFoundException extends RuntimeException {
public PlantNotFoundException(Long id) {
super(String.format("Plant with id '%d' was not found", id));
}
}
package hdm.mi.growbros.models;
import hdm.mi.growbros.models.plant.Plant;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDateTime;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GardenEntry {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Plant plant;
// @ManyToOne(optional = false)
// private User user;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
package hdm.mi.growbros.repositories;
import hdm.mi.growbros.models.GardenEntry;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface GardenRepository extends JpaRepository<GardenEntry, Long> {
}
package hdm.mi.growbros.service;
import hdm.mi.growbros.exceptions.PlantNotFoundException;
import hdm.mi.growbros.models.GardenEntry;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.repositories.GardenRepository;
import hdm.mi.growbros.repositories.PlantRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
@RequiredArgsConstructor
public class GardenService {
private final GardenRepository gardenRepository;
private final PlantRepository plantRepository;
public Collection<Plant> getUserPlants(Long userId) {
return gardenRepository
.findAll()
.stream()
.map(GardenEntry::getPlant)
.toList();
}
public void addPlantToGarden(Long plantId, Long userId) {
//get user
Plant plant = plantRepository.findById(plantId).orElseThrow(() -> new PlantNotFoundException(plantId));
GardenEntry gardenEntry = new GardenEntry();
gardenEntry.setPlant(plant);
gardenRepository.save(gardenEntry);
}
public void removeFromGarden(Long entryId) {
gardenRepository.deleteById(entryId);
}
public Long clearGarden() {
Long deleted = gardenRepository.count();
gardenRepository.deleteAll();
return deleted;
}
}
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