Skip to content

Commit

Permalink
Method/GET records
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusCandido committed Sep 15, 2020
1 parent 0efdc49 commit 1727d55
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import com.dspesquisa.services.GamesService;
import com.dspesquisa.services.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;

@RestController
Expand All @@ -20,8 +24,35 @@ public class RecordsController {

@PostMapping
public ResponseEntity<RecordDTO> insert(@RequestBody RecordInsertDTO dto){
RecordDTO nDto = service.insert(dto);
RecordDTO nDto = new RecordDTO();
nDto = service.insert(dto);
return ResponseEntity.ok().body(nDto);
}

@GetMapping
public ResponseEntity<Page<RecordDTO>> findAll(
@RequestParam(value= "min",defaultValue = "") String min,
@RequestParam(value="max", defaultValue = "") String max,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "linesPerPage", defaultValue = "0") Integer linesPerPage,
@RequestParam(value = "orderBy", defaultValue = "moment") String orderBy,
@RequestParam(value = "direction", defaultValue = "DESC") String direction){

Instant minDate = ("".equals(min)) ? null : Instant.parse(min);

Instant maxDate = ("".equals(max)) ? null : Instant.parse(max);

if(linesPerPage == 0 ){
linesPerPage = Integer.MAX_VALUE;
}

PageRequest pageRequest = PageRequest.of(page, linesPerPage, Sort.Direction.valueOf(direction),orderBy);


Page<RecordDTO> list = service.findByMoments( minDate,maxDate,pageRequest);
return ResponseEntity.ok().body(list);
}



}
60 changes: 60 additions & 0 deletions Backend/src/main/java/com/dspesquisa/dto/RecordDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,64 @@ public RecordDTO(Record entity){
gamePlatform = entity.getGame().getPlataform();
genreName = entity.getGame().getGenre().getName();
}

public static long getSerialvLersionUID() {
return serialvLersionUID;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Instant getMoment() {
return moment;
}

public void setMoment(Instant moment) {
this.moment = moment;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getGameTitle() {
return gameTitle;
}

public void setGameTitle(String gameTitle) {
this.gameTitle = gameTitle;
}

public Plataform getGamePlatform() {
return gamePlatform;
}

public void setGamePlatform(Plataform gamePlatform) {
this.gamePlatform = gamePlatform;
}

public String getGenreName() {
return genreName;
}

public void setGenreName(String genreName) {
this.genreName = genreName;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.dspesquisa.repositories;

import com.dspesquisa.entities.Genre;

import com.dspesquisa.entities.Record;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.Instant;

@Repository
public interface RecordRepository extends JpaRepository<Record, Long> {

@Query(value = "SELECT obj FROM Record obj WHERE " +
"(coalesce(:min, null) IS NULL OR obj.moment >= :min) AND " +
"(coalesce(:max, null) IS NULL OR obj.moment <= :max)")

Page<Record> findByMoments(Instant min, Instant max, Pageable pageRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.dspesquisa.repositories.GameRepository;
import com.dspesquisa.repositories.RecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -38,4 +40,9 @@ public RecordDTO insert (RecordInsertDTO dto){

return new RecordDTO(entity);
}

@Transactional(readOnly = true)
public Page<RecordDTO> findByMoments(Instant minDate, Instant maxDate, PageRequest pageRequest) {
return repository.findByMoments(minDate, maxDate, pageRequest).map(x -> new RecordDTO(x));
}
}
1 change: 1 addition & 0 deletions Backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
spring.profiles.active=test

spring.jpa.open-in-view=false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false

0 comments on commit 1727d55

Please sign in to comment.