<commit_before>// OBJECT CONVERTERS
function bookingToTable(booking){
    var result = "<tr>";
    result += "<td>DB ID</td>";
    result += "<td>" + booking.bookingNumber + "</td>";
    result += "<td>" + guest.guestNumber + "</td>";
    result += "<td>" + room.roomnumber + "</td>";
    result += "<td><button type=\"button\" onclick=\"apiGetBooking(" + booking.bookingNumber + ")\" class=\"btn btn-info\"><span class=\"glyphicon glyphicon-pencil\"></span></button>";
    result += "<button type=\"button\" onclick=\"confirmDelete(" + booking.bookingNumber + ")\" class=\"btn btn-danger\"><span class=\"glyphicon glyphicon-remove\"></span></button></td>";
    result += "</tr>"
    return result;
}
// GENERAL FUNCTIONS
function createBookingDiv(){
//    $("#booking-div").css('display', 'block');
    $("#modal-title").html("Create Booking");
    $("#bookingnumber").val("");
    $("#guestNumber").val("");
    $("#roomnumber").val("");
    $("#btnsubmit").attr('onclick', 'processFormPost();');
    $("#confirmbutton").css('display', 'none');
}

function hideBookingModal(){
    //$("#booking-div").css('display', 'none');
    // hide modal
    $('#myModal').modal('toggle');
}
function fillUpdateModal(booking){
    $("#btnsubmit").attr('onclick', 'processFormPut(' + booking.bookingnumber + ');');
    $("#bookingnumber").val(booking.bookingnumber);
    $("#guestNumber").val(booking.guest.guestNumber);
    $("#roomnumber").val(booking.room.id);
    $("#modal-title").html("Update Booking");
    $("#confirmbutton").css('display', 'inline-block');
    var elem = '<button type="button" class="btn btn-danger" onclick="apiDeleteBooking(' + booking.bookingnumber + ');">Confirm delete</button>';
    $('#confirmbutton').popover({animation:true, content:elem, html:true, container:myModal});
}

function confirmDelete(id){
    var msg = "Delete booking: " + id + "?"
    var r = confirm(msg);
    if (r) {
        apiDeleteBooking(id);
    }
}
function processFormPost(){
//    console.log("processFormPost");
    var bn = parseInt($("#bookingNumber").val());
    var gn = $("#guestNumber").val();
    var rn = $("#roomnumber").val();

    var guest = {
        "guestNumber" : gn
    }

    var room = {
        "id" : rn
    }

    var booking = {
        "guest" : guest,
        "room" : room
    }
// console.log(JSON.stringify(booking));
//    console.log("apiPostBooking with obj: " + JSON.stringify(room));
    apiPostBooking(booking);
}

function processFormPut(id){
//    console.log("processFormPut: " + id);
    var bn = parseInt($("#bookingNumber").val());
    var gn = $("#guestNumber").val();
    var rn = $("#roomnumber").val();

    var guest = {
        "guestNumber" : gn
    }

    var room = {
        "id" : rn
    }

    var booking = {
        "guest" : guest,
        "room" : room
    }
//    console.log("api put: " + id + "  " + JSON.stringify(booking));
    apiPutBooking(id, booking);
//    console.log("apiPostBooking with obj: " + JSON.stringify(booking));
}
function zeroPad(num, places) {
  var zero = places - num.toString().length + 1;
  return Array(+(zero > 0 && zero)).join("0") + num;
}
$(document).ready(onDocumentReady);
// API FUNCTIONALITY
function onDocumentReady(){
    dataTable = $('#bookingtable').DataTable({
        columns: [
        { "data": "bookingnumber" },
        { "data": "guest.guestNumber" },
        { "data": "room.roomNumber" }
                 ]
    });
    $('#bookingtable tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            $('#bookingtable tr.selected').removeClass('selected');
            $(this).addClass('selected');
//            $('#myModal').modal('toggle');
            var table = $('#bookingtable').DataTable();
            var data = table.row( this ).data();

            console.log("selected row: " + JSON.stringify(data));
            apiGetBooking(data.bookingnumber);
            // get booking and show modal with correct values
        }
    });
    apiLoadDatatables();
}

