-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding game persistance, and an endpoint for creating new games. For now it uses a hardcoded "Alfonso" user. This needs to be updated when authentication is implemented I also had to update the spring-data-dynamodb dependency due to this error: derjust/spring-data-dynamodb#267
- Loading branch information
1 parent
ca4b797
commit 3c85bb0
Showing
7 changed files
with
226 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
minesweeper-api/src/main/java/com/example/minesweeper/controller/GameController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example.minesweeper.controller; | ||
|
||
import com.example.minesweeper.controller.request.NewGameRequest; | ||
import com.example.minesweeper.model.Game; | ||
import com.example.minesweeper.model.User; | ||
import com.example.minesweeper.repository.GameRepository; | ||
import com.example.minesweeper.repository.SavedGame; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class GameController { | ||
|
||
@Autowired | ||
private GameRepository gameRepository; | ||
|
||
@PostMapping("/game") | ||
public SavedGame startNewGame(@RequestBody NewGameRequest newGameRequest) { | ||
|
||
// TODO: | ||
// Until I implement the authentication, this endpoint will work with a hardcoded user | ||
User user = new User("alfonso", 0); | ||
|
||
Game game = new Game(user, newGameRequest.rows, newGameRequest.columns, newGameRequest.mines); | ||
SavedGame savedGame = new SavedGame(game); | ||
return gameRepository.save(savedGame); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
minesweeper-api/src/main/java/com/example/minesweeper/controller/request/NewGameRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.minesweeper.controller.request; | ||
|
||
public class NewGameRequest { | ||
|
||
public int rows; | ||
public int columns; | ||
public int mines; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
minesweeper-api/src/main/java/com/example/minesweeper/repository/GameRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.minesweeper.repository; | ||
|
||
import org.socialsignin.spring.data.dynamodb.repository.EnableScan; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
@EnableScan | ||
public interface GameRepository extends CrudRepository<SavedGame, SavedGameId> { | ||
} |
101 changes: 101 additions & 0 deletions
101
minesweeper-api/src/main/java/com/example/minesweeper/repository/SavedGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.example.minesweeper.repository; | ||
|
||
import com.amazonaws.services.dynamodbv2.datamodeling.*; | ||
import com.example.minesweeper.model.Game; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.data.annotation.Id; | ||
|
||
@DynamoDBTable(tableName = "MineSweeperGame") | ||
public class SavedGame { | ||
|
||
@Id | ||
private SavedGameId savedGameId; | ||
|
||
private String player; | ||
|
||
private String id; | ||
|
||
private int rows; | ||
|
||
private int columns; | ||
|
||
private String cellMines; | ||
|
||
private String cellState; | ||
|
||
public SavedGame() { | ||
System.out.println(); | ||
} | ||
|
||
public SavedGame(Game game) { | ||
player = game.getPlayer().getName(); | ||
id = game.getId(); | ||
savedGameId = new SavedGameId(player, id); | ||
rows = game.getRows(); | ||
columns = game.getColumns(); | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
cellMines = objectMapper.writeValueAsString(game.getCellMines()); | ||
cellState = objectMapper.writeValueAsString(game.getCellState()); | ||
} | ||
catch(JsonProcessingException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@DynamoDBHashKey(attributeName = "player") | ||
public String getPlayer() { | ||
return player; | ||
} | ||
|
||
public void setPlayer(String player) { | ||
this.player = player; | ||
} | ||
|
||
@DynamoDBRangeKey(attributeName = "id") | ||
@DynamoDBAutoGeneratedKey | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@DynamoDBAttribute(attributeName = "rows") | ||
public int getRows() { | ||
return rows; | ||
} | ||
|
||
public void setRows(int rows) { | ||
this.rows = rows; | ||
} | ||
|
||
@DynamoDBAttribute(attributeName = "columns") | ||
public int getColumns() { | ||
return columns; | ||
} | ||
|
||
public void setColumns(int columns) { | ||
this.columns = columns; | ||
} | ||
|
||
@DynamoDBAttribute(attributeName = "cellMines") | ||
public String getCellMines() { | ||
return cellMines; | ||
} | ||
|
||
public void setCellMines(String cellMines) { | ||
this.cellMines = cellMines; | ||
} | ||
|
||
@DynamoDBAttribute(attributeName = "cellState") | ||
public String getCellState() { | ||
return cellState; | ||
} | ||
|
||
public void setCellState(String cellState) { | ||
this.cellState = cellState; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
minesweeper-api/src/main/java/com/example/minesweeper/repository/SavedGameId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.minesweeper.repository; | ||
|
||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey; | ||
|
||
public class SavedGameId { | ||
|
||
private String player; | ||
private String id; | ||
|
||
public SavedGameId(String player, String id) { | ||
this.player = player; | ||
this.id = id; | ||
} | ||
|
||
@DynamoDBHashKey(attributeName = "player") | ||
public String getPlayer() { | ||
return player; | ||
} | ||
|
||
public void setPlayer(String player) { | ||
this.player = player; | ||
} | ||
|
||
@DynamoDBRangeKey(attributeName = "id") | ||
@DynamoDBAutoGeneratedKey | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
} |