├── .gitignore ├── src └── main │ ├── groovy │ └── com │ │ └── pokemon │ │ ├── service │ │ ├── WildPokemonService.groovy │ │ ├── TrainerService.groovy │ │ └── impl │ │ │ ├── WildPokemonServiceImpl.groovy │ │ │ └── TrainerServiceImpl.groovy │ │ ├── Application.groovy │ │ ├── repository │ │ ├── TrainerRepository.groovy │ │ └── WildPokemonRepository.groovy │ │ ├── entity │ │ ├── Pokemon.groovy │ │ ├── Trainer.groovy │ │ └── WildPokemon.groovy │ │ └── controller │ │ └── TrainerController.groovy │ └── resources │ ├── application.properties │ └── db │ └── migration │ ├── V2__inserting_example_data.sql │ └── V1__creating_initial_tables.sql └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | build/ 3 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/service/WildPokemonService.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.service 2 | 3 | import com.pokemon.entity.WildPokemon 4 | 5 | interface WildPokemonService { 6 | 7 | List findByTrainer(int trainerId) 8 | } 9 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/service/TrainerService.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.service 2 | 3 | import com.pokemon.entity.Trainer 4 | 5 | interface TrainerService { 6 | 7 | List findAll() 8 | 9 | Trainer findById(int id) 10 | } 11 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/Application.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon 2 | 3 | import org.springframework.boot.SpringApplication 4 | import org.springframework.boot.autoconfigure.SpringBootApplication 5 | 6 | @SpringBootApplication 7 | class Application { 8 | 9 | static void main(String[] args) { 10 | SpringApplication.run Application, args 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.flyway.url=jdbc:mysql://localhost/ 2 | spring.flyway.user=your-username 3 | spring.flyway.password=your-password 4 | spring.flyway.schemas=your-database 5 | 6 | spring.datasource.url=jdbc:mysql://localhost/your-database 7 | spring.datasource.username=your-username 8 | spring.datasource.password=your-password 9 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 10 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/repository/TrainerRepository.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.repository 2 | 3 | import com.pokemon.entity.Trainer 4 | import org.springframework.data.jpa.repository.JpaRepository 5 | import org.springframework.stereotype.Repository 6 | 7 | @Repository 8 | interface TrainerRepository extends JpaRepository { 9 | 10 | List findAll() 11 | 12 | Trainer findById(Integer id) 13 | } 14 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/repository/WildPokemonRepository.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.repository 2 | 3 | import com.pokemon.entity.WildPokemon 4 | import org.springframework.data.jpa.repository.JpaRepository 5 | import org.springframework.stereotype.Repository 6 | 7 | @Repository 8 | interface WildPokemonRepository extends JpaRepository { 9 | 10 | List findByTrainerId(Integer trainerId) 11 | } 12 | -------------------------------------------------------------------------------- /src/main/resources/db/migration/V2__inserting_example_data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO trainers VALUES (1, 'Red', 40), (2, 'Ash Ketchum', 10); 2 | 3 | INSERT INTO pokemon VALUES 4 | (1, 'Bulbasaur', 1), (2, 'Ivysaur', 2), (3, 'Venosaur', 3), (4, 'Charmander', 4), 5 | (5, 'Charmeleon', 5), (6, 'Charizard', 6), (7, 'Squirtle', 7), (8, 'Wartortle', 8), 6 | (9, 'Blastoise', 9); 7 | 8 | INSERT INTO wild_pokemon VALUES 9 | (1, 2000, 3, 1), (2, 2100, 6, 1), (7, 2000, 9, 1), (8, 600, 1, 2); 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pokemon API v1.0 2 | 3 | Spring Boot + Groovy example for Dev.to 4 | 5 | ## Requirements 6 | 7 | * Java 8 or greater 8 | * MySQL 9 | * Gradle 10 | 11 | ## Configuration 12 | 13 | Edit the file `application.properties` and change **your-username**, **your-password** and **your-database** values. 14 | 15 | Running the project will create the database, tables and inserting example data automatically. 16 | 17 | ## Running the project 18 | 19 | In terminal type the command `gradle bootRun`. 20 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/entity/Pokemon.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.entity 2 | 3 | import javax.persistence.Entity 4 | import javax.persistence.Id 5 | import javax.persistence.GeneratedValue 6 | import javax.persistence.Table 7 | import javax.persistence.GenerationType 8 | import javax.validation.constraints.NotNull 9 | import javax.persistence.Column 10 | 11 | @Entity 12 | @Table(name = "pokemon") 13 | class Pokemon { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | Integer id 18 | 19 | @NotNull 20 | @Column(nullable = false) 21 | String name 22 | 23 | @NotNull 24 | @Column(nullable = false) 25 | Short number 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/entity/Trainer.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.entity 2 | 3 | import javax.persistence.Entity 4 | import javax.persistence.Id 5 | import javax.persistence.GeneratedValue 6 | import javax.persistence.Table 7 | import javax.persistence.GenerationType 8 | import javax.validation.constraints.NotNull 9 | import javax.persistence.Column 10 | 11 | @Entity 12 | @Table(name = "trainers") 13 | class Trainer { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | Integer id 18 | 19 | @NotNull 20 | @Column(nullable = false) 21 | String name 22 | 23 | @NotNull 24 | @Column(nullable = false) 25 | Short level 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/service/impl/WildPokemonServiceImpl.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.service.impl 2 | 3 | import com.pokemon.entity.WildPokemon 4 | import com.pokemon.service.WildPokemonService 5 | import com.pokemon.repository.WildPokemonRepository 6 | import org.springframework.beans.factory.annotation.Autowired 7 | import org.springframework.stereotype.Service 8 | 9 | @Service 10 | class WildPokemonServiceImpl implements WildPokemonService { 11 | 12 | @Autowired 13 | private final WildPokemonRepository wildPokemonRepository 14 | 15 | @Override 16 | List findByTrainer(int trainerId) { 17 | wildPokemonRepository.findByTrainerId trainerId 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/resources/db/migration/V1__creating_initial_tables.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE trainers( 2 | id INTEGER AUTO_INCREMENT, 3 | name VARCHAR(100) NOT NULL, 4 | level SMALLINT NOT NULL DEFAULT 1, 5 | PRIMARY KEY (id) 6 | ); 7 | 8 | CREATE TABLE pokemon( 9 | id INTEGER AUTO_INCREMENT, 10 | name VARCHAR(20) NOT NULL, 11 | number SMALLINT NOT NULL, 12 | PRIMARY KEY (id) 13 | ); 14 | 15 | CREATE TABLE wild_pokemon( 16 | id INTEGER AUTO_INCREMENT, 17 | combat_power SMALLINT NOT NULL DEFAULT 0, 18 | pokemon_id INTEGER NOT NULL, 19 | trainer_id INTEGER, 20 | PRIMARY KEY (id), 21 | FOREIGN KEY (pokemon_id) REFERENCES pokemon (id), 22 | FOREIGN KEY (trainer_id) REFERENCES trainers (id) 23 | ); 24 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/service/impl/TrainerServiceImpl.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.service.impl 2 | 3 | import com.pokemon.entity.Trainer 4 | import com.pokemon.service.TrainerService 5 | import com.pokemon.repository.TrainerRepository 6 | import org.springframework.beans.factory.annotation.Autowired 7 | import org.springframework.stereotype.Service 8 | 9 | @Service 10 | class TrainerServiceImpl implements TrainerService { 11 | 12 | @Autowired 13 | private final TrainerRepository trainerRepository 14 | 15 | @Override 16 | List findAll() { 17 | trainerRepository.findAll() 18 | } 19 | 20 | @Override 21 | Trainer findById(int id) { 22 | trainerRepository.findById id 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/entity/WildPokemon.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.entity 2 | 3 | import javax.persistence.Entity 4 | import javax.persistence.Id 5 | import javax.persistence.GeneratedValue 6 | import javax.persistence.Table 7 | import javax.persistence.GenerationType 8 | import javax.validation.constraints.NotNull 9 | import javax.persistence.Column 10 | import javax.persistence.ManyToOne 11 | import javax.persistence.JoinColumn 12 | 13 | @Entity 14 | @Table(name = "wild_pokemon") 15 | class WildPokemon { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | Integer id 20 | 21 | @NotNull 22 | @Column(name = "combat_power", nullable = false) 23 | Integer combatPower 24 | 25 | @ManyToOne 26 | @JoinColumn(name = "pokemon_id", referencedColumnName = "id", nullable = false) 27 | Pokemon pokemon 28 | 29 | @ManyToOne 30 | @JoinColumn(name = "trainer_id", referencedColumnName = "id", nullable = true) 31 | Trainer trainer 32 | } 33 | -------------------------------------------------------------------------------- /src/main/groovy/com/pokemon/controller/TrainerController.groovy: -------------------------------------------------------------------------------- 1 | package com.pokemon.controller 2 | 3 | import com.pokemon.entity.Trainer 4 | import com.pokemon.entity.WildPokemon 5 | import com.pokemon.service.TrainerService 6 | import com.pokemon.service.WildPokemonService 7 | import org.springframework.web.bind.annotation.RestController 8 | import org.springframework.web.bind.annotation.RequestMapping 9 | import org.springframework.beans.factory.annotation.Autowired 10 | import org.springframework.web.bind.annotation.RequestMethod 11 | import org.springframework.web.bind.annotation.PathVariable 12 | 13 | @RestController 14 | @RequestMapping('/trainers') 15 | class TrainerController { 16 | 17 | @Autowired 18 | private final TrainerService trainerService 19 | 20 | @Autowired 21 | private final WildPokemonService wildPokemonService 22 | 23 | @RequestMapping(method = RequestMethod.GET) 24 | List findAll() { 25 | trainerService.findAll() 26 | } 27 | 28 | @RequestMapping(value = '/{id}/pokemon', method = RequestMethod.GET) 29 | List findCaughtPokemon(@PathVariable('id') int id) { 30 | wildPokemonService.findByTrainer id 31 | } 32 | 33 | @RequestMapping(value = '/{id}', method = RequestMethod.GET) 34 | Trainer findById(@PathVariable('id') int id) { 35 | trainerService.findById id 36 | } 37 | } 38 | --------------------------------------------------------------------------------