Skip to content

Commit

Permalink
DTO, Service
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusCandido committed Sep 15, 2020
1 parent 44d8747 commit 6e44c22
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dspesquisa.controllers;

import com.dspesquisa.dto.GameDTO;
import com.dspesquisa.entities.Games;
import com.dspesquisa.repositories.GameRepository;
import com.dspesquisa.services.GamesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value="/games")
public class GamesController {

@Autowired
private GamesService service;

@GetMapping()
public ResponseEntity<List<GameDTO>> findAll(){
List<GameDTO> list = service.findAll();
return ResponseEntity.ok().body(list);
}

}
44 changes: 44 additions & 0 deletions Backend/src/main/java/com/dspesquisa/dto/GameDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.dspesquisa.dto;

import com.dspesquisa.entities.Games;
import com.dspesquisa.entities.Plataform;

import java.io.Serializable;

public class GameDTO implements Serializable {

private static final long serialvLersionUID =1L;
private Long id;
private String title;
private Plataform platform;

public GameDTO(Games entity){
id= entity.getId();
title = entity.getTitle();
platform= entity.getPlataform();
}

public Long getId() {
return id;
}

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

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Plataform getPlatform() {
return platform;
}

public void setPlatform(Plataform platform) {
this.platform = platform;
}
}
5 changes: 4 additions & 1 deletion Backend/src/main/java/com/dspesquisa/entities/Games.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dspesquisa.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -23,10 +25,11 @@ public class Games implements Serializable {
@JoinColumn(name = "genre_id")
private Genre genre;


@OneToMany(mappedBy = "game")
private List<Record> records = new ArrayList<>();

public Games(){};

public Games(Long id, String title, Plataform platform, Genre genre) {
this.id = id;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.dspesquisa.entities.Games;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Repository //Ou @ component
public interface GameRepository extends JpaRepository<Games, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.dspesquisa.entities.Games;
import com.dspesquisa.entities.Genre;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GenreRepository extends JpaRepository<Genre, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.dspesquisa.entities.Genre;
import com.dspesquisa.entities.Record;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

}
25 changes: 25 additions & 0 deletions Backend/src/main/java/com/dspesquisa/services/GamesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dspesquisa.services;

import com.dspesquisa.dto.GameDTO;
import com.dspesquisa.entities.Games;
import com.dspesquisa.repositories.GameRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GamesService {

@Autowired
private GameRepository repository;

@Transactional(readOnly = true)
public List<GameDTO> findAll(){
List<Games> list= repository.findAll();
return list.stream().map(x -> new GameDTO(x)).collect(Collectors.toList()); //Função lambda para converter o tipo Games em uma Lista de games DTO
}
}

0 comments on commit 6e44c22

Please sign in to comment.