title: How can i create new score for existing player and game at the different time in the POST method of Spring Boot rest api
body: <p>I used Spring Boot Rest Api with MongoDB.
  In the POST Method, if the specific "player" with its nickname and "game" with its code exist, then create the score for them and add the new score with its date to the history.
  I want to check if the score with its date exist in the history of the specific player and game, then the POST Method return as a ResponseEntity the statuse "Bad Request!", and for doing this i used @Query in my Score Repository and also I used for score class a relation between player class and game class.
  but it does not work and i do not know what is my mistake.</p>
  
  <p>The Score Class: </p>
  
  <pre><code> package thesisMongoProject;
  
  import java.util.Date;
  import java.util.List;
  
  import javax.validation.constraints.NotBlank;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  import com.fasterxml.jackson.annotation.JsonView;
  
  @Document(collection = "score")
  public class Score {
  
      @NotBlank
      @JsonView(Views.class)
      private String score;
      @NotBlank
      @JsonView(Views.class)
      private String player;
      @NotBlank
      @JsonView(Views.class)
      private String gamecode;
      @JsonView(Views.class)
      private Date date;
      private List&lt;History&gt; history;
  
      public String getScore() {
          return score;
      }
      public void setScore(String score) {
          this.score = score;
      }
      public String getPlayer() {
          return player;
      }
      public void setPlayer(String player) {
          this.player = player;
      }
      public String getGamecode() {
          return gamecode;
      }
      public void setGamecode(String gamecode) {
          this.gamecode = gamecode;
      }
      public Date getDate() {
          return date;
      }
      public void setDate(Date date) {
          this.date = date;
      }
      public List&lt;History&gt; getHistory() {
          return history;
      }
      public void setHistory(List&lt;History&gt; history) {
          this.history = history;
      }
      public Score(@NotBlank String score, @NotBlank String player, @NotBlank String gamecode,
              Date date, List&lt;History&gt; history) {
          super();
          this.score = score;
          this.player = player;
          this.gamecode = gamecode;
          this.date = date;
          this.history = history;
      }   
  
  }
  </code></pre>
  
  <p>The Player Class:</p>
  
  <pre><code> package thesisMongoProject;
  
  import javax.validation.constraints.NotBlank;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  @Document(collection = "player")
  public class Player {
      @Id
      @NotBlank
      private String nickname;
      @NotBlank
      private String firstname;
      @NotBlank
      private String lastname;
      @NotBlank
      private String email;
  
      public Player(String nickname, String firstname, String lastname, String email) {
          super();
          this.nickname = nickname;
          this.firstname = firstname;
          this.lastname = lastname;
          this.email = email;
      }
      public String getNickname() {
          return nickname;
      }
      public void setNickname(String nickname) {
          this.nickname = nickname;
      }
      public String getFirstname() {
          return firstname;
      }
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
      public String getLastname() {
          return lastname;
      }
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }
      @Override
      public String toString() {
          return "Player [nickname=" + nickname + ", firstname=" + firstname + ", lastname=" + lastname + ", email="
                  + email + "]";
      }
  
  
  }
  </code></pre>
  
  <p>The History Class:</p>
  
  <pre><code>package thesisMongoProject;
  
  import java.util.Date;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  @Document(collection = "history")
  public class History {  
  
  
          private String score;
          private Date date;
  
  
  
  
  
  
  
          public String getScore() {
              return score;
          }
  
  
  
          public void setScore(String score) {
              this.score = score;
          }
  
  
  
          public Date getDate() {
              return date;
          }
  
  
  
          public void setDate(Date date) {
              this.date = date;
          }
  
  
  
          @Override
          public String toString() {
              return "History [score=" + score + ", date=" + date + "]";
          }
  
  
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>The Games Class:</p>
  
  <pre><code>package thesisMongoProject;
  
  import javax.validation.constraints.NotBlank;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  
  @Document(collection = "game")
  public class Games {
      @Id
      @NotBlank
      private String code;
      @NotBlank
      private String title;
      @NotBlank
      private String software_house;
      @NotBlank
      private String version;
      @NotBlank
      private String release_year;
      public Games(@NotBlank String code, @NotBlank String title, @NotBlank String software_house,
              @NotBlank String version, @NotBlank @NotBlank @NotBlank String release_year) {
          super();
          this.code = code;
          this.title = title;
          this.software_house = software_house;
          this.version = version;
          this.release_year = release_year;
      }
      public String getCode() {
          return code;
      }
      public void setCode(String code) {
          this.code = code;
      }
      public String getTitle() {
          return title;
      }
      public void setTitle(String title) {
          this.title = title;
      }
      public String getSoftware_house() {
          return software_house;
      }
      public void setSoftware_house(String software_house) {
          this.software_house = software_house;
      }
      public String getVersion() {
          return version;
      }
      public void setVersion(String version) {
          this.version = version;
      }
      public @NotBlank String getRelease_year() {
          return release_year;
      }
      public void setRelease_year(@NotBlank String release_year) {
          this.release_year = release_year;
      }
      @Override
      public String toString() {
          return "Games [code=" + code + ", title=" + title + ", software_house=" + software_house + ", version="
                  + version + ", release_year=" + release_year + "]";
      }
  
  
  }
  </code></pre>
  
  <p>The Score Repository:</p>
  
  <pre><code>package thesisMongoProject.Repository;
  
  import java.util.Date;
  import java.util.List;
  
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.data.mongodb.repository.Query;
  import org.springframework.stereotype.Repository;
  
  import thesisMongoProject.History;
  import thesisMongoProject.Score;
  
  @Repository
  public interface ScoreRepository extends MongoRepository&lt;Score, String&gt;{
  
      @Query( "{'History.score':?0, 'History.date':?1}")
      public List&lt;History&gt; findByHistory(String score, Date date);
      public List&lt;Score&gt; findByScore(String score);
  
      @Query("{'Player.nickname':?0}")
      public List&lt;Score&gt; findByPlayerName(String player);
  
      @Query("{'Games.code':?0}")
      public List&lt;Score&gt; findByGameCode(String game);
  }
  </code></pre>
  
  <p>The POST Method:</p>
  
  <pre><code>@RestController
  @RequestMapping("/score")
  public class ScoreController {
      @Autowired
      private ScoreRepository srepo;
      @Autowired
      private HistoryRepository hrepo;
      List&lt;History&gt; history;
      History h;
  
  
  
  
      //Create Score
          @PostMapping
          public ResponseEntity&lt;?&gt; createScore(@RequestBody @JsonView(Views.class) @Valid  Score score) {
  
              List&lt;Score&gt; p = srepo.findByPlayerName(score.getPlayer());
              List&lt;Score&gt; g = srepo.findByGameCode(score.getGamecode());
              List&lt;History&gt; id = srepo.findByHistory(score.getScore(), score.getDate());
              if ((p == null)&amp;&amp;(g == null)&amp;&amp;(id != null))
                  return ResponseEntity.status(400).body("Bad Request!");
              else {
                      history = new ArrayList&lt;History&gt;();
                      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);
  
              }
  
          }
  </code></pre>
  
  <p>I do not have any Error in my console. The problem is in the Postman, every time when i give different score for the same player and different game with the same date, the POST method create the new score and add it to the history.
  But it is not correct, because it is not possible a player have different scores for different games at the same time. </p>