PAN-7 Finished the DefinitionController

This commit is contained in:
2019-02-17 22:22:05 -07:00
parent dffe0187df
commit abefb397e3
5 changed files with 92 additions and 21 deletions
@@ -98,10 +98,11 @@ public class PersistenceApi {
Properties additionalProperties() {
final Properties properties = new Properties();
properties.setProperty("hibernate.ddl-auto", hibernateTablePolicy);
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", hibernateDialect);
properties.setProperty("spring.jpa.show-sql", showSql);
properties.setProperty("spring.datasource.tomcat.max-active", tomcatPoolMaxActive);
properties.setProperty("hibernate.id.new_generator_mappings","false");
return properties;
}
@@ -3,14 +3,18 @@ package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(path = "/definitions/")
@@ -20,34 +24,98 @@ public class DefinitionController {
@GetMapping("/")
public @ResponseBody
ResponseEntity<Iterable<DefinitionDto>> getAllDefinitions() {
final Iterable<DefinitionDto> listOfDefinitions = definitionRepository.findAll();
LOG.info("Received request to list all definitions");
LOG.debug("Querying for list of all definitions");
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<DefinitionDto> listOfDefinitions = definitionRepository.findAll();
stopWatch.stop();
LOG.debug("Successfully completed query. Query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning list of all definition with size " + listOfDefinitions.size());
return new ResponseEntity<>(listOfDefinitions, HttpStatus.OK);
}
@GetMapping("/{id}")
public @ResponseBody
ResponseEntity<DefinitionDto> getDefinitionById(@PathVariable("id") final Integer id) {
LOG.info("Received request to query for definition with id " + id);
if (id == null) {
LOG.error("ERROR: ID was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for definition with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<DefinitionDto> definition = definitionRepository.findById(id);
return definition.map(definitionDto ->
new ResponseEntity<>(definitionDto, HttpStatus.OK)).orElseGet(
() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return definition.map(definitionDto -> {
LOG.info("Returning definition with id " + id);
return new ResponseEntity<>(definitionDto, HttpStatus.OK);
}).orElseGet(
() -> {
LOG.warn("No definition was found with id " + id);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
});
}
@PostMapping("/add")
@PostMapping("/")
public @ResponseBody ResponseEntity<DefinitionDto> insertDefinition(
@Valid @ModelAttribute("definitionDto") final DefinitionDto definitionDto,
@Validated(DefinitionDto.Insert.class) @RequestBody final DefinitionDto definitionDto,
final BindingResult bindingResult) {
LOG.info("Received request to insert a new definition");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (definitionDto == null) {
LOG.error("Passed entity is unprocessable");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Saving new definition");
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final DefinitionDto savedDefinition = definitionRepository.save(definitionDto);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning the newly created definition with id " + savedDefinition.getId());
return new ResponseEntity<>(savedDefinition, HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteDefinitionById(@PathVariable("id") final Integer id) {
LOG.info("Received request to delete definition with id " + id);
if (id == null) {
LOG.error("Specified id is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Deleting definition with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
definitionRepository.deleteById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
@@ -14,6 +14,7 @@ import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import javax.persistence.*;
import java.io.Serializable;
@Data
@@ -28,10 +29,12 @@ import javax.persistence.*;
@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
@TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
})
public class BaseDto {
public class BaseDto implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Integer id;
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Version
Integer version;
private Integer version;
public static final long serialVersionUID = -1686252381978213945L;
}
@@ -8,17 +8,22 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
@Entity(name = "definitions")
@Table(name = "definitions")
@EntityListeners(AuditingEntityListener.class)
@Data
@EqualsAndHashCode(callSuper = true)
public class DefinitionDto extends BaseDto implements Serializable {
@Size(min = 1, max = 200, message = "Must be between 1 and 200 characters") private String name;
@Type(type = "jsonb") @Column(columnDefinition = "jsonb") private Definition definition;
@Type(type = "jsonb") @Column(columnDefinition = "jsonb") private Notation notation;
@NotBlank(groups = Insert.class) @Size(min = 1, max = 200, message = "Must be between 1 and 200 characters") private String name;
@NotBlank(groups = Insert.class) @Type(type = "json") @Column(columnDefinition = "jsonb") private Definition definition;
@Type(type = "json") @Column(columnDefinition = "jsonb") private Notation notation;
public static final long serialVersionUID = -5314619286352932857L;
public interface Insert {}
}
@@ -2,16 +2,10 @@ package edu.msudenver.tsp.persistence.repository;
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
import edu.msudenver.tsp.persistence.dto.Notation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DefinitionRepository extends JpaRepository<DefinitionDto, Integer> {
DefinitionDto findByName(final String name);
DefinitionDto findByDefinition(final String definition);
DefinitionDto findByNotation(final Notation notation);
}