Skip to content
Snippets Groups Projects
Commit 997b8506 authored by Blersch Lara's avatar Blersch Lara
Browse files

update: WishList #7

parent 020c95b4
No related branches found
No related tags found
1 merge request!14#7 backend of wishList
package hdm.mi.growbros.controllers; package hdm.mi.growbros.controllers;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.service.PlantsService;
import hdm.mi.growbros.service.WishListService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.service.PlantsService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@CrossOrigin
public class WishListController { public class WishListController {
private final WishListService wishListService;
public WishListController(WishListService wishListService) {
this.wishListService = wishListService;
}
@GetMapping("/api/v1/wishlist")
public Collection<Plant> getAllPlants() {
return wishListService.getWishedPlants();
}
} }
package hdm.mi.growbros.models.plant;
import org.springframework.cglib.core.Local;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
public class WishList {
private Map<Long, LocalDate> wishList = new HashMap<>();
public void WishList () {
}
private void addPlant (Long plantID) {
wishList.put(plantID, LocalDate.now());
//log plant added to wishlist
}
private void removePlant (Long plantID) {
wishList.remove(plantID);
//log plant removed from wishlist
}
private Map<Long, LocalDate> sortList (String sortKey) {
Map<Long, LocalDate> sortedWishList = new HashMap<>();
switch (sortKey) {
case ("name"):
;
case ("date"):
;
case ("plantWeek"):
;
}
return sortedWishList;
}
}
package hdm.mi.growbros.models.plant;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDate;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WishListEntry {
//Todo: private User user;
@ManyToOne
private Plant plant;
@LastModifiedDate
@Temporal(TemporalType.TIME)
private LocalDate addedOn;
@Id
private Long id;
}
package hdm.mi.growbros.repositories; package hdm.mi.growbros.repositories;
public interface WishListRepository { import hdm.mi.growbros.models.plant.Plant;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
@Repository
public interface WishListRepository extends JpaRepository<Plant, Long> {
} }
package hdm.mi.growbros.service; package hdm.mi.growbros.service;
import hdm.mi.growbros.models.plant.Plant;
import hdm.mi.growbros.repositories.WishListRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
@Service
@RequiredArgsConstructor
public class WishListService { public class WishListService {
private final WishListRepository wishListRepository;
public Collection<Plant> getWishedPlants() {
return wishListRepository.findAll();
}
public List<Plant> sort(String sortKey) {
switch (sortKey) {
case ("name"):
Sort sortByName = Sort.by(Sort.Direction.ASC, "name");
return wishListRepository.findAll(sortByName);
case ("addedOn"):
Sort sortByDate = Sort.by(Sort.Direction.ASC, "addedOn");
return wishListRepository.findAll(sortByDate);
case ("plantWeek"):
int currentWeek = getWeekNum(LocalDate.now());
Sort sortByPlantWeek = Sort.by(Sort.Direction.ASC, "plantWeekStart");
return wishListRepository.findAll(sortByPlantWeek);
default:
return wishListRepository.findAll();
}
}
public int getWeekNum(LocalDate date) {
WeekFields wf = WeekFields.of(Locale.getDefault()) ; // Use week fields appropriate to your locale. People in different places define a week and week-number differently, such as starting on a Monday or a Sunday, and so on.
TemporalField weekNum = wf.weekOfWeekBasedYear(); // Represent the idea of this locale’s definition of week number as a `TemporalField`.
int week = Integer.parseInt(String.format("%02d", date.get(weekNum))); // Using that locale’s definition of week number, determine the week-number for this particular `LocalDate` value.
return week-1; //because in class Plant weeks are indexed at 0
}
} }
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