Skip to content
Open
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
30 changes: 22 additions & 8 deletions app/controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import ch.qos.logback.core.status.ErrorStatus;
import com.fasterxml.jackson.databind.JsonNode;
import io.ebean.PagedList;
import io.swagger.annotations.*;
import models.dao.Game;
import models.dto.GameDto;
import models.dto.PageDto;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
Expand All @@ -21,8 +24,8 @@ public class GameController extends Controller {
@ApiOperation(value = "Add Game", notes = "Put game from json into list")
public Result addGame() {
JsonNode json = request().body().asJson();
Game game = Json.fromJson(json, Game.class);
gameService.createGame(game);
GameDto gameDto = Json.fromJson(json, GameDto.class);
gameService.createGame(new Game(gameDto));
return ok();
}

Expand All @@ -36,31 +39,42 @@ public Result deleteGame(@ApiParam(value = "Game Id", name = "id") long id) {
@ApiOperation(value = "Update Game", notes = "Update game in list from json")
@ApiResponses({
@ApiResponse(code = 404, message = "Game Not Found", response = ErrorStatus.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class) })
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class)})
public Result updateGame(@ApiParam(value = "Game Id", name = "id") long id) {
JsonNode json = request().body().asJson();
Game game = Json.fromJson(json, Game.class);
gameService.updateGame(game);
GameDto gameDto = Json.fromJson(json, GameDto.class);
gameService.updateGame(new Game(gameDto));
return ok();
}

@ApiOperation(value = "Get All Games", notes = "Get list of games", response = JsonNode.class)
@ApiResponses({
@ApiResponse(code = 404, message = "Games Not Found", response = ErrorStatus.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class) })
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class)})
public Result getGames() {
List<Game> games = gameService.getGames();
JsonNode jsonNode = Json.toJson(games);
return ok(jsonNode).as("application/json");
}

@ApiOperation(value = "Get Page", notes = "Get page of games", response = JsonNode.class)
@ApiResponses({
@ApiResponse(code = 404, message = "Games Not Found", response = ErrorStatus.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class)})
public Result getPage(int page) {
PagedList<Game> pagedList = gameService.getPage(page, 15);
PageDto pageDto = new PageDto(page, pagedList);
JsonNode jsonNode = Json.toJson(pageDto);
return ok(jsonNode).as("application/json");
}

@ApiOperation(value = "Get Game By Id", notes = "Get the game by it's Id", response = JsonNode.class)
@ApiResponses({
@ApiResponse(code = 404, message = "Game Not Found", response = ErrorStatus.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class) })
@ApiResponse(code = 500, message = "Internal Server Error", response = ErrorStatus.class)})
public Result getGameById(@ApiParam(value = "Game Id", name = "id") long id) {
Game game = gameService.getGameById(id);
JsonNode jsonNode = Json.toJson(game);
JsonNode jsonNode = Json.toJson(new GameDto(game));
return ok(jsonNode).as("application/json");
}
}
15 changes: 15 additions & 0 deletions app/models/dao/Game.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package models.dao;

import io.ebean.Ebean;
import io.ebean.Finder;
import io.ebean.Model;
import io.ebean.Query;
import models.dto.GameDto;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

@Entity
@Table(name = "games")
Expand All @@ -28,6 +32,17 @@ public Game() {
this.created_at = created_at;
}

public Game(GameDto game) {
this.id = game.getId();
this.name = game.getName();
this.popularity = game.getPopularity();
try {
this.created_at = new java.sql.Timestamp(new SimpleDateFormat("dd-MM-yyyy").parse(game.getCreated_at()).getTime());
} catch (Exception e) {
e.printStackTrace();
}
}

public static final Finder<Long, Game> find = new Finder<>(Game.class);

