Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update happens successfully but no changes are made

I have a spring boot program and right now my PutMapping function seems not be working as expected. Right now if I update the field in postman it says no errors but when I checked to see if the update occurred then there are no changes.

Controller:

    @PutMapping(path = "{bookingId}")
    public void updateBooking (
            @PathVariable("bookingId") Long bookingId,
            @RequestParam(required = false) Integer tickets ) {
        bookingService.updateBooking(bookingId, tickets);

    }

Service:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    public void updateBooking(Long bookingId, Integer tickets) {
        // checks if event id exists
        Booking booking = bookingRepository.findById(bookingId).orElseThrow(() -> new IllegalStateException(
                "Booking with id " + bookingId + " does not exists. "));

        if (tickets != null  && !Objects.equals(booking.getTickets(), tickets)) {
            booking.setTickets(tickets);
            booking.setAmount(booking.getAmount());
        }
        else {
            throw new IllegalStateException(
                    "The update was unsuccessful" );
        }
    }

Model:

    public Integer getTickets() {
        return tickets;
    }

    public void setTickets(Integer tickets) {
        this.tickets = tickets;
    }

>Solution :

You are not saving the updated object in the database.

    public void updateBooking(Long bookingId, Integer tickets) {
    // checks if event id exists
    Booking booking = bookingRepository.findById(bookingId).orElseThrow(() -> new IllegalStateException(
            "Booking with id " + bookingId + " does not exists. "));

    if (tickets != null  && !Objects.equals(booking.getTickets(), tickets)) {
        booking.setTickets(tickets);
        booking.setAmount(booking.getAmount());
        // save the updated booking in the database
        bookingRepository.save(booking)
    }
    else {
        throw new IllegalStateException(
                "The update was unsuccessful" );
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading