-
Notifications
You must be signed in to change notification settings - Fork 5
Diseñar e implementar la vista de creación de mazos #33
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
Draft
amm927
wants to merge
2
commits into
ualdra:develop
Choose a base branch
from
amm927:main
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
166 changes: 166 additions & 0 deletions
166
backend/src/main/java/com/magicvs/backend/config/CardExampleSeeder.java
This file contains hidden or 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,166 @@ | ||
| package com.magicvs.backend.config; | ||
|
|
||
| import com.magicvs.backend.model.Card; | ||
| import com.magicvs.backend.model.CardFace; | ||
| import com.magicvs.backend.repository.CardRepository; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Component | ||
| public class CardExampleSeeder implements CommandLineRunner { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(CardExampleSeeder.class); | ||
|
|
||
| private final CardRepository cardRepository; | ||
|
|
||
| public CardExampleSeeder(CardRepository cardRepository) { | ||
| this.cardRepository = cardRepository; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
| List<CardSeed> examples = List.of( | ||
| new CardSeed( | ||
| "11111111-1111-1111-1111-111111111111", | ||
| "Arcane Apprentice", | ||
| "{1}{U}", | ||
| 2, | ||
| "Creature - Human Wizard", | ||
| "When Arcane Apprentice enters, draw a card.", | ||
| "[\"U\"]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Arcane+Apprentice" | ||
| ), | ||
| new CardSeed( | ||
| "22222222-2222-2222-2222-222222222222", | ||
| "Inferno Raider", | ||
| "{2}{R}", | ||
| 3, | ||
| "Creature - Goblin Berserker", | ||
| "Haste", | ||
| "[\"R\"]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Inferno+Raider" | ||
| ), | ||
| new CardSeed( | ||
| "33333333-3333-3333-3333-333333333333", | ||
| "Verdant Guardian", | ||
| "{3}{G}", | ||
| 4, | ||
| "Creature - Treefolk", | ||
| "Reach", | ||
| "[\"G\"]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Verdant+Guardian" | ||
| ), | ||
| new CardSeed( | ||
| "44444444-4444-4444-4444-444444444444", | ||
| "Radiant Pulse", | ||
| "{1}{W}", | ||
| 2, | ||
| "Instant", | ||
| "You gain 4 life and draw a card.", | ||
| "[\"W\"]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Radiant+Pulse" | ||
| ), | ||
| new CardSeed( | ||
| "55555555-5555-5555-5555-555555555555", | ||
| "Nightveil Ritual", | ||
| "{2}{B}", | ||
| 3, | ||
| "Sorcery", | ||
| "Target player discards two cards.", | ||
| "[\"B\"]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Nightveil+Ritual" | ||
| ), | ||
| new CardSeed( | ||
| "66666666-6666-6666-6666-666666666666", | ||
| "Mystic Crossroads", | ||
| "", | ||
| 0, | ||
| "Land", | ||
| "{T}: Add one mana of any color.", | ||
| "[]", | ||
| "https://placehold.co/488x680/1f2937/e5e7eb?text=Mystic+Crossroads" | ||
| ) | ||
| ); | ||
|
|
||
| int inserted = 0; | ||
| for (CardSeed seed : examples) { | ||
| if (insertIfMissing(seed)) { | ||
| inserted++; | ||
| } | ||
| } | ||
|
|
||
| if (inserted > 0) { | ||
| logger.info("Inserted {} sample cards for tester flows", inserted); | ||
| } | ||
| } | ||
|
|
||
| private boolean insertIfMissing(CardSeed seed) { | ||
| UUID scryfallId = UUID.fromString(seed.scryfallId()); | ||
| if (cardRepository.existsByScryfallId(scryfallId)) { | ||
| return false; | ||
| } | ||
|
|
||
| Card card = new Card(); | ||
| card.setScryfallId(scryfallId); | ||
| card.setName(seed.name()); | ||
| card.setLang("en"); | ||
| card.setLayout("normal"); | ||
| card.setManaCost(seed.manaCost()); | ||
| card.setCmc(BigDecimal.valueOf(seed.cmc())); | ||
| card.setTypeLine(seed.typeLine()); | ||
| card.setOracleText(seed.oracleText()); | ||
| card.setRarity("common"); | ||
| card.setReserved(false); | ||
| card.setReprint(false); | ||
| card.setDigital(false); | ||
| card.setFoil(true); | ||
| card.setNonfoil(true); | ||
| card.setPromo(false); | ||
| card.setFullArt(false); | ||
| card.setTextless(false); | ||
| card.setColorsJson(seed.colorsJson()); | ||
| card.setColorIdentityJson(seed.colorsJson()); | ||
| card.setGamesJson("[\"paper\"]"); | ||
| card.setKeywordsJson("[]"); | ||
| card.setProducedManaJson("[]"); | ||
| card.setPurchaseUrisJson("{}"); | ||
| card.setRelatedUrisJson("{}"); | ||
| card.setRawJson("{}"); | ||
| card.setSyncedAt(LocalDateTime.now()); | ||
|
|
||
| CardFace face = new CardFace(); | ||
| face.setCard(card); | ||
| face.setFaceOrder(0); | ||
| face.setName(seed.name()); | ||
| face.setManaCost(seed.manaCost()); | ||
| face.setTypeLine(seed.typeLine()); | ||
| face.setOracleText(seed.oracleText()); | ||
| face.setColorsJson(seed.colorsJson()); | ||
| face.setNormalImageUri(seed.normalImageUri()); | ||
| face.setSmallImageUri(seed.normalImageUri()); | ||
| face.setLargeImageUri(seed.normalImageUri()); | ||
|
|
||
| card.getFaces().add(face); | ||
| cardRepository.save(card); | ||
| return true; | ||
| } | ||
|
|
||
| private record CardSeed( | ||
| String scryfallId, | ||
| String name, | ||
| String manaCost, | ||
| int cmc, | ||
| String typeLine, | ||
| String oracleText, | ||
| String colorsJson, | ||
| String normalImageUri | ||
| ) { | ||
| } | ||
| } |
189 changes: 189 additions & 0 deletions
189
backend/src/main/java/com/magicvs/backend/controller/CardControllerCardsExamples.java
This file contains hidden or 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,189 @@ | ||
| package com.magicvs.backend.controller; | ||
|
|
||
| import com.magicvs.backend.model.Card; | ||
| import com.magicvs.backend.repository.CardRepository; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/cards") | ||
| @CrossOrigin(origins = "http://localhost:4200") | ||
| @Transactional(readOnly = true) | ||
| public class CardControllerCardsExamples { | ||
|
|
||
| private final CardRepository cardRepository; | ||
|
|
||
| public CardControllerCardsExamples(CardRepository cardRepository) { | ||
| this.cardRepository = cardRepository; | ||
| } | ||
|
|
||
| /** | ||
| * Busca cartas por nombre | ||
| * GET /api/cards/search?name=query&limit=20 | ||
| */ | ||
| @GetMapping("/search") | ||
| public ResponseEntity<List<CardSearchResponse>> searchCards( | ||
| @RequestParam String name, | ||
| @RequestParam(defaultValue = "20") int limit) { | ||
|
|
||
| if (name == null || name.trim().isEmpty()) { | ||
| return new ResponseEntity<>(List.of(), HttpStatus.OK); | ||
| } | ||
|
|
||
| Pageable pageable = PageRequest.of(0, Math.min(limit, 50)); | ||
| List<CardSearchResponse> results = cardRepository | ||
| .searchProjectedByName(name, pageable) | ||
| .getContent() | ||
| .stream() | ||
| .map(card -> new CardSearchResponse( | ||
| card.getId(), | ||
| card.getName(), | ||
| card.getManaCost() == null ? "" : card.getManaCost(), | ||
| card.getTypeLine() == null ? "" : card.getTypeLine(), | ||
| "https://placehold.co/488x680/111827/e5e7eb?text=MagicVS", | ||
| extractColorsFromManaCost(card.getManaCost()) | ||
| )) | ||
| .toList(); | ||
|
|
||
| return new ResponseEntity<>(results, HttpStatus.OK); | ||
| } | ||
|
|
||
| private static List<String> extractColorsFromManaCost(String manaCost) { | ||
| if (manaCost == null || manaCost.isBlank()) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| String value = manaCost.toUpperCase(Locale.ROOT); | ||
| List<String> colors = new ArrayList<>(); | ||
|
|
||
| if (value.contains("{W}")) colors.add("W"); | ||
| if (value.contains("{U}")) colors.add("U"); | ||
| if (value.contains("{B}")) colors.add("B"); | ||
| if (value.contains("{R}")) colors.add("R"); | ||
| if (value.contains("{G}")) colors.add("G"); | ||
|
|
||
| return colors; | ||
| } | ||
|
|
||
| /** | ||
| * Obtiene una carta por ID | ||
| * GET /api/cards/:cardId | ||
| */ | ||
| @GetMapping("/{cardId}") | ||
| public ResponseEntity<?> getCardById(@PathVariable Long cardId) { | ||
| var card = cardRepository.findById(cardId); | ||
| if (card.isPresent()) { | ||
| return new ResponseEntity<>(card.get(), HttpStatus.OK); | ||
| } else { | ||
| return new ResponseEntity<>(new ErrorResponse("Carta no encontrada"), HttpStatus.NOT_FOUND); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Obtiene todas las cartas con paginación | ||
| * GET /api/cards?page=0&size=20 | ||
| */ | ||
| @GetMapping | ||
| public ResponseEntity<List<Card>> getAllCards( | ||
| @RequestParam(defaultValue = "0") int page, | ||
| @RequestParam(defaultValue = "20") int size) { | ||
|
|
||
| Pageable pageable = PageRequest.of(page, Math.min(size, 100)); | ||
| List<Card> cards = cardRepository.findAll(pageable).getContent(); | ||
|
|
||
| return new ResponseEntity<>(cards, HttpStatus.OK); | ||
| } | ||
|
|
||
| /** | ||
| * Clase interna para errores | ||
| */ | ||
| static class ErrorResponse { | ||
| private String message; | ||
|
|
||
| public ErrorResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| public void setMessage(String message) { | ||
| this.message = message; | ||
| } | ||
| } | ||
|
|
||
| static class CardSearchResponse { | ||
| private Long id; | ||
| private String name; | ||
| private String manaCost; | ||
| private String type; | ||
| private String imageUrl; | ||
| private List<String> colors; | ||
|
|
||
| public CardSearchResponse(Long id, String name, String manaCost, String type, String imageUrl, List<String> colors) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.manaCost = manaCost; | ||
| this.type = type; | ||
| this.imageUrl = imageUrl; | ||
| this.colors = colors; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getManaCost() { | ||
| return manaCost; | ||
| } | ||
|
|
||
| public void setManaCost(String manaCost) { | ||
| this.manaCost = manaCost; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public void setType(String type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getImageUrl() { | ||
| return imageUrl; | ||
| } | ||
|
|
||
| public void setImageUrl(String imageUrl) { | ||
| this.imageUrl = imageUrl; | ||
| } | ||
|
|
||
| public List<String> getColors() { | ||
| return colors; | ||
| } | ||
|
|
||
| public void setColors(List<String> colors) { | ||
| this.colors = colors; | ||
| } | ||
| } | ||
| } |
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.
Cambiale el nombre para que tenga Controller al final: Ej. CardsExamplesController.java