Hendrik Jungnitsch
2022-08-26 05744839ae85d45f557bf9686db8b25a5456a54f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package de.gedoplan.seminar.jpa.exercise.rest;
 
import java.util.Optional;
 
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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;
 
@Controller
@RequestMapping(path = "/highways", produces = MediaType.APPLICATION_JSON_VALUE)
public class HighwayResource {
 
 
  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: " + highway);
  }
 
  /**
   * Exercise JPA_BASICS_01: Find entries by id.
   */
  @GetMapping("/{ID}")
  public Highway findById(@PathVariable("ID") Integer id) {
    this.logger.debug("----- findById -----");
    Optional<Highway> 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> highway = Optional.empty();//this.highwayRepository.
    highway.ifPresent(h -> this.logger.debug(origin+": "+h));
    return highway.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
  }
 
}