public void setId(long id) {
Expand Down
51 changes: 51 additions & 0 deletions app/models/dto/GameDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
package models.dto;

import models.dao.Game;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;

public class GameDto {
private long id;
private String name;
private double popularity;
private String created_at;

public GameDto(Game game) {
this.id = game.getId();
this.name = game.getName();
this.popularity = new BigDecimal(game.getPopularity()).setScale(3, RoundingMode.UP).doubleValue();
this.created_at = new SimpleDateFormat("dd-MM-yyyy").format(game.getCreated_at());
}

public long getId() {
return id;
}

public double getPopularity() {
return popularity;
}

public String getCreated_at() {
return created_at;
}

public String getName() {
return name;
}

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

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

public void setPopularity(double popularity) {
this.popularity = popularity;
}

public void setCreated_at(String created_at) {
this.created_at = created_at;
}


}
37 changes: 37 additions & 0 deletions app/models/dto/PageDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package models.dto;

import io.ebean.PagedList;
import models.dao.Game;

import java.util.List;

public class PageDto {
private int pageNumber;
private final int totalNumber;
private List<Game> gameList;

public PageDto(int pageNumber, PagedList<Game> pagedList) {

this.pageNumber = pageNumber;
this.totalNumber = pagedList.getTotalPageCount();
this.gameList = pagedList.getList();
}

public List<Game> getGameList() {
return gameList;
}

public void setGameList(List<Game> gameList) {
this.gameList = gameList;
}

public int getPageNumber() {
return pageNumber;
}

public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}

public int getTotalNumber() { return totalNumber; }
}
2 changes: 1 addition & 1 deletion app/modules/InitialData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Date;

