Skip to content

Commit

Permalink
Post, Records
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusCandido committed Sep 15, 2020
1 parent 6e44c22 commit 0efdc49
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.dspesquisa.controllers;

import com.dspesquisa.dto.GameDTO;
import com.dspesquisa.dto.RecordDTO;
import com.dspesquisa.dto.RecordInsertDTO;
import com.dspesquisa.services.GamesService;
import com.dspesquisa.services.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="/records")
public class RecordsController {

@Autowired
private RecordService service;

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

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

import com.dspesquisa.entities.Plataform;
import com.dspesquisa.entities.Record;

import java.io.Serializable;
import java.time.Instant;

public class RecordDTO implements Serializable {

private static final long serialvLersionUID =1L;

private Long id;
private Instant moment;
private String name;
private Integer age;
private String gameTitle;
private Plataform gamePlatform;
private String genreName;

public RecordDTO(){}

public RecordDTO(Record entity){
id = entity.getId();
moment= entity.getMoment();
name= entity.getName();
age = entity.getAge();
gameTitle = entity.getGame().getTitle();
gamePlatform = entity.getGame().getPlataform();
genreName = entity.getGame().getGenre().getName();
}
}
38 changes: 38 additions & 0 deletions Backend/src/main/java/com/dspesquisa/dto/RecordInsertDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dspesquisa.dto;

import java.io.Serializable;

public class RecordInsertDTO implements Serializable {

private static final long serialvLersionUID =1L;
private String name;
private Integer age;
private Long gameId;

public RecordInsertDTO() {
}

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 Long getGameId() {
return gameId;
}

public void setGameId(Long gameId) {
this.gameId = gameId;
}
}
4 changes: 4 additions & 0 deletions Backend/src/main/java/com/dspesquisa/entities/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class Record implements Serializable {
@JoinColumn(name = "game_id")
private Games game;


public Record() {
}

public Record(Long id, String name, Integer age, Instant moment, Games game) {
this.id = id;
this.name = name;
Expand Down
41 changes: 41 additions & 0 deletions Backend/src/main/java/com/dspesquisa/services/RecordService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.dspesquisa.services;

import com.dspesquisa.dto.GameDTO;
import com.dspesquisa.dto.RecordDTO;
import com.dspesquisa.dto.RecordInsertDTO;
import com.dspesquisa.entities.Games;
import com.dspesquisa.entities.Record;
import com.dspesquisa.repositories.GameRepository;
import com.dspesquisa.repositories.RecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class RecordService {

@Autowired
private RecordRepository repository;

@Autowired
private GameRepository gameRepository;

@Transactional
public RecordDTO insert (RecordInsertDTO dto){
Record entity = new Record();
entity.setName(dto.getName());
entity.setAge(dto.getAge());
entity.setMoment(Instant.now());

Games game = gameRepository.getOne(dto.getGameId());
entity.setGame(game);

entity = repository.save(entity);

return new RecordDTO(entity);
}
}

0 comments on commit 0efdc49

Please sign in to comment.