WIP: Hateoas #245

Closed
romanov73 wants to merge 1 commits from hateoas into dev

@ -105,6 +105,8 @@ dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-hateoas', version: '2.2.1.RELEASE'
compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5' compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner'
@ -132,4 +134,4 @@ dependencies {
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-support', version: '3.3.1' testCompile group: 'org.seleniumhq.selenium', name: 'selenium-support', version: '3.3.1'
//testCompile group: 'com.google.guava', name: 'guava', version: '21.0' //testCompile group: 'com.google.guava', name: 'guava', version: '21.0'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
} }

@ -1,5 +1,8 @@
package ru.ulstu.paper.controller; package ru.ulstu.paper.controller;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -20,6 +23,8 @@ import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import static org.springframework.hateoas.core.DummyInvocationUtils.methodOn;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static ru.ulstu.paper.controller.PaperRestController.URL; import static ru.ulstu.paper.controller.PaperRestController.URL;
@RestController @RestController
@ -38,6 +43,27 @@ public class PaperRestController {
return new Response<>(paperService.findAllDto()); return new Response<>(paperService.findAllDto());
} }
@GetMapping("/with-resources")
public Resources<List<PaperDto>> getPapersWithResources() {
List<PaperDto> paperDtos = paperService.findAllDto();
for (final PaperDto paperDto : paperDtos) {
final Link selfLink = linkTo(
methodOn(PaperRestController.class).getPaperWithResources(paperDto.getEntityId())).withSelfRel();
paperDto.add(selfLink);
}
Link link = linkTo(PaperRestController.class).withSelfRel();
Resources<List<PaperDto>> result = new Resources(paperDtos, link);
return result;
}
@GetMapping("/with-resources/{paper-id}")
public Resource<PaperDto> getPaperWithResources(@PathVariable("paper-id") Integer paperId) {
Link link = linkTo(methodOn(PaperRestController.class).getPapersWithResources()).withSelfRel();
Resource<PaperDto> result = new Resource(paperService.findById(paperId), link);
return result;
}
@GetMapping("/{paper-id}") @GetMapping("/{paper-id}")
public Response<PaperDto> getPaper(@PathVariable("paper-id") Integer paperId) { public Response<PaperDto> getPaper(@PathVariable("paper-id") Integer paperId) {
return new Response<>(paperService.findById(paperId)); return new Response<>(paperService.findById(paperId));

@ -3,6 +3,7 @@ package ru.ulstu.paper.model;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.hateoas.ResourceSupport;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
@ -17,10 +18,10 @@ import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
public class PaperDto { public class PaperDto extends ResourceSupport {
private final static int MAX_AUTHORS_LENGTH = 60; private final static int MAX_AUTHORS_LENGTH = 60;
private Integer id; private Integer entityId;
@NotEmpty @NotEmpty
@Size(min = 3, max = 254) @Size(min = 3, max = 254)
private String title; private String title;
@ -46,7 +47,7 @@ public class PaperDto {
} }
@JsonCreator @JsonCreator
public PaperDto(@JsonProperty("id") Integer id, public PaperDto(@JsonProperty("entityId") Integer entityId,
@JsonProperty("title") String title, @JsonProperty("title") String title,
@JsonProperty("status") Paper.PaperStatus status, @JsonProperty("status") Paper.PaperStatus status,
@JsonProperty("type") Paper.PaperType type, @JsonProperty("type") Paper.PaperType type,
@ -62,7 +63,7 @@ public class PaperDto {
@JsonProperty("authors") Set<UserDto> authors, @JsonProperty("authors") Set<UserDto> authors,
@JsonProperty("references") List<ReferenceDto> references, @JsonProperty("references") List<ReferenceDto> references,
@JsonProperty("formatStandard") ReferenceDto.FormatStandard formatStandard) { @JsonProperty("formatStandard") ReferenceDto.FormatStandard formatStandard) {
this.id = id; this.entityId = entityId;
this.title = title; this.title = title;
this.status = status; this.status = status;
this.type = type; this.type = type;
@ -80,7 +81,7 @@ public class PaperDto {
} }
public PaperDto(Paper paper) { public PaperDto(Paper paper) {
this.id = paper.getId(); this.entityId = paper.getId();
this.title = paper.getTitle(); this.title = paper.getTitle();
this.status = paper.getStatus(); this.status = paper.getStatus();
this.type = paper.getType(); this.type = paper.getType();
@ -97,12 +98,12 @@ public class PaperDto {
this.references = convert(paper.getReferences(), ReferenceDto::new); this.references = convert(paper.getReferences(), ReferenceDto::new);
} }
public Integer getId() { public Integer getEntityId() {
return id; return entityId;
} }
public void setId(Integer id) { public void setId(Integer id) {
this.id = id; this.entityId = id;
} }
public String getTitle() { public String getTitle() {

@ -176,7 +176,7 @@ public class PaperService {
@Transactional @Transactional
public Integer update(PaperDto paperDto) throws IOException { public Integer update(PaperDto paperDto) throws IOException {
Paper paper = paperRepository.getOne(paperDto.getId()); Paper paper = paperRepository.getOne(paperDto.getEntityId());
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors()); Set<User> oldAuthors = new HashSet<>(paper.getAuthors());

Loading…
Cancel
Save