ItemRatingController.java
2.98 KB
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
package com.srh.api.controller;
import com.srh.api.dto.resource.ItemRatingDto;
import com.srh.api.dto.resource.ItemRatingForm;
import com.srh.api.hypermedia.ItemRatingModelAssembler;
import com.srh.api.model.ItemRating;
import com.srh.api.service.ItemRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.net.URI;
import static com.srh.api.dto.resource.ItemRatingDto.convert;
@RestController
@RequestMapping("/itemratings")
public class ItemRatingController {
@Autowired
private ItemRatingService itemRatingService;
@Autowired
private ItemRatingModelAssembler itemRatingModelAssembler;
@Autowired
PagedResourcesAssembler<ItemRatingDto> pagedResourcesAssembler;
@GetMapping
public PagedModel<EntityModel<ItemRatingDto>> listAll(@PageableDefault(page = 0, size = 5)
Pageable pageInfo) {
Page<ItemRating> itemRatings = itemRatingService.findAll(pageInfo);
return pagedResourcesAssembler.toModel(convert(itemRatings));
}
@GetMapping("/{id}")
public EntityModel<ItemRatingDto> find(@PathVariable Integer id) {
ItemRating itemRating = itemRatingService.find(id);
return itemRatingModelAssembler.toModel(new ItemRatingDto(itemRating));
}
@PostMapping
public ResponseEntity<EntityModel<ItemRatingDto>> create(@RequestBody @Valid ItemRatingForm itemRatingForm,
UriComponentsBuilder uriBuilder) {
ItemRating itemRating = itemRatingForm.build();
itemRatingService.save(itemRating);
URI uri = uriBuilder.path("/itemratings/{id}").buildAndExpand(itemRating.getId()).toUri();
return ResponseEntity.created(uri)
.body(itemRatingModelAssembler.toModel(new ItemRatingDto(itemRating)));
}
@PutMapping("/{id}")
@Transactional
public EntityModel<ItemRatingDto> update(@RequestBody @Valid ItemRatingForm itemRatingForm,
@PathVariable Integer id) {
ItemRating itemRating = itemRatingForm.build();
itemRating.setId(id);
itemRating = itemRatingService.update(itemRating);
return itemRatingModelAssembler.toModel(new ItemRatingDto(itemRating));
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable Integer id) {
itemRatingService.delete(id);
return ResponseEntity.noContent().build();
}
}