简体   繁体   中英

How can i create new score for existing player and game at the different time in the POST method of Spring Boot rest api

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.

The Score Class:

 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<History> 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<History> getHistory() {
        return history;
    }
    public void setHistory(List<History> history) {
        this.history = history;
    }
    public Score(@NotBlank String score, @NotBlank String player, @NotBlank String gamecode,
            Date date, List<History> history) {
        super();
        this.score = score;
        this.player = player;
        this.gamecode = gamecode;
        this.date = date;
        this.history = history;
    }   

}

The Player Class:

 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 + "]";
    }


}

The History Class:

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 + "]";
        }








}

The Games Class:

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 + "]";
    }


}

The Score Repository:

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<Score, String>{

    @Query( "{'History.score':?0, 'History.date':?1}")
    public List<History> findByHistory(String score, Date date);
    public List<Score> findByScore(String score);

    @Query("{'Player.nickname':?0}")
    public List<Score> findByPlayerName(String player);

    @Query("{'Games.code':?0}")
    public List<Score> findByGameCode(String game);
}

The POST Method:

@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreRepository srepo;
    @Autowired
    private HistoryRepository hrepo;
    List<History> history;
    History h;




    //Create Score
        @PostMapping
        public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) @Valid  Score score) {

            List<Score> p = srepo.findByPlayerName(score.getPlayer());
            List<Score> g = srepo.findByGameCode(score.getGamecode());
            List<History> id = srepo.findByHistory(score.getScore(), score.getDate());
            if ((p == null)&&(g == null)&&(id != null))
                return ResponseEntity.status(400).body("Bad Request!");
            else {
                    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);

            }

        }

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.

For solving this problem i used scoreID for checking Duplicate and for checking at the same time one player cannot play different games and takes different scores, I used this:

Query query = new Query();
        query.addCriteria(new Criteria().andOperator(Criteria.where("player").is(score.getPlayer()),
                Criteria.where("date").is(score.getDate())));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM