From ab1a2463a3010603a5f31a64fee9b29aed7c9cab Mon Sep 17 00:00:00 2001 From: Ljubi Date: Sun, 5 Oct 2025 12:56:34 +0200 Subject: [PATCH] Exercise Finished! --- pom.xml | 63 ++++++++++++++ .../ExerciseSpringJpaApplication.java | 13 +++ .../controller/FootballerController.java | 43 ++++++++++ .../exercisespringjpa/demo/DataLoader.java | 36 ++++++++ .../exercisespringjpa/enums/Position.java | 5 ++ .../exercisespringjpa/model/Footballer.java | 86 +++++++++++++++++++ .../repository/FootballerRepository.java | 23 +++++ .../service/FootballerService.java | 42 +++++++++ src/main/resources/application.properties | 10 +++ .../ExerciseSpringJpaApplicationTests.java | 13 +++ 10 files changed, 334 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplication.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/controller/FootballerController.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/enums/Position.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/repository/FootballerRepository.java create mode 100644 src/main/java/com/ironhack/exercisespringjpa/service/FootballerService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplicationTests.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1e13de8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.6 + + + com.ironhack + Exercise-spring-jpa + 0.0.1-SNAPSHOT + Exercise-spring-jpa + Exercise-spring-jpa + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + com.mysql + mysql-connector-j + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplication.java b/src/main/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplication.java new file mode 100644 index 0000000..84cd44d --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplication.java @@ -0,0 +1,13 @@ +package com.ironhack.exercisespringjpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExerciseSpringJpaApplication { + + public static void main(String[] args) { + SpringApplication.run(ExerciseSpringJpaApplication.class, args); + } + +} diff --git a/src/main/java/com/ironhack/exercisespringjpa/controller/FootballerController.java b/src/main/java/com/ironhack/exercisespringjpa/controller/FootballerController.java new file mode 100644 index 0000000..620cc92 --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/controller/FootballerController.java @@ -0,0 +1,43 @@ +package com.ironhack.exercisespringjpa.controller; + +import com.ironhack.exercisespringjpa.model.Footballer; +import com.ironhack.exercisespringjpa.service.FootballerService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@RestController +@RequestMapping("api/footballers") +public class FootballerController { + public final FootballerService footballerService; + + public FootballerController (FootballerService footballerService){ + this.footballerService = footballerService; + } + @GetMapping + public List findAll(){ + return footballerService.getAll(); + } + @PostMapping("/create") + public Footballer create(@RequestBody Footballer footballer){ + return footballerService.create(footballer); + } + @PutMapping("/update") + public Footballer update (@RequestBody Footballer footballer){ + return footballerService.update(footballer); + } + @DeleteMapping("/{id}") + public void delete(@PathVariable UUID id) { + footballerService.delete(id); + } + @GetMapping("/findByAge") + public List findByAge(){ + return footballerService.findFootballersByAgeASC(); + } + @GetMapping("/findByName") + public List findByName(@RequestParam String name){ + return footballerService.findFootballerByName(name); + } + } + diff --git a/src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java b/src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java new file mode 100644 index 0000000..23ced6a --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java @@ -0,0 +1,36 @@ +package com.ironhack.exercisespringjpa.demo; + +import com.ironhack.exercisespringjpa.enums.Position; +import com.ironhack.exercisespringjpa.model.Footballer; +import com.ironhack.exercisespringjpa.service.FootballerService; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class DataLoader implements CommandLineRunner { + public final FootballerService footballerService; + + public DataLoader(FootballerService footballerService) { + this.footballerService = footballerService; + } + + @Override + public void run(String... args) throws Exception { + System.out.println("Loading inital data.."); + if (!footballerService.getAll().isEmpty()) { + System.out.println("Data already loaded, skipping...."); + return; + } + Footballer footballer1 = new Footballer("Luka Modric", 40, "Croatian", + "A.C. Milan", Position.MIDFIELDER); + Footballer footballer2 = new Footballer("Florian Wirtz", 22, "German", + "Liverpool FC", Position.MIDFIELDER); + Footballer footballer3 = new Footballer("Dani Olmo", 27, "Spanish", + "Barcelona", Position.MIDFIELDER); + footballerService.create(footballer1); + footballerService.create(footballer2); + footballerService.create(footballer3); + System.out.println("Data loaded!"); + } + +} diff --git a/src/main/java/com/ironhack/exercisespringjpa/enums/Position.java b/src/main/java/com/ironhack/exercisespringjpa/enums/Position.java new file mode 100644 index 0000000..32e0615 --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/enums/Position.java @@ -0,0 +1,5 @@ +package com.ironhack.exercisespringjpa.enums; + +public enum Position { + GOALKEEPER, DEFENDER , MIDFIELDER , ATTACKER +} diff --git a/src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java b/src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java new file mode 100644 index 0000000..9156204 --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java @@ -0,0 +1,86 @@ +package com.ironhack.exercisespringjpa.model; + +import com.ironhack.exercisespringjpa.enums.Position; +import jakarta.persistence.*; + +import java.util.UUID; + +@Entity +@Table(name = "footballer") +public class Footballer { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID id; + @Column(name = "name", nullable = false,length = 200) + private String name; + private int age; + @Column(name = "nationality", nullable = false) + private String nationality; + @Column(name = "currentTeam", nullable = false) + private String currentTeam; + @Enumerated(EnumType.STRING) + private Position position; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getNationality() { + return nationality; + } + + public void setNationality(String nationality) { + this.nationality = nationality; + } + + public String getCurrentTeam() { + return currentTeam; + } + + public void setCurrentTeam(String currentTeam) { + this.currentTeam = currentTeam; + } + + public Position getPosition() { + return position; + } + + public void setPosition(Position position) { + this.position = position; + } + + public Footballer(String name, int age, String nationality, String currentTeam, Position position) { + this.name = name; + this.age = age; + this.nationality = nationality; + this.currentTeam = currentTeam; + this.position = position; + } + + public Footballer() { + } + + @Override + public String toString() { + return "Footballer{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", nationality='" + nationality + '\'' + + ", currentTeam='" + currentTeam + '\'' + + ", position=" + position + + '}'; + } +} diff --git a/src/main/java/com/ironhack/exercisespringjpa/repository/FootballerRepository.java b/src/main/java/com/ironhack/exercisespringjpa/repository/FootballerRepository.java new file mode 100644 index 0000000..5547099 --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/repository/FootballerRepository.java @@ -0,0 +1,23 @@ +package com.ironhack.exercisespringjpa.repository; + +import com.ironhack.exercisespringjpa.model.Footballer; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; +import java.util.UUID; + +public interface FootballerRepository extends JpaRepository { + List findFootballersByNationality(String nationality); + List findFootballersByPosition(String position); + public Footballer findFootballerByAge(int age); + + @Query("SELECT f from Footballer f ORDER BY f.age ASC") + List findFootballersByAgeASC(); + @Query(value = "SELECT * FROM footballer WHERE footballer_name = :footballerName", nativeQuery = true) + List findFootballerByName(@Param("footballerName") String footballerName); + + String name(String name); +} + diff --git a/src/main/java/com/ironhack/exercisespringjpa/service/FootballerService.java b/src/main/java/com/ironhack/exercisespringjpa/service/FootballerService.java new file mode 100644 index 0000000..a885e1e --- /dev/null +++ b/src/main/java/com/ironhack/exercisespringjpa/service/FootballerService.java @@ -0,0 +1,42 @@ +package com.ironhack.exercisespringjpa.service; + +import com.ironhack.exercisespringjpa.model.Footballer; +import com.ironhack.exercisespringjpa.repository.FootballerRepository; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; +import java.util.UUID; + +@Service +public class FootballerService { + public final FootballerRepository footballerRepository; + + public FootballerService(FootballerRepository footballerRepository){ + this.footballerRepository = footballerRepository; + } + public List getAll(){ + return footballerRepository.findAll(); + } + public Footballer create(Footballer footballer){ + return footballerRepository.save(footballer); + } + public Footballer update (Footballer footballer) { + return footballerRepository.save(footballer); + } + public void delete(UUID id){ + footballerRepository.deleteById(id); + } + public List findByNationality(String nationality){ + return footballerRepository.findFootballersByNationality(nationality); + } + public List findByPosition(String position){ + return footballerRepository.findFootballersByPosition(position); + } + public List findFootballersByAgeASC(){ + return footballerRepository.findFootballersByAgeASC(); + } +public List findFootballerByName(String name){ + return footballerRepository.findFootballerByName(name); +} +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..7fcadd8 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.application.name=Exercise-spring-jpa +# Connection configuration +spring.datasource.url=jdbc:mysql://localhost:3306/jpa_exercise +spring.datasource.username=root +spring.datasource.password=Haxball17! + +# JPA properties +spring.jpa.hibernate.ddl-auto=create +spring.jpa.show-sql=true + diff --git a/src/test/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplicationTests.java b/src/test/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplicationTests.java new file mode 100644 index 0000000..3efba01 --- /dev/null +++ b/src/test/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplicationTests.java @@ -0,0 +1,13 @@ +package com.ironhack.exercisespringjpa; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ExerciseSpringJpaApplicationTests { + + @Test + void contextLoads() { + } + +}