generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BuildingCardFactory #148
Open
chiashengchen
wants to merge
10
commits into
main
Choose a base branch
from
backend/create_building_card
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5a1eac
Add BuildingCardFactory
chiashengchen 964c9f6
Refactor BuildingCardFactory
chiashengchen 4a25564
Add InitBuildCards
chiashengchen 7aa6721
Save json to mongodb
chiashengchen d5bb486
Save json to mongodb & build from mongodb
chiashengchen 7df9c9d
Add swagger dependency
chiashengchen 15fb9a7
Change Building Cards Setting Format
chiashengchen 15f2313
Refactoring BuildingCard Factory
chiashengchen 75918a3
Add Get BuildingCard API
chiashengchen ac38c49
Chore
chiashengchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 0 additions & 27 deletions
27
packages/backend/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
...d/domain/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCard.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,36 @@ | ||
package tw.waterballsa.gaas.citadels.domain.BuildingCard; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
|
||
import java.util.Optional; | ||
|
||
@Value | ||
@AllArgsConstructor | ||
public class BuildingCard { | ||
String name; | ||
int coins; | ||
Color color; | ||
|
||
public enum Color { | ||
PURPLE("特別地區"), | ||
GOLD("貴族地區"), | ||
RED("軍事地區"), | ||
BLUE("商業地區"), | ||
GREEN("宗教地區"); | ||
private final String area; | ||
|
||
Color(String area) { | ||
this.area = area; | ||
} | ||
|
||
public static Optional<Color> fromString(String str) { | ||
for (Color color : Color.values()) { | ||
if(str.equalsIgnoreCase(color.toString())) { | ||
return Optional.ofNullable(color); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n/src/main/java/tw/waterballsa/gaas/citadels/domain/BuildingCard/BuildingCardFactory.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,10 @@ | ||
package tw.waterballsa.gaas.citadels.domain.BuildingCard; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface BuildingCardFactory { | ||
public Optional<List<BuildingCard>> createBuildingCards(); | ||
} | ||
|
||
|
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
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
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
38 changes: 38 additions & 0 deletions
38
...s/backend/spring/src/main/java/tw/waterballsa/gaas/citadels/spring/InitBuildingCards.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,38 @@ | ||
package tw.waterballsa.gaas.citadels.spring; | ||
|
||
import org.bson.BsonArray; | ||
import org.bson.BsonValue; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import org.bson.Document; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
@Component | ||
public class InitBuildingCards implements CommandLineRunner { | ||
|
||
private final MongoTemplate mongoTemplate; | ||
|
||
@Autowired | ||
public InitBuildingCards(MongoTemplate mongoTemplate) { | ||
this.mongoTemplate = mongoTemplate; | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
if(mongoTemplate.collectionExists("buildingCards")) { | ||
return; | ||
} | ||
ClassPathResource resource = new ClassPathResource("BuildingCardInfo.json"); | ||
String jsonContent = new String(Files.readAllBytes(Paths.get(resource.getURI()))); | ||
BsonArray bsonArray = BsonArray.parse(jsonContent); | ||
for(BsonValue bsonValue : bsonArray) { | ||
Document doc = Document.parse(bsonValue.asDocument().toJson()); | ||
mongoTemplate.save(doc, "buildingCards"); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...va/tw/waterballsa/gaas/citadels/spring/controllers/GetBuildingCardsSettingController.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,27 @@ | ||
package tw.waterballsa.gaas.citadels.spring.controllers; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
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 tw.waterballsa.gaas.citadels.spring.repositories.dao.BuildingCardDAO; | ||
|
||
import static org.springframework.http.ResponseEntity.status; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping(value = "/api/citadels") | ||
public class GetBuildingCardsSettingController { | ||
|
||
BuildingCardDAO buildingCardDAO; | ||
|
||
@GetMapping(value = "/building-cards") | ||
public ResponseEntity<?> getBuildingCardsSetting() { | ||
return status(HttpStatus.OK) | ||
.header("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE) | ||
.body(buildingCardDAO.findAll()); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...c/main/java/tw/waterballsa/gaas/citadels/spring/repositories/BuildingCardFactoryImpl.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,24 @@ | ||
package tw.waterballsa.gaas.citadels.spring.repositories; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; | ||
import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCardFactory; | ||
import tw.waterballsa.gaas.citadels.spring.repositories.dao.BuildingCardDAO; | ||
import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardData; | ||
import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; | ||
|
||
import javax.inject.Named; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Named | ||
@RequiredArgsConstructor | ||
public class BuildingCardFactoryImpl implements BuildingCardFactory { | ||
|
||
private final BuildingCardDAO buildingCardDAO; | ||
|
||
@Override | ||
public Optional<List<BuildingCard>> createBuildingCards() { | ||
return Optional.of(BuildingCardSetting.toDomains(buildingCardDAO.findAll())); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...g/src/main/java/tw/waterballsa/gaas/citadels/spring/repositories/dao/BuildingCardDAO.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 tw.waterballsa.gaas.citadels.spring.repositories.dao; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
import tw.waterballsa.gaas.citadels.spring.repositories.data.BuildingCardSetting; | ||
|
||
@Repository | ||
public interface BuildingCardDAO extends MongoRepository<BuildingCardSetting, String> { | ||
} |
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
40 changes: 40 additions & 0 deletions
40
.../main/java/tw/waterballsa/gaas/citadels/spring/repositories/data/BuildingCardSetting.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,40 @@ | ||
package tw.waterballsa.gaas.citadels.spring.repositories.data; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import tw.waterballsa.gaas.citadels.domain.BuildingCard.BuildingCard; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Data | ||
@Document("buildingCards") | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class BuildingCardSetting { | ||
private String name; | ||
private int coins; | ||
private BuildingCard.Color color; | ||
private int quantity; | ||
private String description; | ||
private String image; | ||
|
||
public static List<BuildingCard> toDomain(BuildingCardSetting buildingCardSetting) { | ||
int quantity = buildingCardSetting.getQuantity(); | ||
List<BuildingCard> buildingCards = new ArrayList<>(); | ||
for (int i = 0; i < quantity; i++) { | ||
BuildingCard buildingCard = new BuildingCard(buildingCardSetting.getName(), buildingCardSetting.getCoins(), buildingCardSetting.getColor()); | ||
buildingCards.add(buildingCard); | ||
} | ||
return buildingCards; | ||
} | ||
|
||
public static List<BuildingCard> toDomains(List<BuildingCardSetting> buildingCardSettings) { | ||
return buildingCardSettings.stream(). | ||
flatMap(setting -> toDomain(setting).stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此BuildingCard 類別可能要封裝屬性只能Get ,因為建築卡應該是不會被改變的~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以使用 @value 對嗎