public class InitialData {
public InitialData() {

public InitialData() {
User user = User.find.query().where().like("login", "admin").findOne();
if (user == null) {
User admin = new User();
Expand Down
2 changes: 2 additions & 0 deletions app/service/GameService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package service;

import io.ebean.PagedList;
import models.dao.Game;

import java.util.List;

public interface GameService {
Game getGameById(long id);
List<Game> getGames();
PagedList<Game> getPage(int page, int size);
void deleteGame(long id);
void createGame(Game game);
void updateGame(Game game);
Expand Down
11 changes: 11 additions & 0 deletions app/service/GameServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package service;

import io.ebean.Ebean;
import io.ebean.Model;
import io.ebean.PagedList;
import models.dao.Game;

import java.util.List;
Expand All @@ -15,6 +17,15 @@ public List<Game> getGames() {
return Game.find.all();
}

public PagedList<Game> getPage(int page, int size) {
return Game.find.query()
.fetch("game_id")
.setFirstRow((page-1)*size)
.setMaxRows(size)
.orderBy("id")
.findPagedList();
}

public void deleteGame(long id) {
Game.db().delete(id);
}
Expand Down
2 changes: 1 addition & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ play.modules {
# explicitly below.
# If there are any built-in modules that you want to enable, you can list them here.
#enabled += my.application.Module
#enabled += "play.modules.swagger.SwaggerModule"
enabled += "play.modules.swagger.SwaggerModule"
enabled += "modules.ComponentModule"
enabled += be.objectify.deadbolt.java.DeadboltModule
enabled += "modules.SecurityModule"
Expand Down
66 changes: 34 additions & 32 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@
# ~~~~

# Serve index page from public directory
GET / controllers.FrontendController.index()
GET / controllers.FrontendController.index()

# Serve static assets under public directory
#GET /*file controllers.FrontendController.assetOrDefault(file)

GET /callback @org.pac4j.play.CallbackController.callback()
POST /callback @org.pac4j.play.CallbackController.callback()
GET /casProxyCallback @org.pac4j.play.CallbackController.callback()
POST /casProxyCallback @org.pac4j.play.CallbackController.callback()
GET /logout @org.pac4j.play.LogoutController.logout()

GET /api/game controllers.GameController.getGames
GET /api/game/$id<[0-9]+> controllers.GameController.getGameById(id: Long)
GET /api/user controllers.UserController.getUsers
GET /api/user/$id<[0-9]+> controllers.UserController.getUserById(id:Long)
GET /api/claim controllers.ClaimController.getClaims
GET /api/claim/$id<[0-9]+> controllers.ClaimController.getClaimById(id:Long)
GET /api/wishlist controllers.WishlistController.getWishlists
GET /api/wishlist/$id<[0-9]+> controllers.WishlistController.getWishlistById(id:Long)
GET /callback @org.pac4j.play.CallbackController.callback()
POST /callback @org.pac4j.play.CallbackController.callback()
GET /casProxyCallback @org.pac4j.play.CallbackController.callback()
POST /casProxyCallback @org.pac4j.play.CallbackController.callback()
GET /logout @org.pac4j.play.LogoutController.logout()

GET /api/game controllers.GameController.getGames
GET /api/games/:page controllers.GameController.getPage(page: Int)
GET /api/game/$id<[0-9]+> controllers.GameController.getGameById(id: Long)
GET /api/user controllers.UserController.getUsers
GET /api/user/$id<[0-9]+> controllers.UserController.getUserById(id:Long)
GET /api/claim controllers.ClaimController.getClaims
GET /api/claim/$id<[0-9]+> controllers.ClaimController.getClaimById(id:Long)
GET /api/wishlist controllers.WishlistController.getWishlists
GET /api/wishlist/$id<[0-9]+> controllers.WishlistController.getWishlistById(id:Long)

+nocsrf
POST /api/game controllers.GameController.addGame
POST /api/game controllers.GameController.addGame
+nocsrf
POST /api/addgames controllers.ToolController.addGameToDb
PUT /api/game/$id<[0-9]+> controllers.GameController.updateGame(id: Long)
DELETE /api/game/$id<[0-9]+> controllers.GameController.deleteGame(id: Long)
POST /api/user controllers.UserController.addUser
PUT /api/user/$id<[0-9]+> controllers.UserController.updateUser(id: Long)
DELETE /api/user/$id<[0-9]+> controllers.UserController.deleteUser(id: Long)
POST /api/claim controllers.ClaimController.addClaim
PUT /api/claim/$id<[0-9]+> controllers.ClaimController.updateClaim(id: Long)
DELETE /api/claim/$id<[0-9]+> controllers.ClaimController.deleteClaim(id: Long)
POST /api/wishlist controllers.WishlistController.addWishlist
PUT /api/wishlist/$id<[0-9]+> controllers.WishlistController.updateWishlist(id: Long)
DELETE /api/wishlist/$id<[0-9]+> controllers.WishlistController.deleteWishlist(id: Long)

POST /api/addgames controllers.ToolController.addGameToDb
PUT /api/game/$id<[0-9]+> controllers.GameController.updateGame(id: Long)
DELETE /api/game/$id<[0-9]+> controllers.GameController.deleteGame(id: Long)
POST /api/user controllers.UserController.addUser
PUT /api/user/$id<[0-9]+> controllers.UserController.updateUser(id: Long)
DELETE /api/user/$id<[0-9]+> controllers.UserController.deleteUser(id: Long)
POST /api/claim controllers.ClaimController.addClaim
PUT /api/claim/$id<[0-9]+> controllers.ClaimController.updateClaim(id: Long)
DELETE /api/claim/$id<[0-9]+> controllers.ClaimController.deleteClaim(id: Long)
POST /api/wishlist controllers.WishlistController.addWishlist
PUT /api/wishlist/$id<[0-9]+> controllers.WishlistController.updateWishlist(id: Long)
DELETE /api/wishlist/$id<[0-9]+> controllers.WishlistController.deleteWishlist(id: Long)

# Serve static assets under public directory
GET /*file controllers.FrontendController.assetOrDefault(file)
GET /swagger.json controllers.ApiHelpController.getResources
GET /docs controllers.Assets.at(path="/public/swagger", file="index.html")
GET /docs/*file controllers.Assets.at(path="/public/swagger", file)

# Serve static assets under public directory
GET /swagger.json controllers.ApiHelpController.getResources
GET /docs/ controllers.Assets.at(path="/public/swagger",file="index.html")
GET /*file controllers.FrontendController.assetOrDefault(file)

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"express": "^4.16.4"
},
"devDependencies": {}
}
}
Binary file added public/swagger/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/swagger/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading