Hendrik Jungnitsch
2022-09-28 6e6d835460000277b0a860f83a0419acec1b5d74
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
71
72
73
74
75
76
77
78
79
80
81
82
83
package de.gedoplan.seminar.sbt.di.exercise.rest;
 
import de.gedoplan.seminar.sbt.di.exercise.domain.CocktailOrder;
import de.gedoplan.seminar.sbt.di.exercise.exception.OutOfStockException;
import de.gedoplan.seminar.sbt.di.exercise.repository.CocktailOrderRepository;
import de.gedoplan.seminar.sbt.di.exercise.service.CocktailOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.net.URI;
 
@RequestMapping(path = "mixer", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class MixerEndpoint {
 
  @Autowired
  CocktailOrderService cocktailOrderService;
 
  @Autowired
  CocktailOrderRepository cocktailOrderRepository;
 
 
  @PostMapping(path = "order",produces = MediaType.TEXT_PLAIN_VALUE)
  public ResponseEntity<String> postOrder(UriComponentsBuilder uriComponentsBuilder) {
    Integer newId = this.cocktailOrderService.createNewOrder();
    return ResponseEntity
        .created(uriComponentsBuilder
                .pathSegment("api","mixer","order", newId.toString())
                .build().toUri())
        .build();
  }
 
  @GetMapping(value = "order/{id}")
  public CocktailOrder getOrder(@PathVariable Integer id) {
    return this.cocktailOrderRepository.findById(id)
            .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
  }
 
  @PostMapping(path = "order/{id}/{cocktailId}", produces = MediaType.TEXT_PLAIN_VALUE)
  public ResponseEntity<String> addCocktail(@PathVariable Integer id, @PathVariable String cocktailId) {
    try {
      this.cocktailOrderService.addCocktail(id, cocktailId);
 
      return ResponseEntity
          .status(HttpStatus.CREATED)
          .build();
    } catch (IllegalArgumentException e) {
      return ResponseEntity
          .notFound()
          .build();
    } catch (OutOfStockException e) {
      return ResponseEntity
              .status(HttpStatus.CONFLICT)
          .body(e.getMessage());
    }
  }
 
 
  @PutMapping("order/{id}/placed")
  public ResponseEntity<String> setPlaced(@PathVariable Integer id) {
    try {
      this.cocktailOrderService.placeOrder(id);
 
      return ResponseEntity.noContent().build();
    } catch (IllegalArgumentException e) {
      return ResponseEntity
          .notFound()
          .build();
    }
 
  }
 
 
  @DeleteMapping("order/{id}")
  public void delete(@PathVariable Integer id) {
    this.cocktailOrderService.cancelOrder(id);
  }
}