Skip to content

Instantly share code, notes, and snippets.

@cmh114933
Created July 7, 2019 10:18
Show Gist options
  • Save cmh114933/1cc93bd09325ea4b5e404e829b178000 to your computer and use it in GitHub Desktop.
Save cmh114933/1cc93bd09325ea4b5e404e829b178000 to your computer and use it in GitHub Desktop.
Create Booking For Annonymous User

Background

To elaborate on the context: Booking without signing up is a feature to reduce the barrier to entry for users to use the application. Depending on how stringent the business requirements are, your business may be more interested in getting customers first, hence not requiring users to sign up.

Later on though, users may want to be able to track previous bookings that they've made through their account. Hence, you need to be able to allow users to link their previous bookings to their account. This may be done as a separate function from the creation of users, but in the current scenario, we'll assume that the user wants to link the bookings on user creation.

The user would have received previous booking ids for their annonymous bookings, which they can attach to their sign up form. We would parse the bookingId into our request and thus handle the linking of annoymous bookings. For the purpose of this exercise, we'll keep to one booking id only.

Booking Creation

There are a few reasons why we weren't able to create the booking as simply as the others previously:

  1. The Booking class did not correctly map the database. In this case, my Booking class did not have a remarks column, which my database had as a non-nullable field. You'll need to make sure that your Booking class is not missing any database columns.
  2. Because we are specifically trying to add a null value to a foreign key user_id on Booking, we need some special handling. Mainly, on the Booking's user_id column, your type declaration cannot be long. It must be Long. If you use long, hibernate will automatically use a default value for your Booking class object, which is 0, as the long type is non-nullable, whereas Long is nullable. If you used long, you would be trying to save a booking with user_id = 0, which is not valid.
// BookingController.java
    @PostMapping(value = "/bookings")
    public void addBooking(@RequestBody Booking booking){
        repository.save(booking);
    }
//Booking.java
// ... other properties

    @Column(name = "user_id")
    private Long userId;
    
// ... other code

Additional tips

  • Saving a booking as user_id null is one way to handle annonymous users, but as you can imagine, it would be fairly difficult to identify whose booking this belongs to come the time you wish to link previous bookings. One way to get around this would be to instead generate a placeholder user and save the bookings under this placeholder. The user will not need to sign up, but we can track the bookings made by this person using some form of unique identification.

User Creation

There are many ways to handle the transfer of booking id data to your application, you could pass it in as PathVariable, or a RequestParam. We'll go with RequestParam though, because adding bookingId to PathVariable is not very meaningful.

// UserController.java
    @PostMapping(value="/users")
    public void addUser(@RequestBody User user, @RequestParam long bookingId){
        User savedUser = userRepository.save(user);
        Booking booking = bookingRepository.findById(bookingId).orElse(new Booking());
        if(booking.getId() != null && savedUser.getId() != null){
            booking.setUserId(savedUser.getId());
            bookingRepository.save(booking);
        }
    }

In the above code, we need to first create the user, and find the booking based on the bookingId provided. If either user cannot be created OR booking cannot be found, we will not execute the UPDATE function to link the booking to user.

Once you manage to create the booking, the rest is about updating the booking with the newly created users id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment