public OLS ConstructOLS(tbl_servicedata service, tbl_labourerdata labourer, tbl_offeredservicesdata offeredServiceObj)
        {
            OLS ols = new OLS();

            ols.fld_addedtowishlist = 0;
            ols.fld_address         = labourer.fld_address;
            ols.fld_area            = offeredServiceObj.fld_area;
            ols.fld_category        = service.fld_category;
            ols.fld_cost            = Convert.ToInt32(offeredServiceObj.fld_cost * 100);
            ols.fld_dateofbirth     = labourer.fld_dateofbirth;
            ols.fld_description     = service.fld_description;
            ols.fld_email           = labourer.fld_email;
            ols.fld_firstname       = labourer.fld_firstname;
            ols.fld_gender          = labourer.fld_gender;
            ols.fld_imagelink       = service.fld_imagelink;
            ols.fld_labourerid      = labourer.fld_labourerid;
            ols.fld_lastname        = labourer.fld_lastname;
            ols.fld_name            = service.fld_name;
            ols.fld_phonenumber     = labourer.fld_phonenumber;
            ols.fld_serviceid       = service.fld_serviceid;
            ols.fld_stillavailable  = offeredServiceObj.fld_stillavailable;
            ols.fld_timefirst       = offeredServiceObj.fld_timefirst.ToString();
            ols.fld_timelast        = offeredServiceObj.fld_timelast.ToString();
            ols.fld_timesbought     = offeredServiceObj.fld_timesbought;
            ols.fld_zipcode         = labourer.fld_zipcode;

            ols.fld_offeredserviceid = offeredServiceObj.fld_offeredserviceid;

            return(ols);
        }
        public int CreateItem(string insertedDic, string type)
        {
            //return the id back to the JS Datatable!
            switch (type)
            {
            case "tbl_userdata":
                tbl_userdata newUser = JsonConvert.DeserializeObject <tbl_userdata>(insertedDic);

                //Check if the email has been taken already
                int emailIsTaken = MollShopContext.CheckIfUserExists(newUser.fld_email);

                if (emailIsTaken == 0)
                {
                    //Email has not yet been taken

                    //Salt and Hash the password
                    newUser.fld_password = MollShopContext.SaltNHash(newUser.fld_password);

                    newUser.fld_userid = MollShopContext.CreateRow(newUser, type);

                    if (newUser.fld_dateofbirth == "")
                    {
                        newUser.fld_dateofbirth = null;
                    }
                    EsUpdater <tbl_userdata> .InsertDocument(newUser, "moll_users", "User", newUser.fld_userid.ToString());

                    return(newUser.fld_userid);
                }

                else
                {
                    //Email has been taken
                    return(-1);
                }

            case "tbl_servicedata":
                tbl_servicedata newService = JsonConvert.DeserializeObject <tbl_servicedata>(insertedDic);
                newService.fld_serviceid = MollShopContext.CreateRow(newService, type);
                EsUpdater <tbl_servicedata> .InsertDocument(newService, "moll_dataservices", "Services", newService.fld_serviceid.ToString());

                return(newService.fld_serviceid);

            case "tbl_labourerdata":
                tbl_labourerdata newLabourer = JsonConvert.DeserializeObject <tbl_labourerdata>(insertedDic);
                newLabourer.fld_labourerid = MollShopContext.CreateRow(newLabourer, type);
                EsUpdater <tbl_labourerdata> .InsertDocument(newLabourer, "moll_labourers", "Labourer", newLabourer.fld_labourerid.ToString());

                return(newLabourer.fld_labourerid);

            default:
                break;
            }

            return(0);
        }
        public IActionResult OLSPreview(string offeredService, string serviceId, string labourerId)
        {
            tbl_servicedata         service           = EsServiceQuery.FindById(Convert.ToInt32(serviceId));
            tbl_labourerdata        labourer          = EsLabourerQuery.FindById(Convert.ToInt32(labourerId));
            tbl_offeredservicesdata offeredServiceObj = JsonConvert.DeserializeObject <tbl_offeredservicesdata>(offeredService);

            offeredServiceObj.fld_offeredserviceid = 0;

            OLS ols = ConstructOLS(service, labourer, offeredServiceObj);


            return(PartialView("OLSPreview", ols));
        }
        public IActionResult Details(OfferedLabourerService package)
        {
            tbl_labourerdata labourer = new tbl_labourerdata();

            labourer.fld_address     = package.fld_address;
            labourer.fld_dateofbirth = package.fld_dateofbirth.ToString();
            labourer.fld_email       = package.fld_email;
            labourer.fld_firstname   = package.fld_email;
            labourer.fld_gender      = package.fld_gender;
            labourer.fld_labourerid  = package.fld_labourerid;
            labourer.fld_lastname    = package.fld_lastname;
            labourer.fld_phonenumber = package.fld_phonenumber;
            labourer.fld_zipcode     = package.fld_zipcode;

            return(View("Details", labourer));
        }
        public IActionResult ItemPreview(string insertedDic, string type)
        {
            switch (type)
            {
            case "tbl_servicedata":
                tbl_servicedata currentService = JsonConvert.DeserializeObject <tbl_servicedata>(insertedDic);
                return(PartialView("ServicePreview", currentService));

            case "tbl_labourerdata":
                tbl_labourerdata currentLabourer = JsonConvert.DeserializeObject <tbl_labourerdata>(insertedDic);
                return(PartialView("LabourerPreview", currentLabourer));

            default:
                break;
            }

            return(NotFound());
        }
        public OLS CreateOLS(string offeredService, string serviceId, string labourerId)
        {
            tbl_servicedata         service           = EsServiceQuery.FindById(Convert.ToInt32(serviceId));
            tbl_labourerdata        labourer          = EsLabourerQuery.FindById(Convert.ToInt32(labourerId));
            tbl_offeredservicesdata offeredServiceObj = JsonConvert.DeserializeObject <tbl_offeredservicesdata>(offeredService);

            offeredServiceObj.fld_labourerid = labourer.fld_labourerid;
            offeredServiceObj.fld_serviceid  = service.fld_serviceid;

            offeredServiceObj.fld_offeredserviceid = MollShopContext.CreateRow(offeredServiceObj, "tbl_offeredservicesdata");

            OLS ols = ConstructOLS(service, labourer, offeredServiceObj);

            //Because ElasticSearch does not support decimal numbers, we must multiply the cost by a 100
            ols.fld_cost = ols.fld_cost * 100;

            EsUpdater <OLS> .InsertDocument(ols, "moll_ols", "OLS", ols.fld_offeredserviceid.ToString());

            //return the OfferedLabourerService, so we can later render it into the Datatable on the ManageOLS page
            //We must divide the cost by a 100 again, to render it correctly

            ols.fld_cost = ols.fld_cost / 100;
            return(ols);
        }
        public IActionResult Details(string labourerId)
        {
            tbl_labourerdata labourer = MollShopContext.FindLabourerById(labourerId);

            return(View("Details", labourer));
        }
        public int EditItem(string insertedDic, string type)
        {
            switch (type)
            {
            case "tbl_userdata":
                try
                {
                    tbl_userdata currentUser = JsonConvert.DeserializeObject <tbl_userdata>(insertedDic);

                    //Dates and ElasticSearch do not mix very well, so we do a little check beforehand
                    if (currentUser.fld_dateofbirth == "")
                    {
                        currentUser.fld_dateofbirth = null;
                    }

                    EsUpdater <tbl_userdata> .UpsertDocument(currentUser, "moll_users", "User", currentUser.fld_userid);

                    MollShopContext.UpdateRow(currentUser, "fld_UserId", currentUser.fld_userid);
                }
                catch (Exception e)
                {
                    return(-1);
                }

                break;

            case "tbl_servicedata":
                tbl_servicedata currentService = JsonConvert.DeserializeObject <tbl_servicedata>(insertedDic);

                //Update the stand-alone service document
                EsUpdater <tbl_servicedata> .UpsertDocument(currentService, "moll_dataservices", "Services", currentService.fld_serviceid);

                //Find all OLS documents in ES that contain this service
                List <OfferedLabourerService> packages = EsOLSQuery <OfferedLabourerService> .getByService(currentService.fld_serviceid);

                //Foreach OLS ID, update it with the current service
                foreach (OfferedLabourerService package in packages)
                {
                    package.fld_name        = currentService.fld_name;
                    package.fld_category    = currentService.fld_category;
                    package.fld_description = currentService.fld_description;
                    package.fld_imagelink   = currentService.fld_imagelink;

                    EsUpdater <OfferedLabourerService> .UpsertDocument(package, "moll_ols", "OLS", package.fld_offeredserviceid);
                }

                MollShopContext.UpdateRow(currentService, "fld_ServiceId", currentService.fld_serviceid);

                break;

            case "tbl_labourerdata":
                tbl_labourerdata currentLabourer = JsonConvert.DeserializeObject <tbl_labourerdata>(insertedDic);

                //Update the stand-alone labourer document
                EsUpdater <tbl_labourerdata> .UpsertDocument(currentLabourer, "moll_labourers", "Labourer", currentLabourer.fld_labourerid);

                //Find all OLS documents in ES that contain this labourer
                List <OfferedLabourerService> olspackages = EsOLSQuery <OfferedLabourerService> .getByLabourer(currentLabourer.fld_labourerid);

                //Foreach OLS Id, update it with the current labourer
                foreach (OfferedLabourerService package in olspackages)
                {
                    package.fld_address     = currentLabourer.fld_address;
                    package.fld_firstname   = currentLabourer.fld_firstname;
                    package.fld_email       = currentLabourer.fld_email;
                    package.fld_gender      = currentLabourer.fld_gender;
                    package.fld_lastname    = currentLabourer.fld_lastname;
                    package.fld_phonenumber = currentLabourer.fld_phonenumber;
                    package.fld_zipcode     = currentLabourer.fld_zipcode;

                    EsUpdater <OfferedLabourerService> .UpsertDocument(package, "moll_ols", "OLS", package.fld_offeredserviceid);
                }

                MollShopContext.UpdateRow(currentLabourer, "fld_LabourerId", currentLabourer.fld_labourerid);

                break;

            default:
                break;
            }

            return(1);
        }