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

Merge branch 'wishList' into 'main'

#7 backend of wishList

See merge request !14
parents 6c47c16b fb2b5188
No related branches found
No related tags found
1 merge request!14#7 backend of wishList
......@@ -25,3 +25,18 @@ content-type: application/json
"email": "lukas.karsch@gmx.de",
"password": "myPassword123"
}
### Get all plants from wishlist
GET http://localhost:8080/api/v1/wishlist
### Get amount of plants in wishlist
GET http://localhost:8080/api/v1/wishlist/count
### Add plant to wishlist
POST http://localhost:8080/api/v1/wishlist/add/1
### Remove plant from wishlist
DELETE http://localhost:8080/api/v1/wishlist/remove/1
### Remove all plants from wishlist
DELETE http://localhost:8080/api/v1/wishlist/remove/all
\ No newline at end of file
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.user.User;
import hdm.mi.growbros.service.WishListService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@CrossOrigin
@RequestMapping("/api/v1/wishlist")
@RequiredArgsConstructor
public class WishListController {
private final WishListService wishListService;
@GetMapping
public Collection<Plant> getAllPlantsInWishList(@RequestParam(value = "sort", required = false) String sort,
@AuthenticationPrincipal User user) {
return wishListService.getWishedPlants(user, sort);
}
@GetMapping("/count")
public int getWishListSize(
@AuthenticationPrincipal User user) {
return wishListService.countByUser(user);
}
@PostMapping("/add/{plantId}")
public ResponseEntity<Integer> addPlantToWishlist(@PathVariable Long plantId,
@AuthenticationPrincipal User user) {
wishListService.addPlantToWishList(plantId, user);
return ResponseEntity.status(201).build();
}
@DeleteMapping("/remove/{entryId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removePlantFromWishlist(@PathVariable Long entryId,
@AuthenticationPrincipal User user) {
wishListService.removeFromWishList(entryId, user);
}
@DeleteMapping("/remove/all")
public int removeAllPlantsFromWishlist(@AuthenticationPrincipal User user) {
return wishListService.clearWishList(user);
}
}
\ No newline at end of file
package hdm.mi.growbros.models;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import java.util.Date;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WishListEntry {
@ManyToOne
@CreatedBy
private User user;
@ManyToOne
private Plant plant;
@CreatedDate
private Date createdAt;
@Id
@GeneratedValue
private Long id;
}
\ No newline at end of file
package hdm.mi.growbros.repositories;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.models.WishListEntry;
import hdm.mi.growbros.models.user.User;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface WishListRepository extends JpaRepository<WishListEntry, Long> {
List<WishListEntry> findByUser(User user);
List<WishListEntry> findByUser(User user, Sort sort);
int countByUser(User user);
int deleteAllByUser(User user);
void deleteByIdAndUser(long entryId, User user);
@Query("""
SELECT ge FROM WishListEntry ge
WHERE ge.user = :user
ORDER BY
CASE
WHEN :week <= ge.plant.plantWeekStart
THEN ABS(:week - ge.plant.plantWeekStart)
ELSE ABS(52 + :week - ge.plant.plantWeekStart)
END,
ge.plant.name
ASC
""")
List<WishListEntry> findAllByNearestPlantingWeek(@Param("user") User user, @Param("week") int currentWeek);
@Query("""
SELECT ge FROM WishListEntry ge
WHERE ge.user = :user
AND ge.plant.plantWeekStart <= :week
AND ge.plant.plantWeekEnd >= :week
""")
List<WishListEntry> findByCurrentPlantingWeek(@Param("user") User user, @Param("week") int currentWeek);
boolean existsByUserAndPlant(User user, Plant plant);
}
\ No newline at end of file
package hdm.mi.growbros.service;
import hdm.mi.growbros.exceptions.PlantNotFoundException;
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.PlantRepository;
import hdm.mi.growbros.repositories.WishListRepository;
import hdm.mi.growbros.util.DateTimeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
@Service
@RequiredArgsConstructor
public class WishListService {
private final WishListRepository wishListRepository;
private final PlantRepository plantRepository;
public List<Plant> getWishedPlants(User user, String sort) {
if (sort == null) return getAllPlantsOrderedByName(user);
return switch (sort) {
case "createdAt" -> getAllPlantsOrderedByCreatedDate(user);
case "plantDate" -> getAllPlantsOrderedByNextPossiblePlantDate(user);
case "currentPlantable" -> getCurrentPlantablePlants(user);
default -> getAllPlantsOrderedByName(user);
};
}
public void addPlantToWishList(Long plantId, User user) {
Plant plant = plantRepository.findById(plantId).orElseThrow(() -> new PlantNotFoundException(plantId));
if (wishListRepository.existsByUserAndPlant(user, plant)) {
return;
}
WishListEntry wishListEntry = new WishListEntry();
wishListEntry.setPlant(plant);
wishListEntry.setUser(user);
wishListRepository.save(wishListEntry);
}
public void removeFromWishList(Long entryId, User user) {
wishListRepository.deleteByIdAndUser(entryId, user);
}
public int clearWishList(User user) {
return wishListRepository.deleteAllByUser(user);
}
private List<Plant> getAllPlantsOrderedByName(User user) {
return mapWishListEntriesToPlants(
wishListRepository.findByUser(user, Sort.by("plant.name"))
);
}
private List<Plant> getAllPlantsOrderedByCreatedDate(User user) {
return mapWishListEntriesToPlants(
wishListRepository.findByUser(user, Sort.by("createdAt"))
);
}
private List<Plant> getAllPlantsOrderedByNextPossiblePlantDate(User user) {
return mapWishListEntriesToPlants(
wishListRepository.findAllByNearestPlantingWeek(user, DateTimeUtils.getCurrentWeekForToday())
);
}
private List<Plant> getCurrentPlantablePlants(User user) {
return mapWishListEntriesToPlants(
wishListRepository.findByCurrentPlantingWeek(user, DateTimeUtils.getCurrentWeekForToday())
);
}
private List<Plant> mapWishListEntriesToPlants(Collection<WishListEntry> entries) {
return entries
.stream()
.map(WishListEntry::getPlant)
.toList();
}
public int countByUser(User user) {
return wishListRepository.countByUser(user);
}
}
\ No newline at end of file
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