├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── main │ ├── java │ │ └── com │ │ │ └── pd │ │ │ └── Pokedex │ │ │ ├── infrastructure │ │ │ ├── exception │ │ │ │ ├── NoDataFoundException.java │ │ │ │ ├── PhotoNotFoundException.java │ │ │ │ ├── TypeNotFoundException.java │ │ │ │ ├── PokemonNotFoundException.java │ │ │ │ └── PokemonAlreadyExistsException.java │ │ │ ├── output │ │ │ │ ├── jpa │ │ │ │ │ ├── repository │ │ │ │ │ │ ├── ITypeRepository.java │ │ │ │ │ │ └── IPokemonRepository.java │ │ │ │ │ ├── entity │ │ │ │ │ │ ├── TypeEntity.java │ │ │ │ │ │ └── PokemonEntity.java │ │ │ │ │ ├── mapper │ │ │ │ │ │ ├── TypeEntityMapper.java │ │ │ │ │ │ └── PokemonEntityMapper.java │ │ │ │ │ └── adapter │ │ │ │ │ │ ├── TypeJpaAdapter.java │ │ │ │ │ │ └── PokemonJpaAdapter.java │ │ │ │ └── mongodb │ │ │ │ │ ├── repository │ │ │ │ │ └── IPhotoRepository.java │ │ │ │ │ ├── entity │ │ │ │ │ └── PhotoEntity.java │ │ │ │ │ ├── mapper │ │ │ │ │ └── PhotoEntityMapper.java │ │ │ │ │ └── adapter │ │ │ │ │ └── PhotoMongodbAdapter.java │ │ │ ├── exceptionhandler │ │ │ │ ├── ExceptionResponse.java │ │ │ │ └── ControllerAdvisor.java │ │ │ ├── configuration │ │ │ │ └── BeanConfiguration.java │ │ │ └── input │ │ │ │ └── rest │ │ │ │ └── PokedexRestController.java │ │ │ ├── application │ │ │ ├── dto │ │ │ │ ├── TypeDto.java │ │ │ │ ├── PokedexResponse.java │ │ │ │ └── PokedexRequest.java │ │ │ ├── mapper │ │ │ │ ├── TypeDtoMapper.java │ │ │ │ ├── PokedexRequestMapper.java │ │ │ │ └── PokedexResponseMapper.java │ │ │ └── handler │ │ │ │ ├── IPokedexHandler.java │ │ │ │ └── PokedexHandler.java │ │ │ ├── PokedexApplication.java │ │ │ └── domain │ │ │ ├── api │ │ │ ├── ITypeServicePort.java │ │ │ ├── IPhotoServicePort.java │ │ │ └── IPokemonServicePort.java │ │ │ ├── spi │ │ │ ├── ITypePersistencePort.java │ │ │ ├── IPhotoPersistencePort.java │ │ │ └── IPokemonPersistencePort.java │ │ │ ├── model │ │ │ ├── Photo.java │ │ │ ├── Type.java │ │ │ └── Pokemon.java │ │ │ └── usecase │ │ │ ├── TypeUseCase.java │ │ │ ├── PhotoUseCase.java │ │ │ └── PokemonUseCase.java │ └── resources │ │ └── application.yml └── test │ └── java │ └── com │ └── pd │ └── Pokedex │ └── PokedexApplicationTests.java ├── Dockerfile ├── .gitignore ├── README.md ├── docker-compose.yml ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Pokedex' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jocrugon/Pokedex/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exception/NoDataFoundException.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exception; 2 | 3 | public class NoDataFoundException extends RuntimeException{ 4 | 5 | public NoDataFoundException(){ 6 | super(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exception/PhotoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exception; 2 | 3 | public class PhotoNotFoundException extends RuntimeException{ 4 | public PhotoNotFoundException(){ 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exception/TypeNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exception; 2 | 3 | public class TypeNotFoundException extends RuntimeException{ 4 | public TypeNotFoundException(){ 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exception/PokemonNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exception; 2 | 3 | public class PokemonNotFoundException extends RuntimeException{ 4 | public PokemonNotFoundException(){ 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/dto/TypeDto.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.dto; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | @Getter 7 | @Setter 8 | public class TypeDto { 9 | private String firstType; 10 | private String secondType; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exception/PokemonAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exception; 2 | 3 | public class PokemonAlreadyExistsException extends RuntimeException{ 4 | public PokemonAlreadyExistsException(){ 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /src/test/java/com/pd/Pokedex/PokedexApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class PokedexApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/dto/PokedexResponse.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.dto; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | @Getter 7 | @Setter 8 | public class PokedexResponse { 9 | private Long number; 10 | private String name; 11 | private TypeDto type; 12 | private String photo; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/repository/ITypeRepository.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.repository; 2 | 3 | import com.pd.Pokedex.infrastructure.output.jpa.entity.TypeEntity; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface ITypeRepository extends JpaRepository { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/mongodb/repository/IPhotoRepository.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.mongodb.repository; 2 | 3 | import com.pd.Pokedex.infrastructure.output.mongodb.entity.PhotoEntity; 4 | import org.springframework.data.mongodb.repository.MongoRepository; 5 | 6 | public interface IPhotoRepository extends MongoRepository { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/dto/PokedexRequest.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.dto; 2 | 3 | import com.pd.Pokedex.domain.model.Type; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class PokedexRequest { 10 | private Long number; 11 | private String name; 12 | private Type type; 13 | private String photo; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/PokedexApplication.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class PokedexApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(PokedexApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/api/ITypeServicePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.api; 2 | 3 | import com.pd.Pokedex.domain.model.Type; 4 | 5 | import java.util.List; 6 | 7 | public interface ITypeServicePort { 8 | Type saveType(Type type); 9 | List getAllType(); 10 | Type getType(Long typeNumber); 11 | void updateType(Type type); 12 | void deleteType(Long typeNumber); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/spi/ITypePersistencePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.spi; 2 | 3 | import com.pd.Pokedex.domain.model.Type; 4 | 5 | import java.util.List; 6 | 7 | public interface ITypePersistencePort { 8 | Type saveType(Type type); 9 | List getAllType(); 10 | Type getType(Long typeNumber); 11 | void updateType(Type type); 12 | void deleteType(Long typeNumber); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/api/IPhotoServicePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.api; 2 | 3 | import com.pd.Pokedex.domain.model.Photo; 4 | 5 | import java.util.List; 6 | 7 | public interface IPhotoServicePort { 8 | Photo savePhoto(Photo photo); 9 | List getAllPhotos(); 10 | Photo getPhoto(String photoNumber); 11 | void updatePhoto(Photo Photo); 12 | void deletePhoto(String photoNumber); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/spi/IPhotoPersistencePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.spi; 2 | 3 | import com.pd.Pokedex.domain.model.Photo; 4 | 5 | import java.util.List; 6 | 7 | public interface IPhotoPersistencePort { 8 | Photo savePhoto(Photo photo); 9 | List getAllPhotos(); 10 | Photo getPhoto(String photoNumber); 11 | void updatePhoto(Photo photo); 12 | void deletePhoto(String photoNumber); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/mongodb/entity/PhotoEntity.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.mongodb.entity; 2 | 3 | 4 | import lombok.Data; 5 | import org.springframework.data.annotation.Id; 6 | import org.springframework.data.mongodb.core.mapping.Document; 7 | 8 | @Document(collection = "photo") 9 | @Data 10 | public class PhotoEntity { 11 | @Id 12 | private String id; 13 | private byte[] photo; 14 | } -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/api/IPokemonServicePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.api; 2 | 3 | import com.pd.Pokedex.domain.model.Pokemon; 4 | 5 | import java.util.List; 6 | 7 | public interface IPokemonServicePort { 8 | void savePokemon(Pokemon pokemon); 9 | List getAllPokemon(); 10 | Pokemon getPokemon(Long pokemonNumber); 11 | void updatePokemon(Pokemon pokemon); 12 | void deletePokemon(Long pokemonNumber); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/spi/IPokemonPersistencePort.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.spi; 2 | 3 | import com.pd.Pokedex.domain.model.Pokemon; 4 | 5 | import java.util.List; 6 | 7 | public interface IPokemonPersistencePort { 8 | void savePokemon(Pokemon pokemon); 9 | List getAllPokemon(); 10 | Pokemon getPokemon(Long pokemonNumber); 11 | void updatePokemon(Pokemon pokemon); 12 | void deletePokemon(Long pokemonNumber); 13 | } 14 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Usa la imagen oficial de OpenJDK con Java 17 como base 2 | FROM openjdk:17 3 | 4 | # Define el directorio de trabajo dentro del contenedor 5 | WORKDIR /app 6 | 7 | # Copia el archivo JAR construido por Gradle a la imagen 8 | COPY build/libs/Pokedex-0.0.1-SNAPSHOT.jar app.jar 9 | 10 | # Expone el puerto en el que se ejecuta tu aplicación 11 | EXPOSE 8091 12 | 13 | # Comando para ejecutar la aplicación al iniciar el contenedor 14 | CMD ["java", "-jar", "app.jar"] 15 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/mapper/TypeDtoMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.mapper; 2 | 3 | import com.pd.Pokedex.application.dto.TypeDto; 4 | import com.pd.Pokedex.domain.model.Type; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.ReportingPolicy; 7 | 8 | @Mapper(componentModel = "spring", 9 | unmappedSourcePolicy = ReportingPolicy.IGNORE, 10 | unmappedTargetPolicy = ReportingPolicy.IGNORE) 11 | public interface TypeDtoMapper { 12 | TypeDto toDto(Type type); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/repository/IPokemonRepository.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.repository; 2 | 3 | import com.pd.Pokedex.infrastructure.output.jpa.entity.PokemonEntity; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.Optional; 7 | 8 | public interface IPokemonRepository extends JpaRepository { 9 | Optional findByNumber(Long pokemonNumber); 10 | 11 | void deleteByNumber(Long pokemonNumber); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8091 3 | 4 | spring: 5 | datasource: 6 | url: ${MYSQL_URL} 7 | username: ${MYSQL_USERNAME} 8 | password: ${MYSQL_PASSWORD} 9 | jpa: 10 | hibernate: 11 | ddl-auto: update 12 | show-sql: true 13 | 14 | data: 15 | mongodb: 16 | host: ${MONGO_HOST} 17 | port: ${MONGO_PORT} 18 | database: ${MONGO_DATABASE} 19 | 20 | appDescription: "Pokedex API" 21 | appVersion: "1.0.0" 22 | 23 | #swagger http://localhost:8091/swagger-ui/index.html 24 | 25 | springdoc: 26 | swagger-ui: 27 | path: /index.html -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/entity/TypeEntity.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | @Entity 10 | @Table(name = "type") 11 | @NoArgsConstructor 12 | @AllArgsConstructor 13 | @Getter 14 | @Setter 15 | public class TypeEntity { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | private String firstType; 21 | private String secondType; 22 | } -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/model/Photo.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.model; 2 | 3 | public class Photo { 4 | private String id; 5 | private byte[] photo; 6 | 7 | public Photo(String id, byte[] photo){ 8 | this.id = id; 9 | this.photo = photo; 10 | } 11 | 12 | public String getId() { 13 | return id; 14 | } 15 | 16 | public void setId(String id) { 17 | this.id = id; 18 | } 19 | 20 | public byte[] getPhoto() { 21 | return photo; 22 | } 23 | 24 | public void setPhoto(byte[] photo) { 25 | this.photo = photo; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/handler/IPokedexHandler.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.handler; 2 | 3 | import com.pd.Pokedex.application.dto.PokedexRequest; 4 | import com.pd.Pokedex.application.dto.PokedexResponse; 5 | 6 | import java.util.List; 7 | 8 | public interface IPokedexHandler { 9 | void savePokemonInPokedex(PokedexRequest pokedexRequest); 10 | List getAllPokemonFromPokedex(); 11 | PokedexResponse getPokemonFromPokedex(Long pokemonNumber); 12 | void updatePokemonInPokedex(PokedexRequest pokedexRequest); 13 | void deletePokemonFromPokedex(Long pokemonNumber); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/entity/PokemonEntity.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.entity; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | @Entity 10 | @Table(name = "Pokemon") 11 | @NoArgsConstructor 12 | @AllArgsConstructor 13 | @Getter 14 | @Setter 15 | public class PokemonEntity { 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | private Long id; 19 | private Long number; 20 | private String name; 21 | private Long typeId; 22 | private String photoId; 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pokedex 2 | API desarrollado en java con spring boot, usando arquitectura hexagonal, swagger, manejador de excepciones y 2 bases de datos MySQL y MongoDB 3 | las bases de datos están en el archivo docker-compose, también la creación de la imagen del API indicado en el Dockerfile 4 | 5 | - SWAGGER 6 | 7 | ![imagen](https://github.com/jocrugon/Pokedex/assets/93726141/5ddeaab8-61ac-41cb-b435-452ebdf30091) 8 | 9 | - Arquitectura Hexagonal 10 | 11 | ![imagen](https://github.com/jocrugon/Pokedex/assets/93726141/b692b471-88d9-43e6-9a75-4e8732a4cf4e) 12 | 13 | - Docker 14 | 15 | ![imagen](https://github.com/jocrugon/Pokedex/assets/93726141/49cfcd81-be7a-4594-88db-892525dd5bd2) 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/mapper/TypeEntityMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.mapper; 2 | 3 | import com.pd.Pokedex.domain.model.Type; 4 | import com.pd.Pokedex.infrastructure.output.jpa.entity.TypeEntity; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.ReportingPolicy; 7 | 8 | import java.util.List; 9 | 10 | @Mapper(componentModel = "spring", 11 | unmappedTargetPolicy = ReportingPolicy.IGNORE, 12 | unmappedSourcePolicy = ReportingPolicy.IGNORE) 13 | public interface TypeEntityMapper { 14 | TypeEntity toEntity(Type type); 15 | 16 | Type toType(TypeEntity typeEntity); 17 | 18 | List toTypeList(List typeEntityList); 19 | } -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exceptionhandler/ExceptionResponse.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exceptionhandler; 2 | 3 | public enum ExceptionResponse { 4 | POKEMON_NOT_FOUND("No Pokemon was found with that number"), 5 | POKEMON_ALREADY_EXISTS("There is already a pokemon with that number"), 6 | TYPE_NOT_FOUND("No type was found for a pokemon"), 7 | NO_DATA_FOUND("No data found for the requested petition"), 8 | PHOTO_NOT_FOUND("No photo was found for a pokemon"); 9 | 10 | private String message; 11 | 12 | ExceptionResponse(String message){ 13 | this.message = message; 14 | } 15 | 16 | public String getMessage(){ 17 | return this.message; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/mongodb/mapper/PhotoEntityMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.mongodb.mapper; 2 | 3 | 4 | import com.pd.Pokedex.domain.model.Photo; 5 | import com.pd.Pokedex.infrastructure.output.mongodb.entity.PhotoEntity; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | 9 | import java.util.List; 10 | 11 | @Mapper(componentModel = "spring", 12 | unmappedSourcePolicy = ReportingPolicy.IGNORE, 13 | unmappedTargetPolicy = ReportingPolicy.IGNORE) 14 | public interface PhotoEntityMapper { 15 | 16 | PhotoEntity toEntity(Photo photo); 17 | Photo toPhoto(PhotoEntity photoEntity); 18 | List toPhotoList(List photoEntityList); 19 | } -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/mapper/PokemonEntityMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.mapper; 2 | 3 | import com.pd.Pokedex.domain.model.Pokemon; 4 | import com.pd.Pokedex.infrastructure.output.jpa.entity.PokemonEntity; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.ReportingPolicy; 7 | 8 | import java.util.List; 9 | 10 | @Mapper(componentModel = "spring", 11 | unmappedTargetPolicy = ReportingPolicy.IGNORE, 12 | unmappedSourcePolicy = ReportingPolicy.IGNORE) 13 | public interface PokemonEntityMapper { 14 | 15 | PokemonEntity toEntity(Pokemon pokemon); 16 | 17 | Pokemon toPokemon(PokemonEntity pokemonEntity); 18 | 19 | List toPokemonList(List pokemonEntityList); 20 | } -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/model/Type.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.model; 2 | 3 | public class Type { 4 | private Long id; 5 | private String firstType; 6 | private String secondType; 7 | 8 | public Type(Long id, String firstType, String secondType) { 9 | this.id = id; 10 | this.firstType = firstType; 11 | this.secondType = secondType; 12 | } 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Long id) { 19 | this.id = id; 20 | } 21 | 22 | public String getFirstType() { 23 | return firstType; 24 | } 25 | 26 | public void setFirstType(String firstType) { 27 | this.firstType = firstType; 28 | } 29 | 30 | public String getSecondType() { 31 | return secondType; 32 | } 33 | 34 | public void setSecondType(String secondType) { 35 | this.secondType = secondType; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | ### MySQL database 4 | db-pokedex: 5 | container_name: db-pokedex 6 | image: mysql:latest 7 | restart: unless-stopped 8 | environment: 9 | MYSQL_DATABASE: pokedex 10 | MYSQL_USER: user 11 | MYSQL_PASSWORD: user 12 | MYSQL_ROOT_PASSWORD: root 13 | ports: 14 | - 3310:3306 15 | ### MongoDB database 16 | mongodb-pokedex: 17 | container_name: mongodb-pokedex 18 | image: mongo:latest 19 | restart: unless-stopped 20 | environment: 21 | MONGO_INITDB_DATABASE: pokedex 22 | ports: 23 | - 27017:27017 24 | ### Spring Boot application 25 | spring-app: 26 | container_name: spring-app 27 | image: dacrugon22/pokedex:latest 28 | ports: 29 | - 8091:8091 30 | environment: 31 | MYSQL_URL: jdbc:mysql://db-pokedex:3306/pokedex 32 | MYSQL_USERNAME: user 33 | MYSQL_PASSWORD: user 34 | MONGO_HOST: mongodb-pokedex 35 | MONGO_PORT: 27017 36 | MONGO_DATABASE: pokedex 37 | depends_on: 38 | - db-pokedex 39 | - mongodb-pokedex -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/usecase/TypeUseCase.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.usecase; 2 | 3 | import com.pd.Pokedex.domain.api.ITypeServicePort; 4 | import com.pd.Pokedex.domain.model.Type; 5 | import com.pd.Pokedex.domain.spi.ITypePersistencePort; 6 | 7 | import java.util.List; 8 | 9 | public class TypeUseCase implements ITypeServicePort { 10 | private final ITypePersistencePort typePersistencePort; 11 | 12 | public TypeUseCase(ITypePersistencePort typePersistencePort) { 13 | this.typePersistencePort = typePersistencePort; 14 | } 15 | 16 | @Override 17 | public Type saveType(Type type) { 18 | return typePersistencePort.saveType(type); 19 | } 20 | 21 | @Override 22 | public List getAllType() { 23 | return typePersistencePort.getAllType(); 24 | } 25 | 26 | @Override 27 | public Type getType(Long typeNumber) { 28 | return typePersistencePort.getType(typeNumber); 29 | } 30 | 31 | @Override 32 | public void updateType(Type type) { 33 | typePersistencePort.updateType(type); 34 | } 35 | 36 | @Override 37 | public void deleteType(Long typeNumber) { 38 | typePersistencePort.deleteType(typeNumber); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/mapper/PokedexRequestMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.mapper; 2 | 3 | import com.pd.Pokedex.application.dto.PokedexRequest; 4 | import com.pd.Pokedex.domain.model.Photo; 5 | import com.pd.Pokedex.domain.model.Pokemon; 6 | import com.pd.Pokedex.domain.model.Type; 7 | import org.mapstruct.Mapper; 8 | import org.mapstruct.Mapping; 9 | import org.mapstruct.Named; 10 | import org.mapstruct.ReportingPolicy; 11 | 12 | import java.util.Base64; 13 | 14 | @Mapper(componentModel = "spring", 15 | unmappedSourcePolicy = ReportingPolicy.IGNORE, 16 | unmappedTargetPolicy = ReportingPolicy.IGNORE) 17 | public interface PokedexRequestMapper { 18 | Pokemon toPokemon(PokedexRequest pokedexRequest); 19 | 20 | @Mapping(target = "firstType",source = "type.firstType") 21 | @Mapping(target = "secondType",source = "type.secondType") 22 | Type toType(PokedexRequest pokedexRequest); 23 | 24 | @Mapping(target = "photo", qualifiedByName = "base64ToByteArray") 25 | Photo toPhoto(PokedexRequest pokedexRequest); 26 | 27 | @Named("base64ToByteArray") 28 | static byte[] base64ToByteArray(String base64Photo){ 29 | return Base64.getDecoder().decode(base64Photo); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/usecase/PhotoUseCase.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.usecase; 2 | 3 | import com.pd.Pokedex.domain.api.IPhotoServicePort; 4 | import com.pd.Pokedex.domain.model.Photo; 5 | import com.pd.Pokedex.domain.spi.IPhotoPersistencePort; 6 | 7 | import java.util.List; 8 | 9 | public class PhotoUseCase implements IPhotoServicePort { 10 | private final IPhotoPersistencePort photoPersistencePort; 11 | 12 | public PhotoUseCase(IPhotoPersistencePort PhotoPersistencePort) { 13 | this.photoPersistencePort = PhotoPersistencePort; 14 | } 15 | 16 | @Override 17 | public Photo savePhoto(Photo photo) { 18 | return photoPersistencePort.savePhoto(photo); 19 | } 20 | 21 | @Override 22 | public List getAllPhotos() { 23 | return photoPersistencePort.getAllPhotos(); 24 | } 25 | 26 | @Override 27 | public Photo getPhoto(String photoNumber) { 28 | return photoPersistencePort.getPhoto(photoNumber); 29 | } 30 | 31 | @Override 32 | public void updatePhoto(Photo Photo) { 33 | photoPersistencePort.updatePhoto(Photo); 34 | } 35 | 36 | @Override 37 | public void deletePhoto(String photoNumber) { 38 | photoPersistencePort.deletePhoto(photoNumber); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/model/Pokemon.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.model; 2 | 3 | public class Pokemon { 4 | private Long id; 5 | private Long number; 6 | private String name; 7 | private Long typeId; 8 | private String photoId; 9 | 10 | public Pokemon(Long id, Long number, String name, Long typeId, String photoId) { 11 | this.id = id; 12 | this.number = number; 13 | this.name = name; 14 | this.typeId = typeId; 15 | this.photoId = photoId; 16 | } 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public Long getNumber() { 27 | return number; 28 | } 29 | 30 | public void setNumber(Long number) { 31 | this.number = number; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public Long getTypeId() { 43 | return typeId; 44 | } 45 | 46 | public void setTypeId(Long typeId) { 47 | this.typeId = typeId; 48 | } 49 | 50 | public String getPhotoId() { 51 | return photoId; 52 | } 53 | 54 | public void setPhotoId(String photoId) { 55 | this.photoId = photoId; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/domain/usecase/PokemonUseCase.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.domain.usecase; 2 | 3 | import com.pd.Pokedex.domain.api.IPokemonServicePort; 4 | import com.pd.Pokedex.domain.model.Pokemon; 5 | import com.pd.Pokedex.domain.spi.IPokemonPersistencePort; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import java.util.List; 9 | 10 | public class PokemonUseCase implements IPokemonServicePort { 11 | private final IPokemonPersistencePort pokemonPersistencePort; 12 | 13 | public PokemonUseCase(IPokemonPersistencePort pokemonPersistencePort) { 14 | this.pokemonPersistencePort = pokemonPersistencePort; 15 | } 16 | 17 | @Override 18 | public void savePokemon(Pokemon pokemon) { 19 | pokemonPersistencePort.savePokemon(pokemon); 20 | } 21 | 22 | @Override 23 | public List getAllPokemon() { 24 | return pokemonPersistencePort.getAllPokemon(); 25 | } 26 | 27 | @Override 28 | public Pokemon getPokemon(Long pokemonNumber) { 29 | return pokemonPersistencePort.getPokemon(pokemonNumber); 30 | } 31 | 32 | @Override 33 | public void updatePokemon(Pokemon pokemon) { 34 | pokemonPersistencePort.updatePokemon(pokemon); 35 | } 36 | 37 | @Override 38 | public void deletePokemon(Long pokemonNumber) { 39 | pokemonPersistencePort.deletePokemon(pokemonNumber); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/adapter/TypeJpaAdapter.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.adapter; 2 | 3 | import com.pd.Pokedex.domain.model.Type; 4 | import com.pd.Pokedex.domain.spi.ITypePersistencePort; 5 | import com.pd.Pokedex.infrastructure.exception.NoDataFoundException; 6 | import com.pd.Pokedex.infrastructure.exception.TypeNotFoundException; 7 | import com.pd.Pokedex.infrastructure.output.jpa.entity.TypeEntity; 8 | import com.pd.Pokedex.infrastructure.output.jpa.mapper.TypeEntityMapper; 9 | import com.pd.Pokedex.infrastructure.output.jpa.repository.ITypeRepository; 10 | import lombok.RequiredArgsConstructor; 11 | 12 | import java.util.List; 13 | 14 | @RequiredArgsConstructor 15 | 16 | public class TypeJpaAdapter implements ITypePersistencePort { 17 | 18 | private final ITypeRepository typeRepository; 19 | private final TypeEntityMapper typeEntityMapper; 20 | 21 | @Override 22 | public Type saveType(Type type) { 23 | return typeEntityMapper.toType(typeRepository.save(typeEntityMapper.toEntity(type))); 24 | } 25 | 26 | @Override 27 | public List getAllType() { 28 | List typeEntityList = typeRepository.findAll(); 29 | if(typeEntityList.isEmpty()){ 30 | throw new NoDataFoundException(); 31 | } 32 | return typeEntityMapper.toTypeList(typeEntityList); 33 | } 34 | 35 | @Override 36 | public Type getType(Long typeNumber) { 37 | 38 | return typeEntityMapper.toType(typeRepository.findById(typeNumber).orElseThrow(TypeNotFoundException::new)); 39 | } 40 | 41 | @Override 42 | public void updateType(Type type) { 43 | typeRepository.save(typeEntityMapper.toEntity(type)); 44 | } 45 | 46 | @Override 47 | public void deleteType(Long typeNumber) { 48 | typeRepository.deleteById(typeNumber); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/mongodb/adapter/PhotoMongodbAdapter.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.mongodb.adapter; 2 | 3 | import com.pd.Pokedex.domain.model.Photo; 4 | import com.pd.Pokedex.domain.spi.IPhotoPersistencePort; 5 | import com.pd.Pokedex.infrastructure.exception.NoDataFoundException; 6 | import com.pd.Pokedex.infrastructure.exception.PhotoNotFoundException; 7 | import com.pd.Pokedex.infrastructure.output.mongodb.entity.PhotoEntity; 8 | import com.pd.Pokedex.infrastructure.output.mongodb.mapper.PhotoEntityMapper; 9 | import com.pd.Pokedex.infrastructure.output.mongodb.repository.IPhotoRepository; 10 | import lombok.RequiredArgsConstructor; 11 | 12 | import java.util.List; 13 | 14 | @RequiredArgsConstructor 15 | public class PhotoMongodbAdapter implements IPhotoPersistencePort { 16 | 17 | private final IPhotoRepository photoRepository; 18 | private final PhotoEntityMapper photoEntityMapper; 19 | 20 | @Override 21 | public Photo savePhoto(Photo photo) { 22 | return photoEntityMapper.toPhoto(photoRepository.save(photoEntityMapper.toEntity(photo))); 23 | } 24 | 25 | @Override 26 | public List getAllPhotos() { 27 | List photoEntityList = photoRepository.findAll(); 28 | if (photoEntityList.isEmpty()) { 29 | throw new NoDataFoundException(); 30 | } 31 | return photoEntityMapper.toPhotoList(photoEntityList); 32 | } 33 | 34 | @Override 35 | public Photo getPhoto(String photoId) { 36 | return photoEntityMapper.toPhoto(photoRepository.findById(photoId).orElseThrow(PhotoNotFoundException::new)); 37 | } 38 | 39 | @Override 40 | public void updatePhoto(Photo photo) { 41 | photoRepository.save(photoEntityMapper.toEntity(photo)); 42 | } 43 | 44 | @Override 45 | public void deletePhoto(String photoId) { 46 | photoRepository.deleteById(photoId); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/output/jpa/adapter/PokemonJpaAdapter.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.output.jpa.adapter; 2 | 3 | import com.pd.Pokedex.domain.model.Pokemon; 4 | import com.pd.Pokedex.domain.spi.IPokemonPersistencePort; 5 | import com.pd.Pokedex.infrastructure.exception.NoDataFoundException; 6 | import com.pd.Pokedex.infrastructure.exception.PokemonAlreadyExistsException; 7 | import com.pd.Pokedex.infrastructure.exception.PokemonNotFoundException; 8 | import com.pd.Pokedex.infrastructure.output.jpa.entity.PokemonEntity; 9 | import com.pd.Pokedex.infrastructure.output.jpa.mapper.PokemonEntityMapper; 10 | import com.pd.Pokedex.infrastructure.output.jpa.repository.IPokemonRepository; 11 | import lombok.RequiredArgsConstructor; 12 | 13 | import java.util.List; 14 | 15 | @RequiredArgsConstructor 16 | public class PokemonJpaAdapter implements IPokemonPersistencePort { 17 | 18 | private final IPokemonRepository pokemonRepository; 19 | private final PokemonEntityMapper pokemonEntityMapper; 20 | 21 | @Override 22 | public void savePokemon(Pokemon pokemon) { 23 | if(pokemonRepository.findByNumber(pokemon.getNumber()).isPresent()){ 24 | throw new PokemonAlreadyExistsException(); 25 | } 26 | pokemonRepository.save(pokemonEntityMapper.toEntity(pokemon)); 27 | } 28 | 29 | @Override 30 | public List getAllPokemon() { 31 | List pokemonEntityList = pokemonRepository.findAll(); 32 | if(pokemonEntityList.isEmpty()){ 33 | throw new NoDataFoundException(); 34 | } 35 | return pokemonEntityMapper.toPokemonList(pokemonEntityList); 36 | } 37 | 38 | @Override 39 | public Pokemon getPokemon(Long pokemonNumber) { 40 | return pokemonEntityMapper.toPokemon(pokemonRepository.findByNumber(pokemonNumber).orElseThrow(PokemonNotFoundException::new)); 41 | } 42 | 43 | @Override 44 | public void updatePokemon(Pokemon pokemon) { 45 | pokemonRepository.save(pokemonEntityMapper.toEntity(pokemon)); 46 | } 47 | 48 | @Override 49 | public void deletePokemon(Long pokemonNumber) { 50 | pokemonRepository.deleteByNumber(pokemonNumber); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/mapper/PokedexResponseMapper.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.mapper; 2 | 3 | import com.pd.Pokedex.application.dto.PokedexResponse; 4 | import com.pd.Pokedex.domain.model.Photo; 5 | import com.pd.Pokedex.domain.model.Pokemon; 6 | import com.pd.Pokedex.domain.model.Type; 7 | import jdk.jfr.Name; 8 | import org.mapstruct.Mapper; 9 | import org.mapstruct.Mapping; 10 | import org.mapstruct.Named; 11 | import org.mapstruct.ReportingPolicy; 12 | import org.mapstruct.factory.Mappers; 13 | 14 | import java.util.Base64; 15 | import java.util.List; 16 | 17 | @Mapper(componentModel = "spring", 18 | unmappedSourcePolicy = ReportingPolicy.IGNORE, 19 | unmappedTargetPolicy = ReportingPolicy.IGNORE) 20 | public interface PokedexResponseMapper { 21 | TypeDtoMapper INSTANCE = Mappers.getMapper(TypeDtoMapper.class); 22 | @Mapping(source="type.firstType",target="type.firstType") 23 | @Mapping(source="type.secondType",target="type.secondType") 24 | @Mapping(target = "photo", qualifiedByName = "byteArrayToBase64") 25 | PokedexResponse toResponse(Pokemon pokemon, Type type, Photo photo); 26 | 27 | @Named("byteArrayToBase64") 28 | static String byteArrayToBase64(byte[] byteArrayPhoto){ 29 | return Base64.getEncoder().encodeToString(byteArrayPhoto); 30 | } 31 | 32 | default List toResponseList(List pokemonList, List typeList, List photoList){ 33 | return pokemonList.stream() 34 | .map(pokemon -> { 35 | PokedexResponse pokedexResponse = new PokedexResponse(); 36 | pokedexResponse.setNumber(pokemon.getNumber()); 37 | pokedexResponse.setName(pokemon.getName()); 38 | pokedexResponse.setType(INSTANCE.toDto(typeList.stream().filter(type -> 39 | type.getId().equals(pokemon.getTypeId())).findFirst().orElse(null))); 40 | pokedexResponse.setPhoto(byteArrayToBase64(photoList.stream().filter(photo -> 41 | photo.getId().equals(pokemon.getPhotoId())).findFirst().orElse(null).getPhoto())); 42 | return pokedexResponse; 43 | }).toList(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/exceptionhandler/ControllerAdvisor.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.exceptionhandler; 2 | 3 | import com.pd.Pokedex.infrastructure.exception.*; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.ControllerAdvice; 7 | import org.springframework.web.bind.annotation.ExceptionHandler; 8 | 9 | import java.util.Collections; 10 | import java.util.Map; 11 | 12 | @ControllerAdvice 13 | public class ControllerAdvisor { 14 | private static final String MESSAGE="Message"; 15 | 16 | @ExceptionHandler(PokemonAlreadyExistsException.class) 17 | public ResponseEntity> handlePokemonAlreadyExistsException( 18 | PokemonAlreadyExistsException pokemonAlreadyExistsException){ 19 | return ResponseEntity.status(HttpStatus.CONFLICT) 20 | .body(Collections.singletonMap(MESSAGE,ExceptionResponse.POKEMON_ALREADY_EXISTS.getMessage())); 21 | } 22 | 23 | @ExceptionHandler(NoDataFoundException.class) 24 | public ResponseEntity> handleNoDataFoundException( 25 | NoDataFoundException noDataFoundException) { 26 | return ResponseEntity.status(HttpStatus.NOT_FOUND) 27 | .body(Collections.singletonMap(MESSAGE, ExceptionResponse.NO_DATA_FOUND.getMessage())); 28 | } 29 | 30 | @ExceptionHandler(PokemonNotFoundException.class) 31 | public ResponseEntity> handlePokemonNotFoundException( 32 | PokemonNotFoundException pokemonNotFoundException) { 33 | return ResponseEntity.status(HttpStatus.NOT_FOUND) 34 | .body(Collections.singletonMap(MESSAGE, ExceptionResponse.POKEMON_NOT_FOUND.getMessage())); 35 | } 36 | 37 | @ExceptionHandler(TypeNotFoundException.class) 38 | public ResponseEntity> handleTypeNotFoundException( 39 | TypeNotFoundException typeNotFoundException) { 40 | return ResponseEntity.status(HttpStatus.NOT_FOUND) 41 | .body(Collections.singletonMap(MESSAGE, ExceptionResponse.TYPE_NOT_FOUND.getMessage())); 42 | } 43 | 44 | @ExceptionHandler(PhotoNotFoundException.class) 45 | public ResponseEntity> handlePhotoNotFoundException( 46 | PhotoNotFoundException photoNotFoundException) { 47 | return ResponseEntity.status(HttpStatus.NOT_FOUND) 48 | .body(Collections.singletonMap(MESSAGE, ExceptionResponse.PHOTO_NOT_FOUND.getMessage())); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/configuration/BeanConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.configuration; 2 | 3 | import com.pd.Pokedex.domain.api.IPhotoServicePort; 4 | import com.pd.Pokedex.domain.api.IPokemonServicePort; 5 | import com.pd.Pokedex.domain.api.ITypeServicePort; 6 | import com.pd.Pokedex.domain.spi.IPhotoPersistencePort; 7 | import com.pd.Pokedex.domain.spi.IPokemonPersistencePort; 8 | import com.pd.Pokedex.domain.spi.ITypePersistencePort; 9 | import com.pd.Pokedex.domain.usecase.PhotoUseCase; 10 | import com.pd.Pokedex.domain.usecase.PokemonUseCase; 11 | import com.pd.Pokedex.domain.usecase.TypeUseCase; 12 | import com.pd.Pokedex.infrastructure.output.jpa.adapter.PokemonJpaAdapter; 13 | import com.pd.Pokedex.infrastructure.output.jpa.adapter.TypeJpaAdapter; 14 | import com.pd.Pokedex.infrastructure.output.jpa.mapper.PokemonEntityMapper; 15 | import com.pd.Pokedex.infrastructure.output.jpa.mapper.TypeEntityMapper; 16 | import com.pd.Pokedex.infrastructure.output.jpa.repository.IPokemonRepository; 17 | import com.pd.Pokedex.infrastructure.output.jpa.repository.ITypeRepository; 18 | import com.pd.Pokedex.infrastructure.output.mongodb.adapter.PhotoMongodbAdapter; 19 | import com.pd.Pokedex.infrastructure.output.mongodb.mapper.PhotoEntityMapper; 20 | import com.pd.Pokedex.infrastructure.output.mongodb.repository.IPhotoRepository; 21 | import lombok.RequiredArgsConstructor; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.Configuration; 24 | 25 | @Configuration 26 | @RequiredArgsConstructor 27 | public class BeanConfiguration { 28 | private final IPokemonRepository pokemonRepository; 29 | private final PokemonEntityMapper pokemonEntityMapper; 30 | private final ITypeRepository typeRepository; 31 | private final TypeEntityMapper typeEntityMapper; 32 | private final IPhotoRepository photoRepository; 33 | private final PhotoEntityMapper photoEntityMapper; 34 | 35 | @Bean 36 | public IPokemonPersistencePort pokemonPersistencePort(){ 37 | return new PokemonJpaAdapter(pokemonRepository, pokemonEntityMapper); 38 | } 39 | 40 | @Bean 41 | public IPokemonServicePort pokemonServicePort(){ 42 | return new PokemonUseCase(pokemonPersistencePort()); 43 | } 44 | 45 | @Bean 46 | public ITypePersistencePort typePersistencePort() { 47 | return new TypeJpaAdapter(typeRepository, typeEntityMapper); 48 | } 49 | 50 | @Bean 51 | public ITypeServicePort typeServicePort() { 52 | return new TypeUseCase(typePersistencePort()); 53 | } 54 | @Bean 55 | public IPhotoPersistencePort photoPersistencePort() { 56 | return new PhotoMongodbAdapter(photoRepository, photoEntityMapper); 57 | } 58 | 59 | @Bean 60 | public IPhotoServicePort photoServicePort() { 61 | return new PhotoUseCase(photoPersistencePort()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/application/handler/PokedexHandler.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.application.handler; 2 | 3 | import com.pd.Pokedex.application.dto.PokedexRequest; 4 | import com.pd.Pokedex.application.dto.PokedexResponse; 5 | import com.pd.Pokedex.application.mapper.PokedexRequestMapper; 6 | import com.pd.Pokedex.application.mapper.PokedexResponseMapper; 7 | import com.pd.Pokedex.application.mapper.TypeDtoMapper; 8 | import com.pd.Pokedex.domain.api.IPhotoServicePort; 9 | import com.pd.Pokedex.domain.api.IPokemonServicePort; 10 | import com.pd.Pokedex.domain.api.ITypeServicePort; 11 | import com.pd.Pokedex.domain.model.Photo; 12 | import com.pd.Pokedex.domain.model.Pokemon; 13 | import com.pd.Pokedex.domain.model.Type; 14 | import lombok.RequiredArgsConstructor; 15 | import org.springframework.stereotype.Service; 16 | import org.springframework.transaction.annotation.Transactional; 17 | 18 | import java.util.List; 19 | @Service 20 | @RequiredArgsConstructor 21 | @Transactional 22 | public class PokedexHandler implements IPokedexHandler{ 23 | 24 | private final IPokemonServicePort pokemonServicePort; 25 | private final ITypeServicePort typeServicePort; 26 | private final IPhotoServicePort photoServicePort; 27 | private final PokedexRequestMapper pokedexRequestMapper; 28 | private final PokedexResponseMapper pokedexResponseMapper; 29 | private final TypeDtoMapper typeDtoMapper; 30 | @Override 31 | public void savePokemonInPokedex(PokedexRequest pokedexRequest) { 32 | Type type = typeServicePort.saveType(pokedexRequestMapper.toType(pokedexRequest)); 33 | Photo photo= photoServicePort.savePhoto(pokedexRequestMapper.toPhoto(pokedexRequest)); 34 | Pokemon pokemon = pokedexRequestMapper.toPokemon(pokedexRequest); 35 | pokemon.setTypeId(type.getId()); 36 | pokemon.setPhotoId(photo.getId()); 37 | pokemonServicePort.savePokemon(pokemon); 38 | } 39 | 40 | @Override 41 | public List getAllPokemonFromPokedex() { 42 | return pokedexResponseMapper.toResponseList(pokemonServicePort.getAllPokemon(),typeServicePort.getAllType(),photoServicePort.getAllPhotos()); 43 | } 44 | 45 | @Override 46 | public PokedexResponse getPokemonFromPokedex(Long pokemonNumber) { 47 | Pokemon pokemon = pokemonServicePort.getPokemon(pokemonNumber); 48 | return pokedexResponseMapper.toResponse(pokemon,typeServicePort.getType(pokemon.getTypeId()),photoServicePort.getPhoto(pokemon.getPhotoId())); 49 | } 50 | 51 | @Override 52 | public void updatePokemonInPokedex(PokedexRequest pokedexRequest) { 53 | Pokemon oldPokemon = pokemonServicePort.getPokemon(pokedexRequest.getNumber()); 54 | Type newType = pokedexRequestMapper.toType(pokedexRequest); 55 | Photo newPhoto = pokedexRequestMapper.toPhoto(pokedexRequest); 56 | newType.setId(oldPokemon.getTypeId()); 57 | newPhoto.setId(oldPokemon.getPhotoId()); 58 | typeServicePort.updateType(newType); 59 | photoServicePort.updatePhoto(newPhoto); 60 | 61 | Pokemon newPokemon = pokedexRequestMapper.toPokemon(pokedexRequest); 62 | newPokemon.setId(oldPokemon.getId()); 63 | newPokemon.setTypeId(oldPokemon.getTypeId()); 64 | newPokemon.setPhotoId(oldPokemon.getPhotoId()); 65 | 66 | pokemonServicePort.updatePokemon(newPokemon); 67 | } 68 | 69 | @Override 70 | public void deletePokemonFromPokedex(Long pokemonNumber) { 71 | Pokemon pokemon = pokemonServicePort.getPokemon(pokemonNumber); 72 | typeServicePort.deleteType(pokemon.getTypeId()); 73 | photoServicePort.deletePhoto(pokemon.getPhotoId()); 74 | pokemonServicePort.deletePokemon(pokemon.getId()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/pd/Pokedex/infrastructure/input/rest/PokedexRestController.java: -------------------------------------------------------------------------------- 1 | package com.pd.Pokedex.infrastructure.input.rest; 2 | 3 | import com.pd.Pokedex.application.dto.PokedexRequest; 4 | import com.pd.Pokedex.application.dto.PokedexResponse; 5 | import com.pd.Pokedex.application.handler.IPokedexHandler; 6 | import io.swagger.v3.oas.annotations.Operation; 7 | import io.swagger.v3.oas.annotations.media.ArraySchema; 8 | import io.swagger.v3.oas.annotations.media.Content; 9 | import io.swagger.v3.oas.annotations.media.Schema; 10 | import io.swagger.v3.oas.annotations.responses.ApiResponse; 11 | import io.swagger.v3.oas.annotations.responses.ApiResponses; 12 | import lombok.RequiredArgsConstructor; 13 | import org.hibernate.annotations.Parameter; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.*; 17 | 18 | import java.util.List; 19 | 20 | @RestController 21 | @RequestMapping("/pokedex") 22 | @RequiredArgsConstructor 23 | public class PokedexRestController { 24 | 25 | private final IPokedexHandler pokedexHandler; 26 | 27 | @Operation(summary = "Add a new Pokemon") 28 | @ApiResponses(value = { 29 | @ApiResponse(responseCode = "201", description = "Pokemon created", content = @Content), 30 | @ApiResponse(responseCode = "409", description = "Pokemon already exists", content = @Content) 31 | }) 32 | @PostMapping("/") 33 | public ResponseEntity savePokemonInPokedex(@RequestBody PokedexRequest pokedexRequest){ 34 | pokedexHandler.savePokemonInPokedex(pokedexRequest); 35 | return ResponseEntity.status(HttpStatus.CREATED).build(); 36 | } 37 | @Operation(summary = "Get all the pokemons") 38 | @ApiResponses(value = { 39 | @ApiResponse(responseCode = "200", description = "All pokemons returned", 40 | content = @Content(mediaType = "application/json", 41 | array = @ArraySchema(schema = @Schema(implementation = PokedexResponse.class)))), 42 | @ApiResponse(responseCode = "404", description = "No data found", content = @Content) 43 | }) 44 | @GetMapping("/") 45 | public ResponseEntity> getAllPokemonFromPokedex() { 46 | return ResponseEntity.ok(pokedexHandler.getAllPokemonFromPokedex()); 47 | } 48 | @Operation(summary = "Get a pokemon by their Number") 49 | @ApiResponses(value = { 50 | @ApiResponse(responseCode = "200", description = "Pokemon found", 51 | content = @Content(mediaType = "application/json", schema = @Schema(implementation = PokedexResponse.class))), 52 | @ApiResponse(responseCode = "404", description = "Pokemon not found", content = @Content) 53 | }) 54 | @GetMapping("/{number}") 55 | public ResponseEntity getPokemonFromPokedex(@PathVariable(name = "number") Long pokemonNumber) { 56 | return ResponseEntity.ok(pokedexHandler.getPokemonFromPokedex(pokemonNumber)); 57 | } 58 | @Operation(summary = "Update an existing pokemon") 59 | @ApiResponses(value = { 60 | @ApiResponse(responseCode = "200", description = "Pokemon updated", content = @Content), 61 | @ApiResponse(responseCode = "404", description = "Pokemon not found", content = @Content) 62 | }) 63 | @PutMapping("/") 64 | public ResponseEntity updatePokemonInPokedex(@RequestBody PokedexRequest pokedexRequest) { 65 | pokedexHandler.updatePokemonInPokedex(pokedexRequest); 66 | return ResponseEntity.noContent().build(); 67 | } 68 | @Operation(summary = "Delete a pokemon by their Number") 69 | @ApiResponses(value = { 70 | @ApiResponse(responseCode = "200", description = "Pokemon deleted", content = @Content), 71 | @ApiResponse(responseCode = "404", description = "Pokemon not found", content = @Content) 72 | }) 73 | @DeleteMapping("/{pokemonNumber}") 74 | public ResponseEntity deletePokemonFromPokedex(@PathVariable Long pokemonNumber) { 75 | pokedexHandler.deletePokemonFromPokedex(pokemonNumber); 76 | return ResponseEntity.noContent().build(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit 88 | 89 | # Use the maximum available, or set MAX_FD != -1 to use that value. 90 | MAX_FD=maximum 91 | 92 | warn () { 93 | echo "$*" 94 | } >&2 95 | 96 | die () { 97 | echo 98 | echo "$*" 99 | echo 100 | exit 1 101 | } >&2 102 | 103 | # OS specific support (must be 'true' or 'false'). 104 | cygwin=false 105 | msys=false 106 | darwin=false 107 | nonstop=false 108 | case "$( uname )" in #( 109 | CYGWIN* ) cygwin=true ;; #( 110 | Darwin* ) darwin=true ;; #( 111 | MSYS* | MINGW* ) msys=true ;; #( 112 | NONSTOP* ) nonstop=true ;; 113 | esac 114 | 115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 116 | 117 | 118 | # Determine the Java command to use to start the JVM. 119 | if [ -n "$JAVA_HOME" ] ; then 120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 121 | # IBM's JDK on AIX uses strange locations for the executables 122 | JAVACMD=$JAVA_HOME/jre/sh/java 123 | else 124 | JAVACMD=$JAVA_HOME/bin/java 125 | fi 126 | if [ ! -x "$JAVACMD" ] ; then 127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 128 | 129 | Please set the JAVA_HOME variable in your environment to match the 130 | location of your Java installation." 131 | fi 132 | else 133 | JAVACMD=java 134 | if ! command -v java >/dev/null 2>&1 135 | then 136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | fi 142 | 143 | # Increase the maximum file descriptors if we can. 144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 145 | case $MAX_FD in #( 146 | max*) 147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 148 | # shellcheck disable=SC2039,SC3045 149 | MAX_FD=$( ulimit -H -n ) || 150 | warn "Could not query maximum file descriptor limit" 151 | esac 152 | case $MAX_FD in #( 153 | '' | soft) :;; #( 154 | *) 155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 156 | # shellcheck disable=SC2039,SC3045 157 | ulimit -n "$MAX_FD" || 158 | warn "Could not set maximum file descriptor limit to $MAX_FD" 159 | esac 160 | fi 161 | 162 | # Collect all arguments for the java command, stacking in reverse order: 163 | # * args from the command line 164 | # * the main class name 165 | # * -classpath 166 | # * -D...appname settings 167 | # * --module-path (only if needed) 168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 169 | 170 | # For Cygwin or MSYS, switch paths to Windows format before running java 171 | if "$cygwin" || "$msys" ; then 172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -classpath "$CLASSPATH" \ 214 | org.gradle.wrapper.GradleWrapperMain \ 215 | "$@" 216 | 217 | # Stop when "xargs" is not available. 218 | if ! command -v xargs >/dev/null 2>&1 219 | then 220 | die "xargs is not available" 221 | fi 222 | 223 | # Use "xargs" to parse quoted args. 224 | # 225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 226 | # 227 | # In Bash we could simply go: 228 | # 229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 230 | # set -- "${ARGS[@]}" "$@" 231 | # 232 | # but POSIX shell has neither arrays nor command substitution, so instead we 233 | # post-process each arg (as a line of input to sed) to backslash-escape any 234 | # character that might be a shell metacharacter, then use eval to reverse 235 | # that process (while maintaining the separation between arguments), and wrap 236 | # the whole thing up as a single "set" statement. 237 | # 238 | # This will of course break if any of these variables contains a newline or 239 | # an unmatched quote. 240 | # 241 | 242 | eval "set -- $( 243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 244 | xargs -n1 | 245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 246 | tr '\n' ' ' 247 | )" '"$@"' 248 | 249 | exec "$JAVACMD" "$@" 250 | --------------------------------------------------------------------------------