Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions pom.xml
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>
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);
}

}
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")
Copy link
Contributor

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

public Footballer create(@RequestBody Footballer footballer){
return footballerService.create(footballer);
}
@PutMapping("/update")
public Footballer update (@RequestBody Footballer footballer){
return footballerService.update(footballer);
}
@DeleteMapping("/{id}")
Copy link
Contributor

Choose a reason for hiding this comment

The 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 src/main/java/com/ironhack/exercisespringjpa/demo/DataLoader.java
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!");
}

}
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 src/main/java/com/ironhack/exercisespringjpa/model/Footballer.java
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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 +
'}';
}
}
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);
}

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);
}
}
10 changes: 10 additions & 0 deletions src/main/resources/application.properties
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

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() {
}

}