I use Spring Boot rest api with MongoDB. In the POST Method, if there is not scoreID and there is not a player with specific date in my collection, because at the same time a player cannot play different games and bring score, then if the specific player and gamecode exist, create a score. In fact, in the POST Method I used Nested IF-ELSE conditions. But, in the Postman when I execute POST Request with this data: { "scoreid":"s11", "score":1000, "player":"sahari", "gamecode":"g12", "date":"2020-01-01" }
always, I recieve an error, in the Postman, 400 Bad Request!, which i defined in the last line of my IF-ELSE statements. I do not know, what is my mistake and why my program doese not execute IF conditions correct.
The POST Method:
//Create Score
@PostMapping
public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) @Valid Score score) {
String p = srepo.findByPlayerName(score.getPlayer());
String g = srepo.findByGameCode(score.getGamecode());
String scoreid = srepo.findByScoreid(score.getScoreid());
Query query = new Query();
query.addCriteria(new Criteria().andOperator(Criteria.where("player").is(score.getPlayer()),
Criteria.where("date").is(score.getDate())));
if((scoreid != null)) {
return ResponseEntity.status(409).body("Conflict!"); }
else
if(mongoTemplate.exists(query, Score.class))
return ResponseEntity.status(409).body("There is not Possible at same time one player brings different Scores!");
else
if((p!= null)&&(g!= null))
{
history = new ArrayList<History>();
h = new History();
h.setScore(score.getScore());
h.setDate(score.getDate());
history.add(h);
hrepo.save(h);
score.setHistory(history);
srepo.insert(score);
return ResponseEntity.ok(score);
}
else
{
return ResponseEntity.status(400).body("Bad Request!");
}
}
The Score Repository:
@Repository
public interface ScoreRepository extends MongoRepository<Score, String>{
@Query("{'scoreid':?0}")
public String findByScoreid(String scoreid);
@Query("{'Player.nickname':?0}")
public String findByPlayerName(String player);
@Query("{'Games.code':?0}")
public String findByGameCode(String game);
}
Aucun commentaire:
Enregistrer un commentaire