function apiLoadDatatables(){
    var api = "http://localhost:8080/api/booking";

    $.get(api, function (dataSet) {

        console.log("Adding dataset to table: \n" + JSON.stringify(dataSet));
        $("#bookingtable").DataTable().clear();
        $("#bookingtable").DataTable().rows.add(dataSet);
        $("#bookingtable").DataTable().columns.adjust().draw();
    });
}

function apiGetBooking(id){
    // get query met id;
//    console.log("getSingle: " + id)
    var api = "http://localhost:8080/api/booking/" + id;
    $.get(api, function(data){
        if (data){
//            $("#booking-div").css('display','block');
            $('#myModal').modal('toggle');
//            $("#booking-div-title").html("Update Booking");
//            console.log('data for update: ' + JSON.stringify(data));
//            console.log('data for update: ' + JSON.stringify(data));
            console.log("got data:");
            console.log(data);
            fillUpdateModal(data);
        }
    });
}
function apiLoadBooking() {
	var api = "http://localhost:8080/api/booking";
	$.get(api, function (data) {
		if (data) {
            $("#tableBody").empty();
            for(var i = 0; i < data.length; i++){
                var item = data[i];

                var bookingTableEntry = bookingToTable(item);
                $("#tableBody").append(bookingTableEntry);
		    }
//            console.log("result of api call: " + data);
		    return data;
		}
	});
}
function apiDeleteBooking(id){
    var api = "http://localhost:8080/api/booking/" + id;
    $.ajax({
        url: api,
        type: 'DELETE',
        success: function(response){
//            console.log(response);
            hideBookingModal()
            apiLoadDatatables();
        }
    });<commit_msg>build delete refresh function<commit_after>// OBJECT CONVERTERS
var deleteID = -1;
function bookingToTable(booking){
    var result = "<tr>";
    result += "<td>DB ID</td>";
    result += "<td>" + booking.bookingNumber + "</td>";
    result += "<td>" + guest.guestNumber + "</td>";
    result += "<td>" + room.roomnumber + "</td>";
    result += "<td><button type=\"button\" onclick=\"apiGetBooking(" + booking.bookingNumber + ")\" class=\"btn btn-info\"><span class=\"glyphicon glyphicon-pencil\"></span></button>";
    result += "<button type=\"button\" onclick=\"confirmDelete(" + booking.bookingNumber + ")\" class=\"btn btn-danger\"><span class=\"glyphicon glyphicon-remove\"></span></button></td>";
    result += "</tr>"
    return result;
}
// GENERAL FUNCTIONS
function createBookingDiv(){
//    $("#booking-div").css('display', 'block');
    $("#modal-title").html("Create Booking");
    $("#bookingnumber").val("");
    $("#guestNumber").val("");
    $("#roomnumber").val("");
    $("#btnsubmit").attr('onclick', 'processFormPost();');
    $("#confirmbutton").css('display', 'none');
}

function hideBookingModal(){
    //$("#booking-div").css('display', 'none');
    // hide modal
    $('#myModal').modal('toggle');
}
function fillUpdateModal(booking){
    $("#btnsubmit").attr('onclick', 'processFormPut(' + booking.bookingnumber + ');');
    $("#bookingnumber").val(booking.bookingnumber);
    $("#guestNumber").val(booking.guest.guestNumber);
    $("#roomnumber").val(booking.room.id);
    $("#modal-title").html("Update Booking");
    $("#confirmbutton").css('display', 'inline-block');
    deleteID = booking.bookingnumber;
    var elem = '<button type="button" class="btn btn-danger" onclick="apiDeleteBooking(' + booking.bookingnumber + ');">Confirm delete</button>';
    $('#confirmbutton').popover({animation:true, content:elem, html:true, container:myModal});
}

function confirmDelete(id){
    var msg = "Delete booking: " + id + "?"
    var r = confirm(msg);
    if (r) {
        apiDeleteBooking(id);
    }
}
function processFormPost(){
//    console.log("processFormPost");
    var bn = parseInt($("#bookingNumber").val());
    var gn = $("#guestNumber").val();
    var rn = $("#roomnumber").val();

    var guest = {
        "guestNumber" : gn
    }

    var room = {
        "id" : rn
    }

    var booking = {
        "guest" : guest,
        "room" : room
    }
// console.log(JSON.stringify(booking));
//    console.log("apiPostBooking with obj: " + JSON.stringify(room));
    apiPostBooking(booking);
}

function processFormPut(id){
//    console.log("processFormPut: " + id);
    var bn = parseInt($("#bookingNumber").val());
    var gn = $("#guestNumber").val();
    var rn = $("#roomnumber").val();

    var guest = {
        "guestNumber" : gn
    }

    var room = {
        "id" : rn
    }

    var booking = {
        "guest" : guest,
        "room" : room
    }
//    console.log("api put: " + id + "  " + JSON.stringify(booking));
    apiPutBooking(id, booking);
//    console.log("apiPostBooking with obj: " + JSON.stringify(booking));
}
function zeroPad(num, places) {
  var zero = places - num.toString().length + 1;
  return Array(+(zero > 0 && zero)).join("0") + num;
}
$(document).ready(onDocumentReady);
// API FUNCTIONALITY
function onDocumentReady(){
    dataTable = $('#bookingtable').DataTable({
        columns: [
        { "data": "bookingnumber" },
        { "data": "guest.guestNumber" },
        { "data": "room.roomNumber" }
                 ]
    });
    $('#bookingtable tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            $('#bookingtable tr.selected').removeClass('selected');
            $(this).addClass('selected');
//            $('#myModal').modal('toggle');
            var table = $('#bookingtable').DataTable();
            var data = table.row( this ).data();

            console.log("selected row: " + JSON.stringify(data));
            apiGetBooking(data.bookingnumber);
            // get booking and show modal with correct values
        }
    });
    apiLoadDatatables();
}

