Я работаю с пружинной загрузкой в IntelliJ
Я расширил интерфейс JpaRepository и создал запрос, который не отображается в переменной репозитория, в мой контроллер (ранее я это делал, но кое-что сейчас не работает, не знаю почему)
репозиторий
package com.ITAcademy.dices.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ITAcademy.dices.entities.Player;
public Player findOneByOrderByrateSuccessAsc();
public Player findOneByOrderByrateSuccessDesc();
public List<Player> findAllByOrderAsc();
}
контроллер:
package com.ITAcademy.dices.controllers;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import com.ITAcademy.dices.exceptions.PlayerNotFoundException;
import com.ITAcademy.dices.repositories.PlayerRepository;
import lombok.Data;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ITAcademy.dices.entities.Player;
import javax.persistence.Entity;
@Data
@Entity
@RestController
@RequestMapping("/dices")
public class PlayerController{
private final JpaRepository repository;
public PlayerController(PlayerRepository repository) {
this.repository = repository;
Player player = new Player();
}
//Get all players
@GetMapping("/players")
public List <Player> getAllPlayers() {
return repository.findAll();
}
//Create a new player
@PostMapping("/player")
public Object newPlayer(@RequestBody Player newPlayer) {
newPlayer.setDateRegister(Date.valueOf(LocalDate.now()));
newPlayer.setRateSuccess(-1);
if (newPlayer.getName()=="") newPlayer.setName("Anonymous");
return repository.save(newPlayer);
}
//Get a player by id
@GetMapping("/player/{idPlayer}")
public Player getPlayer(@PathVariable Integer idPlayer) throws Throwable {
return (Player) repository.findById(idPlayer)
.orElseThrow(() -> new PlayerNotFoundException(idPlayer);
}
//Delete a player
@DeleteMapping("/player/{idPlayer}")
public void deletePlayer(@PathVariable Integer idPlayer) {
try {
repository.deleteById(idPlayer);
} catch (EmptyResultDataAccessException e) {
throw new PlayerNotFoundException(idPlayer);
}
}
//Modify a player
@PutMapping("/player/{idPlayer}")
public Player modifyPlayer(@PathVariable Integer idPlayer, @PathVariable String playerName) throws Throwable {
Player player = new Player();
player = (Player) repository.findById(idPlayer)
.orElseThrow(() -> new PlayerNotFoundException(idPlayer));
player.setName(playerName);
return player;
}
//Get success average from all players
@GetMapping("/players/ranking")
public double getSuccessAverage(){
Double success = new Double;
List<Player> players = new ArrayList<Player>();
players = repository.findAll();
for(int x = 0; x < players.size(); x++){
success = success + players.get(x).getRateSuccess();
}
return success/players.size();
}
//Get player with highest success rate
@GetMapping("/players/ranking/winner")
public Player getWinner() {
return this.repository.findOneByOrderByrateSuccessAsc();
}
//Get player with lowest success rate
@GetMapping("/players/ranking/loser")
public Player getLoser() {
return this.repository.findOneByOrderByrateSuccessDesc();
}
}
методы репозитория написаны мной, потому что они не отображаются после точки репозитория.
Любое предложение? спасибо