teams and matches

This commit is contained in:
= 2020-05-04 15:18:40 +02:00
parent b1867425cf
commit c84273b5c3
11 changed files with 115 additions and 31 deletions

View File

@ -24,13 +24,6 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -51,6 +44,11 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@ -1,8 +1,13 @@
package com.resultprediction.polishekstraklasa.Predictions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -11,7 +16,6 @@ import java.util.Collections;
import java.util.Map;
@SpringBootApplication
@RestController
public class PredictionsApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,50 @@
package com.resultprediction.polishekstraklasa.Predictions.controllers;
import com.resultprediction.polishekstraklasa.Predictions.model.match.Match;
import com.resultprediction.polishekstraklasa.Predictions.model.match.MatchRepository;
import com.resultprediction.polishekstraklasa.Predictions.model.team.Team;
import com.resultprediction.polishekstraklasa.Predictions.model.team.TeamRepository;
import com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics.TeamsMatchStatisticsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Optional;
@Controller
@RequestMapping("/matches")
public class MatchController {
@Autowired
private MatchRepository matchRepository;
@Autowired
private TeamRepository teamRepository;
@Autowired
private TeamsMatchStatisticsRepository teamsMatchStatisticsRepository;
@PostMapping("")
public String create(@RequestBody List<Match> matches) {
for (Match match : matches){
Optional<Team> team1 = teamRepository.findByName(match.getTeamsMatchStatistics1().getTeam().getName());
if (team1.isPresent())
match.getTeamsMatchStatistics1().setTeam(team1.get());
else match.getTeamsMatchStatistics1().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics1().getTeam().getName())));
Optional<Team> team2 = teamRepository.findByName(match.getTeamsMatchStatistics2().getTeam().getName());
if (team2.isPresent())
match.getTeamsMatchStatistics2().setTeam(team2.get());
else match.getTeamsMatchStatistics2().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics2().getTeam().getName())));
match.setTeamsMatchStatistics1(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics1()));
match.setTeamsMatchStatistics2(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics2()));
}
matchRepository.saveAll(matches);
return "done";
}
}

View File

@ -0,0 +1,30 @@
package com.resultprediction.polishekstraklasa.Predictions.controllers;
import com.resultprediction.polishekstraklasa.Predictions.model.team.Team;
import com.resultprediction.polishekstraklasa.Predictions.model.team.TeamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
@RequestMapping("/teams")
public class TeamController {
@Autowired
private TeamRepository teamRepository;
@PostMapping("")
public String create(@RequestBody List<Team> teams) {
for (Team team : teams){
if (teamRepository.findByName(team.getName()).isPresent())
return "some teams are in database already";
}
return "done";
}
}

View File

@ -1,6 +1,6 @@
package com.resultprediction.polishekstraklasa.Predictions.model.match;
import com.resultprediction.polishekstraklasa.Predictions.model.teamStatistics.TeamStatistics;
import com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics.TeamsMatchStatistics;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@ -10,26 +10,28 @@ import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.sql.Date;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Matches")
public class Match {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Getter
@Setter
@OneToOne
private TeamStatistics teamStatistics1;
private TeamsMatchStatistics teamsMatchStatistics1;
@Getter
@Setter
@OneToOne
private TeamStatistics teamStatistics2;
private TeamsMatchStatistics teamsMatchStatistics2;
@Getter
@Setter
@ -39,7 +41,7 @@ public class Match {
@Getter
@Setter
@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date date;
private Date matchDate;
@Getter
@Setter

View File

@ -2,5 +2,5 @@ package com.resultprediction.polishekstraklasa.Predictions.model.match;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MatchRepository extends JpaRepository<Long, Match> {
public interface MatchRepository extends JpaRepository<Match, Long> {
}

View File

@ -5,18 +5,16 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Teams")
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private Long id;

View File

@ -2,5 +2,8 @@ package com.resultprediction.polishekstraklasa.Predictions.model.team;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TeamRepository extends JpaRepository<Long, Team> {
import java.util.Optional;
public interface TeamRepository extends JpaRepository<Team, Long> {
Optional<Team> findByName(String name);
}

View File

@ -1,4 +1,4 @@
package com.resultprediction.polishekstraklasa.Predictions.model.teamStatistics;
package com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics;
import com.resultprediction.polishekstraklasa.Predictions.model.team.Team;
import lombok.AllArgsConstructor;
@ -8,15 +8,15 @@ import lombok.Setter;
import javax.persistence.*;
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class TeamStatistics {
@Entity
public class TeamsMatchStatistics {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Getter

View File

@ -1,6 +1,6 @@
package com.resultprediction.polishekstraklasa.Predictions.model.teamStatistics;
package com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TeamStatisticsRepository extends JpaRepository<Long, TeamStatistics> {
public interface TeamsMatchStatisticsRepository extends JpaRepository<TeamsMatchStatistics, Long> {
}

View File

@ -2,4 +2,3 @@ spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/ekstraklasa
spring.datasource.username=ekstra
spring.datasource.password=YnBDC0hqqhKaxt94
security.session=never