package de.gedoplan.seminar.jpa.exercise.rest; import java.util.List; import java.util.Optional; import org.apache.commons.logging.Log; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.server.ResponseStatusException; import de.gedoplan.seminar.jpa.exercise.domain.Highway; import de.gedoplan.seminar.jpa.exercise.repository.HighwayRepository; @RequestMapping(path = "exercise/highways", produces = MediaType.APPLICATION_JSON_VALUE) public class HighwayResource { // Some test data private Highway testHighwayA1_DO_K = new Highway(4610, "A1", "Dortmund", "Cologne"); private Highway testHighwayA2_DO_H = new Highway(4711, "A2", "Dortmund", "Hannover"); private Highway testHighwayA33_BI_PB = new Highway(4812, "A33", "Bielefeld", "Paderborn"); private List testHighways = List.of(this.testHighwayA1_DO_K, this.testHighwayA2_DO_H, this.testHighwayA33_BI_PB ); private final HighwayRepository highwayRepository; private final Logger logger; public HighwayResource(HighwayRepository highwayRepository, Logger logger) { super(); this.highwayRepository = highwayRepository; this.logger = logger; } /** * Exercise JPA_BASICS_01: Insert test data. */ @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public void insert(@RequestBody Highway highway) { this.logger.debug("----- insert -----"); // this.highwayRepository. this.logger.debug("Inserted: " + testHighways); } /** * Exercise JPA_BASICS_01: Find entries by id. */ @GetMapping("/{ID}") public Highway findById(@PathVariable("ID") Integer id) { this.logger.debug("----- findById -----"); Optional highway = Optional.empty();//this.highwayRepository. highway.ifPresent(h -> this.logger.debug(id+": "+h)); return highway.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); } /** * Exercise JPA_BASICS_03: Find entries by origin. */ @GetMapping("findByOrigin") public Highway findByOrigin(@RequestParam(name = "origin") String origin) { this.logger.debug("----- findByOrigin -----"); Optional highway = Optional.empty();//this.highwayRepository. highway.ifPresent(h -> this.logger.debug(origin+": "+h)); return highway.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); } }