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 CreateOrders(string fld_email)
        {
            try
            {
                //Create the orders

                List <int> orderIds = ParseOrdersToList();
                Order      order    = new Order();

                order.fld_email = fld_email;
                if (fld_email == null)
                {
                    //The OrderMail Session Key is null. This means the user is already logged in, and did not need to save that key
                    order.fld_email = HttpContext.Session.GetString("User");
                }

                //Insert the Orders in the database
                foreach (int orderId in orderIds)
                {
                    order.fld_OfferedServiceId = orderId;
                    MollShopContext.CreateRow(order, "tbl_orders");

                    //If the user is logged in, we need to remove the orders from their database shopping cart
                    if (fld_email == null)
                    {
                        DatabaseController.RemoveFromShoppingCart(orderId, (int)HttpContext.Session.GetInt32("UserId"));
                    }
                }

                //With this, we have created the orders, and the user is now in the process of paying for their products. We can delete the orders.

                //First, we move the orders in the Session to a new Key. This way, we can still summarize the Orders if the order was successful later, but at the same time
                //We prevent the orders from being made multiple times over by user accident

                //Put the Ordered items in "PUR", for "Purchased
                this.HttpContext.Session.SetString("PUR", this.HttpContext.Session.GetString("ORD"));

                //Remove the orders from the Session
                this.HttpContext.Session.Remove("ORD");

                //Remove the ShoppingCart items from the Cookies
                List <string> items = HttpContext.Request.Cookies.Keys.Where(s => s.StartsWith("SC")).ToList();

                foreach (string item in items)
                {
                    Response.Cookies.Delete(item);
                }
            }

            catch (Exception e)
            {
            }

            return(View("Success"));
        }
        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 static string AddtoShoppingCart(int fld_offeredserviceid, int fld_userid)
        {
            //Check eerst of de service al in de shoppingcart zit
            //zet het voor nu ff op false
            bool foundService = MollShopContext.CheckShoppingCartItem(fld_offeredserviceid, fld_userid);

            if (!foundService)
            {
                //Service zit nog niet in de shoppingcart.
                ShoppingCartItem item = new ShoppingCartItem();
                item.fld_offeredServiceId = fld_offeredserviceid;
                item.fld_UserId           = fld_userid;
                try
                {
                    MollShopContext.CreateRow(item, "tbl_shoppingcart");
                }

                catch (Exception e)
                {
                }
            }

            return("hey");
        }
        public async Task <IActionResult> Success()
        {
            //This action returns the result of the payment.
            //This is when the order will receive it's first update: it's either payed or encountered an error.
            var result = PDTHolder.Success(Request.Query["tx"].ToString());

            //Update the order status and history, update the offeredservice


            //Get previously entered order information
            string form      = HttpContext.Session.GetString("FORM");
            char   separator = ';';

            string[] formVars = form.Split(separator);

            //Send a confirmation email
            await ConstructOrderVerificationMailAsync(formVars);


            string     email             = formVars[4];
            List <int> offeredServiceIds = ParsePursToList();

            foreach (int olsId in offeredServiceIds)
            {
                //Fetch the order id
                int orderId = MollShopContext.FindOrderId(olsId, email);


                //Insert a new order history
                tbl_orderhistory history = new tbl_orderhistory();
                history.fld_ActionDate  = DateTime.Now;
                history.fld_lastAction  = "Paid order";
                history.fld_orderstatus = "Sent";
                history.fld_orderid     = orderId;

                MollShopContext.CreateRow(history, "tbl_orderhistory");

                //Insert a new order status
                tbl_orderstatus orderStatus = new tbl_orderstatus();
                orderStatus.fld_dateOrdered        = DateTime.Now;
                orderStatus.fld_orderid            = orderId;
                orderStatus.fld_targetDeliveryDate = DateTime.Now.AddDays(7);
                orderStatus.fld_DateUpdated        = DateTime.Now;
                MollShopContext.CreateRow(orderStatus);

                //Set the availability of the service to 'N'

                //ElasticSearch
                EsUpdater <OfferedLabourerService> .UpdateField("" + olsId, "fld_stillavailable", 'N');

                //Database
                tbl_offeredservicesdata os = new tbl_offeredservicesdata();
                os.fld_stillavailable = 'N';

                MollShopContext.UpdateRow(os, "fld_OfferedServiceId", olsId);
            }



            return(View("Success"));
        }