Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions games.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ info:
description: API sobre videojuegos
version: 1.0.0
servers:
- url: 'https://www.games.com'
- url: 'https://www.games.com/api'
paths:
/games:
/v1/games:
get:
summary: Devuelve la lista de juegos
description: |
Expand Down Expand Up @@ -46,6 +46,7 @@ paths:
post:
summary: Registrar un juego
description: Registra un nuevo juego
deprecated: true
requestBody:
description: Los datos del nuevo juego
content:
Expand Down Expand Up @@ -77,6 +78,41 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/InternalServerError'
/v2/games:
post:
summary: Registrar un juego
description: Registra un nuevo juego
requestBody:
description: Los datos del nuevo juego
content:
application/json:
schema:
$ref: '#/components/schemas/GameInDtoV2'
responses:
'201':
description: Juego registrado
content:
application/json:
schema:
$ref: '#/components/schemas/GameV2'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/BadRequest'
'409':
description: Conflict
content:
application/json:
schema:
$ref: '#/components/schemas/Conflict'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/InternalServerError'
components:
schemas:
Game:
Expand All @@ -96,6 +132,24 @@ components:
releaseDate: 2020-01-12
price: 45
category: Survival
GameV2:
allOf:
- $ref: '#/components/schemas/GameInDtoV2'
type: object
properties:
id:
type: number
format: int64
description: Identificador unico del juego
example:
id: 1
name: 7 Days to die
description: Surival game
type: Shooter
releaseDate: 2020-01-12
price: 45
category: Survival
rate: 4
GameInDto:
type: object
properties:
Expand Down Expand Up @@ -126,6 +180,42 @@ components:
releaseDate: 2020-01-12
price: 45
category: Survival
GameInDtoV2:
type: object
properties:
name:
type: string
description: Nombre del juego
description:
type: string
description: Descripción del juego
type:
type: string
description: Tipo de juego
category:
type: string
description: Categoría del juego
releaseDate:
type: string
format: date
description: Fecha de lanzamiento del juego
price:
type: number
format: float
description: Precio aproximado del juego
rate:
type: number
format: int32
description: Valoración del juego
required: true
example:
name: 7 Days to die
description: Surival game
type: Shooter
releaseDate: 2020-01-12
price: 45
category: Survival
rate: 4
GameOutDto:
type: object
properties:
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/svalero/games/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,67 @@
package com.svalero.games.controller;

import com.svalero.games.domain.Game;
import com.svalero.games.domain.GameV2;
import com.svalero.games.dto.GameDto;
import com.svalero.games.dto.GameOutDto;
import com.svalero.games.exception.ErrorResponse;
import com.svalero.games.exception.GameNotFoundException;
import com.svalero.games.service.GameService;
import jakarta.validation.Valid;
import org.modelmapper.TypeToken;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.modelmapper.ModelMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class GameController {

@Autowired
private GameService gameService;
@Autowired
private ModelMapper modelMapper;

@GetMapping("/games")
@GetMapping("/v1/games")
public ResponseEntity<List<GameOutDto>> getAll(@RequestParam(value = "category", defaultValue = "") String category) {
List<GameOutDto> gamesOutDto = gameService.findAll(category);
return ResponseEntity.ok(gamesOutDto);
}

@GetMapping("/games/{id}")
@GetMapping("/v1/games/{id}")
public ResponseEntity<GameDto> get(@PathVariable long id) throws GameNotFoundException {
GameDto gameDto = gameService.findById(id);
return ResponseEntity.ok(gameDto);
}

@PostMapping("/games")
public ResponseEntity<Game> addGame(@Valid @RequestBody Game game) {
// TODO Añadir validación
@PostMapping("/v1/games")
public ResponseEntity<Game> addGameV1(@Valid @RequestBody Game game) {
// TODO Comprobar que no exista ya un juego con el mismo nombre
Game newGame = gameService.add(game);
return new ResponseEntity<>(newGame, HttpStatus.CREATED);
}

@PutMapping("/games/{id}")
@PostMapping("/v2/games")
public ResponseEntity<GameV2> addGamev2(@Valid @RequestBody GameV2 game) {
// TODO Comprobar que no exista ya un juego con el mismo nombre
GameV2 newGame = gameService.addV2(game);
return new ResponseEntity<>(newGame, HttpStatus.CREATED);
}

@PutMapping("/v1/games/{id}")
public ResponseEntity<Game> modifyGame(@PathVariable long id, @RequestBody Game game) throws GameNotFoundException {
Game newGame = gameService.modify(id, game);
return ResponseEntity.ok(newGame);
}

@DeleteMapping("/games/{id}")
@DeleteMapping("/v1/games/{id}")
public ResponseEntity<Void> deleteGame(@PathVariable long id) throws GameNotFoundException {
gameService.delete(id);
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class ReviewController {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class UserController {

@Autowired
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/svalero/games/domain/GameV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.svalero.games.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity(name = "GameV2")
@Table(name = "games")
public class GameV2 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
@NotNull(message="name is mandatory")
private String name;
@Column
private String description;
@Column
private String type;
@Column(name = "release_date")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate releaseDate;
@Column
@Min(value=0, message="the price must be a positive number")
private float price;
@Column
private String category;
@Column
private String comments;
@Column
@NotNull(message="rate is mandatory")
private int rate;

@OneToMany(mappedBy = "game")
@JsonBackReference
private List<Review> reviews;
}
2 changes: 2 additions & 0 deletions src/main/java/com/svalero/games/dto/GameDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.svalero.games.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -18,4 +19,5 @@ public class GameDto {
private String category;
private float price;
private long daysToRelease;
private int rate;
}
1 change: 1 addition & 0 deletions src/main/java/com/svalero/games/dto/GameOutDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class GameOutDto {
private String description;
private String type;
private String category;
private int rate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.svalero.games.repository;

import com.svalero.games.domain.GameV2;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GameRepositoryV2 extends CrudRepository<GameV2, Long> {
}
21 changes: 21 additions & 0 deletions src/main/java/com/svalero/games/service/GameService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.svalero.games.service;

import com.svalero.games.domain.Game;
import com.svalero.games.domain.GameV2;
import com.svalero.games.dto.GameDto;
import com.svalero.games.dto.GameOutDto;
import com.svalero.games.exception.GameNotFoundException;
import com.svalero.games.repository.GameRepository;
import com.svalero.games.repository.GameRepositoryV2;
import com.svalero.games.util.DateUtil;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
Expand All @@ -13,19 +15,38 @@

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;

@Service
public class GameService {

@Autowired
private GameRepository gameRepository;
@Autowired
private GameRepositoryV2 gameRepositoryV2;
@Autowired
private ModelMapper modelMapper;

public Game add(Game game) {
return gameRepository.save(game);
}

public GameV2 addV2(GameV2 game) {
return gameRepositoryV2.save(game);
}

public List<Game> getGames(String name, String description, String address) {
List<Game> allGames = gameRepository.findAll();

Stream<Game> gameStream = allGames.stream();
if (name != null)
gameStream = gameStream.filter(i -> i.getName().equalsIgnoreCase(name));
if (description != null)
gameStream = gameStream.filter(i -> i.getDescription().equalsIgnoreCase(description));

return gameStream.toList();
}

public void delete(long id) throws GameNotFoundException {
Game game = gameRepository.findById(id)
.orElseThrow(GameNotFoundException::new);
Expand Down
Loading
Loading