Data is retrieved from API
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package be.naaturel.unluckiest.controllers;
|
||||
|
||||
import be.naaturel.unluckiest.entities.ScoreEntity;
|
||||
import be.naaturel.unluckiest.models.Score;
|
||||
import be.naaturel.unluckiest.repositories.ScoreRepo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -10,12 +13,32 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
public class ScoreController {
|
||||
|
||||
private final ScoreRepo scoreRepo;
|
||||
|
||||
@Autowired
|
||||
public ScoreController(){
|
||||
public ScoreController(ScoreRepo scoreRepo){
|
||||
this.scoreRepo = scoreRepo;
|
||||
}
|
||||
|
||||
@PostMapping("/api/submit")
|
||||
public ResponseEntity<?> register(@RequestBody Score s){
|
||||
return null;
|
||||
public ResponseEntity<?> submit(@RequestBody Score s){
|
||||
|
||||
ScoreEntity se = new ScoreEntity();
|
||||
se.owner = s.getOwner();
|
||||
se.value = s.getValue();
|
||||
|
||||
try{
|
||||
scoreRepo.save(se);
|
||||
} catch (Exception e){
|
||||
return ResponseEntity.internalServerError().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping("/api/leaderboard")
|
||||
public ResponseEntity<?> leaderboard(){
|
||||
return ResponseEntity.ok(scoreRepo.findLeaderboard());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,4 +10,12 @@ public class Score {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getOwner(){
|
||||
return this.owner;
|
||||
}
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package be.naaturel.unluckiest.repositories;
|
||||
|
||||
import be.naaturel.unluckiest.entities.ScoreEntity;
|
||||
import be.naaturel.unluckiest.models.Score;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ScoreRepo
|
||||
extends JpaRepository<ScoreEntity, String> {
|
||||
|
||||
@Query(
|
||||
value = "SELECT * FROM Score s HAVING 10;",
|
||||
nativeQuery = true)
|
||||
|
||||
List<ScoreEntity> findLeaderboard();
|
||||
}
|
||||
@@ -7,9 +7,9 @@ sec.cors.authorizedMethods=GET,POST,PUT,DELETE,OPTION
|
||||
sec.cors.authorizedHeader=Authorization,Content-type
|
||||
|
||||
#=============DATABASE=============
|
||||
spring.datasource.url=jdbc:${DB_URL}
|
||||
spring.datasource.username=${DB_USER}
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/unluckiest_db
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
Reference in New Issue
Block a user