PAN-7 Finished the DefinitionController
This commit is contained in:
+2
-1
@@ -98,10 +98,11 @@ public class PersistenceApi {
|
|||||||
|
|
||||||
Properties additionalProperties() {
|
Properties additionalProperties() {
|
||||||
final Properties properties = new Properties();
|
final Properties properties = new Properties();
|
||||||
properties.setProperty("hibernate.ddl-auto", hibernateTablePolicy);
|
properties.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||||
properties.setProperty("hibernate.dialect", hibernateDialect);
|
properties.setProperty("hibernate.dialect", hibernateDialect);
|
||||||
properties.setProperty("spring.jpa.show-sql", showSql);
|
properties.setProperty("spring.jpa.show-sql", showSql);
|
||||||
properties.setProperty("spring.datasource.tomcat.max-active", tomcatPoolMaxActive);
|
properties.setProperty("spring.datasource.tomcat.max-active", tomcatPoolMaxActive);
|
||||||
|
properties.setProperty("hibernate.id.new_generator_mappings","false");
|
||||||
|
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|||||||
+75
-7
@@ -3,14 +3,18 @@ package edu.msudenver.tsp.persistence.controller;
|
|||||||
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
|
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
|
||||||
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
import edu.msudenver.tsp.persistence.repository.DefinitionRepository;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.util.StopWatch;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequestMapping(path = "/definitions/")
|
@RequestMapping(path = "/definitions/")
|
||||||
@@ -20,34 +24,98 @@ public class DefinitionController {
|
|||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<Iterable<DefinitionDto>> getAllDefinitions() {
|
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);
|
return new ResponseEntity<>(listOfDefinitions, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<DefinitionDto> getDefinitionById(@PathVariable("id") final Integer id) {
|
ResponseEntity<DefinitionDto> getDefinitionById(@PathVariable("id") final Integer id) {
|
||||||
|
LOG.info("Received request to query for definition with id " + id);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
|
LOG.error("ERROR: ID was null");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
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);
|
final Optional<DefinitionDto> definition = definitionRepository.findById(id);
|
||||||
return definition.map(definitionDto ->
|
|
||||||
new ResponseEntity<>(definitionDto, HttpStatus.OK)).orElseGet(
|
stopWatch.stop();
|
||||||
() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
|
|
||||||
|
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(
|
public @ResponseBody ResponseEntity<DefinitionDto> insertDefinition(
|
||||||
@Valid @ModelAttribute("definitionDto") final DefinitionDto definitionDto,
|
@Validated(DefinitionDto.Insert.class) @RequestBody final DefinitionDto definitionDto,
|
||||||
final BindingResult bindingResult) {
|
final BindingResult bindingResult) {
|
||||||
|
LOG.info("Received request to insert a new definition");
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
|
LOG.error("Binding result is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
}
|
}
|
||||||
if (definitionDto == null) {
|
if (definitionDto == null) {
|
||||||
|
LOG.error("Passed entity is unprocessable");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG.debug("Saving new definition");
|
||||||
|
|
||||||
|
final StopWatch stopWatch = new StopWatch();
|
||||||
|
stopWatch.start();
|
||||||
|
|
||||||
final DefinitionDto savedDefinition = definitionRepository.save(definitionDto);
|
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);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -14,6 +14,7 @@ import org.hibernate.annotations.TypeDef;
|
|||||||
import org.hibernate.annotations.TypeDefs;
|
import org.hibernate.annotations.TypeDefs;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -28,10 +29,12 @@ import javax.persistence.*;
|
|||||||
@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
|
@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
|
||||||
@TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
|
@TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
|
||||||
})
|
})
|
||||||
public class BaseDto {
|
public class BaseDto implements Serializable {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
Integer id;
|
private int id;
|
||||||
@Version
|
@Version
|
||||||
Integer version;
|
private Integer version;
|
||||||
|
|
||||||
|
public static final long serialVersionUID = -1686252381978213945L;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-3
@@ -8,17 +8,22 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.EntityListeners;
|
import javax.persistence.EntityListeners;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@Entity(name = "definitions")
|
@Entity(name = "definitions")
|
||||||
|
@Table(name = "definitions")
|
||||||
@EntityListeners(AuditingEntityListener.class)
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class DefinitionDto extends BaseDto implements Serializable {
|
public class DefinitionDto extends BaseDto implements Serializable {
|
||||||
@Size(min = 1, max = 200, message = "Must be between 1 and 200 characters") private String name;
|
@NotBlank(groups = Insert.class) @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;
|
@NotBlank(groups = Insert.class) @Type(type = "json") @Column(columnDefinition = "jsonb") private Definition definition;
|
||||||
@Type(type = "jsonb") @Column(columnDefinition = "jsonb") private Notation notation;
|
@Type(type = "json") @Column(columnDefinition = "jsonb") private Notation notation;
|
||||||
|
|
||||||
public static final long serialVersionUID = -5314619286352932857L;
|
public static final long serialVersionUID = -5314619286352932857L;
|
||||||
|
|
||||||
|
public interface Insert {}
|
||||||
}
|
}
|
||||||
|
|||||||
-6
@@ -2,16 +2,10 @@ package edu.msudenver.tsp.persistence.repository;
|
|||||||
|
|
||||||
|
|
||||||
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
|
import edu.msudenver.tsp.persistence.dto.DefinitionDto;
|
||||||
import edu.msudenver.tsp.persistence.dto.Notation;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface DefinitionRepository extends JpaRepository<DefinitionDto, Integer> {
|
public interface DefinitionRepository extends JpaRepository<DefinitionDto, Integer> {
|
||||||
|
|
||||||
DefinitionDto findByName(final String name);
|
|
||||||
|
|
||||||
DefinitionDto findByDefinition(final String definition);
|
|
||||||
|
|
||||||
DefinitionDto findByNotation(final Notation notation);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user