-
Notifications
You must be signed in to change notification settings - Fork 10
Exercise Finished! #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marijanoviclj11
wants to merge
1
commit into
ih-java-08-25:main
Choose a base branch
from
marijanoviclj11:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-parent</artifactId> | ||
| <version>3.5.6</version> | ||
| <relativePath/> <!-- lookup parent from repository --> | ||
| </parent> | ||
| <groupId>com.ironhack</groupId> | ||
| <artifactId>Exercise-spring-jpa</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <name>Exercise-spring-jpa</name> | ||
| <description>Exercise-spring-jpa</description> | ||
| <url/> | ||
| <licenses> | ||
| <license/> | ||
| </licenses> | ||
| <developers> | ||
| <developer/> | ||
| </developers> | ||
| <scm> | ||
| <connection/> | ||
| <developerConnection/> | ||
| <tag/> | ||
| <url/> | ||
| </scm> | ||
| <properties> | ||
| <java.version>17</java.version> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-data-jpa</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-web</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.mysql</groupId> | ||
| <artifactId>mysql-connector-j</artifactId> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/ironhack/exercisespringjpa/controller/FootballerController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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<Footballer> 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}") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here a 204 |
||
| public void delete(@PathVariable UUID id) { | ||
| footballerService.delete(id); | ||
| } | ||
| @GetMapping("/findByAge") | ||
| public List<Footballer> findByAge(){ | ||
| return footballerService.findFootballersByAgeASC(); | ||
| } | ||
| @GetMapping("/findByName") | ||
| public List<Footballer> findByName(@RequestParam String name){ | ||
| return footballerService.findFootballerByName(name); | ||
| } | ||
| } | ||
|
|
||
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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!"); | ||
| } | ||
|
|
||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/ironhack/exercisespringjpa/enums/Position.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.ironhack.exercisespringjpa.enums; | ||
|
|
||
| public enum Position { | ||
| GOALKEEPER, DEFENDER , MIDFIELDER , ATTACKER | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remember that good naming practice for sql tables is with snake case |
||
| 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 + | ||
| '}'; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/main/java/com/ironhack/exercisespringjpa/repository/FootballerRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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<Footballer, UUID> { | ||
| List<Footballer> findFootballersByNationality(String nationality); | ||
| List<Footballer> findFootballersByPosition(String position); | ||
| public Footballer findFootballerByAge(int age); | ||
|
|
||
| @Query("SELECT f from Footballer f ORDER BY f.age ASC") | ||
| List<Footballer> findFootballersByAgeASC(); | ||
| @Query(value = "SELECT * FROM footballer WHERE footballer_name = :footballerName", nativeQuery = true) | ||
| List<Footballer> findFootballerByName(@Param("footballerName") String footballerName); | ||
|
|
||
| String name(String name); | ||
| } | ||
|
|
42 changes: 42 additions & 0 deletions
42
src/main/java/com/ironhack/exercisespringjpa/service/FootballerService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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<Footballer> 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<Footballer> findByNationality(String nationality){ | ||
| return footballerRepository.findFootballersByNationality(nationality); | ||
| } | ||
| public List<Footballer> findByPosition(String position){ | ||
| return footballerRepository.findFootballersByPosition(position); | ||
| } | ||
| public List<Footballer> findFootballersByAgeASC(){ | ||
| return footballerRepository.findFootballersByAgeASC(); | ||
| } | ||
| public List<Footballer> findFootballerByName(String name){ | ||
| return footballerRepository.findFootballerByName(name); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 | ||
|
|
13 changes: 13 additions & 0 deletions
13
src/test/java/com/ironhack/exercisespringjpa/ExerciseSpringJpaApplicationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.ironhack.exercisespringjpa; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
|
|
||
| @SpringBootTest | ||
| class ExerciseSpringJpaApplicationTests { | ||
|
|
||
| @Test | ||
| void contextLoads() { | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you should return a 201 status code :D