Creating your own custom controller

This tutorial will show you how to create you own customized controller that will handle the post event when you create a new booking. We assume that you already got uBooking 1.1+ installed and got the resource displayed correctly on your website.

 

The code

First off you need to create your own SurfaceController:

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using uBooking.Core;
using uBooking.Core.Handlers;
using uBooking.Core.Infrastructure.Constants;
using uBooking.Core.Infrastructure.Helpers;
using uBooking.Core.Models;
using Umbraco.Web.Mvc;

public class MyCustomBookingController : SurfaceController
{
    [HttpPost]
    public ActionResult HandleForm(BookingViewModel model)
    {
        var settings = UBookingContext.Current.Services.SettingService.GetByIdentifierCollection(new List<int>
        {
            SettingConstant.NewBookingSuccessLabel,
            SettingConstant.NewBookingSuccessMessage,
            SettingConstant.NewBookingErrorLabel,
            SettingConstant.NewBookingErrorMessage
        }).ToList();

        if (!ModelState.IsValid)
        {
            TempData["ubStatus"] = "Error";
            TempData["ubLabel"] = UmbracoHelper.GetDictionaryItem(SettingHelper.GetSettingValueWithFallback(settings,
                SettingConstant.NewBookingErrorLabel));
            TempData["ubMessage"] = UmbracoHelper.GetDictionaryItem(SettingHelper.GetSettingValueWithFallback(settings,
                SettingConstant.NewBookingErrorMessage));

            return CurrentUmbracoPage();
        }

        var bookingHandler = new BookingHandler(model);
        var bookingService = UBookingContext.Current.Services.BookingService;

        var booking = bookingHandler.GenerateBooking();
        booking = bookingService.Create(booking);

        bookingHandler.CreateBookingData(booking);
        bookingHandler.SendMails(booking);

        TempData["ubStatus"] = "Success";
        TempData["ubLabel"] =
            UmbracoHelper.GetDictionaryItem(SettingHelper.GetSettingValueWithFallback(settings,
                SettingConstant.NewBookingSuccessLabel));
        TempData["ubMessage"] = UmbracoHelper.GetDictionaryItem(SettingHelper.GetSettingValueWithFallback(settings,
            SettingConstant.NewBookingSuccessMessage));

        return RedirectToCurrentUmbracoPage();
    }
}

Update the View

Navigate to ~/Views/Partials/UBooking/_Render.cshtml and update the code to one below:

@using uBooking.Core.Controllers
@using (Html.BeginUmbracoForm<UBookingController>("HandleForm"))

To
@using Your.Namespace.Controllers
@using (Html.BeginUmbracoForm<MyCustomBookingController>("HandleForm"))

 

Alright.. that is a lot of code, explain it to me!

The BookingViewModel has all information that is needed to render and validate the calendar and the form. The ViewModel is using DataAnnotations to validate that the form is filled out correctly and that the selected date(s) are available according to the settings that you have made in Umbraco.

uBooking have two different alert boxes in the razor view, a success and a alert box. The boxes are activated by the TempData["ubStatus"] variable, if you navigate to ~/Views/Partials/UBooking/_Render.cshtml you see that there is a if statements that checks if the variable exists.

 

Ok, whats up with the booking handler?

var bookingHandler = new BookingHandler(model);
var bookingService = UBookingContext.Current.Services.BookingService;

var booking = bookingHandler.GenerateBooking();
booking = bookingService.Create(booking);

bookingHandler.CreateBookingData(booking);
bookingHandler.SendMails(booking);

 

The BookingHandler does the boring job for you, it takes the data from the viewmodel and generates a booking based on the given data and the settings you have made in Umbraco, e.g. the default payment status, default approval settings, the costs and so on. 

When the booking is generated it is created by the BookingService. When the booking is created the handler then creates the BookingData from the form and sends out the notification emails created in Umbraco.

The BookingHandler is optional, you can do this all by yourself without the handler, all you need to do is to create a new Booking yourself and use the different services that we provide in the UBookingContext class. Please contact us if you need more help with this.