0

I want to create a Post method to update a single property from my API main Object:

@Builder
@Document(collection = "tableItems")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class TableItem {

    @Id
    private String id;
    private int roundNumber;
    private RoundState roundState;
    private int bigBlind;
    private int smallBlind;
    private int maxSize;
    private int freeSeats;
    private double tableChips;
    private List<Card> tableCards;
    private List<Player> players;
    private List<Seat> seats;

}

So, this object ist save on my MongoDB. I want to add a new Player on players, but all other properties should not change!

In my GameController, I have a @RequestMapping("/api/tableitems") and my method looks like this:

@PostMapping("/{id}/players")
public void postNewPlayer(@RequestBody Player newPlayer, String tableId) {
    gameService.addNewPlayer(id, newPlayer);
}

In my GameService my addNewPlayer method looks like this:

public void addNewPlayer(Player newPlayer, String tableId) {

    TableItem tableToBeUpdated = tableItemsRepo.findById(tableId)
            .orElseThrow( () -> new NoSuchElementException(("Table with ID " + tableId + " not found!")));

    tableItemsRepo.save(newPlayer);
}

IntelliJ grumbles and throws the following error message:

Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'backend.model.TableItem'

And finally my repository looks like this:

@Repository
public interface TableItemsRepository extends MongoRepository<TableItem, String> {
}

Is it at all possible to update a single property of an object with @POST? How does it work? If not, how else can it be done? I am at a loss and would be very grateful for some help... Thanks a lot!

2
  • The HTTP mapping used on your controller has nothing to do with updating your entity. If you can somehow get it to your service with a GET request to your controller the service layer wouldn't care. Commented Jun 9, 2022 at 0:06
  • Hi, Thank you! I also noticed that my repo was expecting TableItem and I sent Player to it... Commented Jun 9, 2022 at 18:47

2 Answers 2

0

you can have 2 solutions:

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the quick feedback! I have seen some thinking errors already... my Repository expects TableItems and I just give it the property I want to change(Player in Players). I also forgot by the @Post the PathVariable with the TableId.

Through your tips I have now found a solution that also works! Here my code:

Controller:

 @PostMapping("/{id}/players")
public void postNewPlayer(@RequestBody Player newPlayer, @PathVariable String id) {
    gameService.addNewPlayer(newPlayer, id);
}

Service:

public void addNewPlayer(Player newPlayer, String tableId) {

    TableItem tableToBeUpdated = tableItemsRepo.findById(tableId)
            .orElseThrow( () -> new NoSuchElementException(("Table with ID " + tableId + " not found!")));
    List<Player> players =  tableToBeUpdated.getPlayers();
    if( players == null) {
        players = new ArrayList<>();
        tableToBeUpdated.setPlayers(players);
    }
    players.add(newPlayer);
    tableItemsRepo.save(tableToBeUpdated);
}

I did not have to change anything in the repo. The result it looks like this in the DB:

{
   "roundNumber": 2,
   "bigBlind": 20,
   "smallBlind": 10,
   "maxSize": 2,
   "freeSeats": 2,
   "tableChips": 0,
   "tableCards": [
   {
      "rank": "FIVE",
      "suit": "HEARTS"
  },
  {
     "rank": "SEVEN",
     "suit": "SPADES"
  }
  ],
  "players": [
  {
     "playerName": "brasuca",
     "playerChips": 1000,
     "playerCards": [
     {
        "rank": "SEVEN",
        "suit": "HEARTS"
    },
    {
       "rank": "KING",
       "suit": "HEARTS"
    }
  ]
}
]}

Thank you very much for your help!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.