function apiLoadDatatables(){
    var api = "http://localhost:8080/api/booking";

    $.get(api, function (dataSet) {

        console.log("Adding dataset to table: \n" + JSON.stringify(dataSet));
        $("#bookingtable").DataTable().clear();
        $("#bookingtable").DataTable().rows.add(dataSet);
        $("#bookingtable").DataTable().columns.adjust().draw();
    });
}

function apiGetBooking(id){
    // get query met id;
//    console.log("getSingle: " + id)
    var api = "http://localhost:8080/api/booking/" + id;
    $.get(api, function(data){
        if (data){
//            $("#booking-div").css('display','block');
            $('#myModal').modal('toggle');
//            $("#booking-div-title").html("Update Booking");
//            console.log('data for update: ' + JSON.stringify(data));
//            console.log('data for update: ' + JSON.stringify(data));
            console.log("got data:");
            console.log(data);
            fillUpdateModal(data);
        }
    });
}
function apiLoadBooking() {
	var api = "http://localhost:8080/api/booking";
	$.get(api, function (data) {
		if (data) {
            $("#tableBody").empty();
            for(var i = 0; i < data.length; i++){
                var item = data[i];

                var bookingTableEntry = bookingToTable(item);
                $("#tableBody").append(bookingTableEntry);
		    }
//            console.log("result of api call: " + data);
		    return data;
		}
	});
}
function apiDeleteBooking(){
if (deleteID > -1){
    var api = "http://localhost:8080/api/booking/" + deleteID;
    $.ajax({
        url: api,
        type: 'DELETE',
        success: function(response){
//            console.log(response);
            hideBookingModal()
            apiLoadDatatables();
        }
      });
    }
{'fraction_non_alphanumeric': 0.20451662014717076, 'fraction_numerical': 0.003552397868561279, 'mean_word_length': 2.731145471757652, 'pattern_counts': {'":': 6, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6874405', 'n_tokens_mistral': 3636, 'n_tokens_neox': 3617, 'n_words': 675}