PAN-52 Added findByName and findByBranch methods to the repository, and created first couple of methods and corresponding tests in the ProofController
This commit is contained in:
+60
-2
@@ -1,11 +1,69 @@
|
||||
package edu.msudenver.tsp.persistence.controller;
|
||||
|
||||
import edu.msudenver.tsp.persistence.dto.ProofDto;
|
||||
import edu.msudenver.tsp.persistence.repository.ProofRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Component
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/proofs")
|
||||
public class ProofController {
|
||||
private final ProofRepository proofRepository;
|
||||
|
||||
@GetMapping("/")
|
||||
public @ResponseBody
|
||||
ResponseEntity<Iterable<ProofDto>> getAllProofs() {
|
||||
LOG.info("Received request to list all theorems");
|
||||
|
||||
LOG.debug("Querying for list of all theorems");
|
||||
final StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
final List<ProofDto> listOfProofs = proofRepository.findAll();
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
LOG.debug("Successfully completed query. Query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
|
||||
LOG.info("Returning list of all theorems with size " + listOfProofs.size());
|
||||
|
||||
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/{branch}")
|
||||
public @ResponseBody
|
||||
ResponseEntity<List<ProofDto>> getAllProofsByBranch(@PathVariable("branch") final String branch) {
|
||||
LOG.info("Received request to query for proofs related to the " + branch + " branch of mathematics");
|
||||
if (branch == null) {
|
||||
LOG.error("ERROR: branch was null");
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
LOG.debug("Querying for proofs with branch " + branch);
|
||||
|
||||
final StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
|
||||
final List<ProofDto> listOfProofs = proofRepository.findByBranch(branch);
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
|
||||
LOG.info("Returning list of all proofs with size " + listOfProofs.size());
|
||||
|
||||
if (listOfProofs.isEmpty()) {
|
||||
LOG.warn("No proofs were found for branch {}", branch);
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
LOG.info("Returning list of proofs with branch {}", branch);
|
||||
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,38 +11,42 @@ import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Entity(name = "proofs")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class ProofDto extends BaseDto implements Serializable {
|
||||
@NotBlank(groups = Insert.class)
|
||||
@Size(min = 1, max = 512, message = "The name must be at least 1 character and at most 512 characters")
|
||||
private String name;
|
||||
@NotBlank(groups = Insert.class)
|
||||
@Size(min = 1, max = 512, message = "The branch must be at least 1 character and at most 512 characters")
|
||||
private String branch;
|
||||
@Type(type = "json") @Column(name = "referenced_definitions", columnDefinition = "jsonb") private String referencedDefinitions;
|
||||
@Type(type = "json") @Column(name = "referenced_theorems", columnDefinition = "jsonb") private String referencedTheorems;
|
||||
@Type(type = "json") @Column(name = "referenced_definitions", columnDefinition = "jsonb") private List<String> referencedDefinitions;
|
||||
@Type(type = "json") @Column(name = "referenced_theorems", columnDefinition = "jsonb") private List<String> referencedTheorems;
|
||||
@Temporal(TemporalType.DATE) @Column(name = "date_created") private Date dateCreated;
|
||||
@Temporal(TemporalType.DATE) @Column(name = "last_updated") private Date lastUpdated;
|
||||
|
||||
|
||||
@JsonProperty("referenced_definitions")
|
||||
public String getReferencedDefinitions() {
|
||||
public List<String> getReferencedDefinitions() {
|
||||
return referencedDefinitions;
|
||||
}
|
||||
|
||||
@JsonProperty("referenced_definitions")
|
||||
public void setReferencedDefinitions(final String referencedDefinitions) {
|
||||
public void setReferencedDefinitions(final List<String> referencedDefinitions) {
|
||||
this.referencedDefinitions = referencedDefinitions;
|
||||
}
|
||||
|
||||
@JsonProperty("referenced_theorems")
|
||||
public String getReferencedTheorems() {
|
||||
public List<String> getReferencedTheorems() {
|
||||
return referencedTheorems;
|
||||
}
|
||||
|
||||
@JsonProperty("referenced_theorems")
|
||||
public void setReferencedTheorems(final String referencedTheorems) {
|
||||
public void setReferencedTheorems(final List<String> referencedTheorems) {
|
||||
this.referencedTheorems = referencedTheorems;
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -1,7 +1,13 @@
|
||||
package edu.msudenver.tsp.persistence.repository;
|
||||
|
||||
import edu.msudenver.tsp.persistence.dto.BaseDto;
|
||||
import edu.msudenver.tsp.persistence.dto.ProofDto;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ProofRepository extends JpaRepository<BaseDto, Integer> {
|
||||
import java.util.List;
|
||||
|
||||
public interface ProofRepository extends JpaRepository<ProofDto, Integer> {
|
||||
|
||||
List<ProofDto> findByBranch(String branch);
|
||||
|
||||
List<ProofDto> findByName(String name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user