private async void WriteToDatabase(object sender, EventArgs e)
        {
            var point = await MapMethods.ConvertAddressToLocationAsync(editVendor.addressBox.Text);

            var temp = new ObservableCollection <Vendor>();

            foreach (var vendor in VendorList)
            {
                if (vendor.ID != tempVendor.ID)
                {
                    temp.Add(vendor);
                }
                else
                {
                    tempVendor.Title        = editVendor.titleBox.Text;
                    tempVendor.About        = editVendor.aboutBox.Text;
                    tempVendor.Address      = editVendor.addressBox.Text;
                    tempVendor.Latitude     = double.Parse(point[0]);
                    tempVendor.Longitude    = double.Parse(point[1]);
                    tempVendor.Municipality = point[2];
                    tempVendor.County       = point[3];
                    temp.Add(tempVendor);
                }
            }

            using (var db = new AppDbContext())
            {
                var vendor = db.Vendors.SingleOrDefault(x => x.ID == tempVendor.ID);
                vendor.Title        = editVendor.titleBox.Text;
                vendor.About        = editVendor.aboutBox.Text;
                vendor.Address      = editVendor.addressBox.Text;
                vendor.Latitude     = double.Parse(point[0]);
                vendor.Longitude    = double.Parse(point[1]);
                vendor.Municipality = point[2];
                vendor.County       = point[3];
                db.SaveChanges();
            }
            VendorList = temp;
        }
        public async Task <IActionResult> Edit(int id, CreateEditVendorVM model)
        {
            if (id != model.ID)
            {
                return(NotFound());
            }

            var vendor = await _vendorService.GetVendorAsync(id);

            if (!ValidUser(vendor.UserID))
            {
                return(NotFound());
            }

            if (ModelState.GetFieldValidationState("Title") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid &&
                ModelState.GetFieldValidationState("Address") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid)
            {
                if (model.Image != null && !model.Image.ValidImage())
                {
                    ModelState.AddModelError("", _localizer["Invalid image type"]);
                    _toastNotification.AddErrorToastMessage(_localizer["Invalid image type"]);
                    return(View(model));
                }

                var latLng = await MapMethods.ConvertAddressToLocationAsync(model.Address);

                if (latLng == null)
                {
                    _toastNotification.AddErrorToastMessage(_localizer["Sorry, we can't recognize this address"]);
                    return(View(model));
                }

                try
                {
                    vendor.Latitude  = latLng.Item1;
                    vendor.Longitude = latLng.Item2;

                    model.SetValuesToVendor(vendor);

                    var serviceOperatingHours = model.VendorHours;
                    foreach (var elem in serviceOperatingHours)
                    {
                        if (elem.IsWorking)
                        {
                            if (elem.CloseTime < elem.OpenTime)
                            {
                                ModelState.AddModelError("", "Invalid work hours");
                                _toastNotification.AddErrorToastMessage("Choose appropriate working hours");
                                return(View(model));
                            }

                            else
                            {
                                var workHours = new WorkHours(vendor.ID, elem.IsWorking, elem.Day, elem.OpenTime, elem.CloseTime);
                                await _vendorService.ChangeWorkHoursAsync(workHours);
                            }
                        }
                        else
                        {
                            var timeSpan  = new TimeSpan(0);
                            var workHours = new WorkHours(vendor.ID, elem.IsWorking, elem.Day, timeSpan, timeSpan);
                            await _vendorService.ChangeWorkHoursAsync(workHours);
                        }
                    }

                    await _vendorServiceADO.UpdateVendorAsync(vendor);

                    _toastNotification.AddSuccessToastMessage(_localizer["Service Updated"]);

                    return(RedirectToAction(nameof(MyVendors)));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    _toastNotification.AddErrorToastMessage(ex.Message);
                }
            }

            return(View(model));
        }
Exemple #3
0
        public async Task <IActionResult> Create(CreateEditVendorVM model)
        {
            try
            {
                if (ModelState.GetFieldValidationState("Title") == ModelValidationState.Valid &&
                    ModelState.GetFieldValidationState("Address") == ModelValidationState.Valid)
                {
                    if (model.Image != null && !model.Image.ValidImage())
                    {
                        ModelState.AddModelError("", _localizer["Invalid image type"]);
                        _toastNotification.AddErrorToastMessage(_localizer["Invalid image type"]);
                        return(View(model));
                    }

                    var latLng = await MapMethods.ConvertAddressToLocationAsync(model.Address);

                    if (latLng == null)
                    {
                        ModelState.AddModelError("", _localizer["Sorry, we can't recognize this address"]);
                        _toastNotification.AddErrorToastMessage(_localizer["Sorry, we can't recognize this address"]);
                        return(View(model));
                    }

                    var user = await _userManager.GetUserAsync(User);

                    user.VendorsCount++;
                    await _userManager.UpdateAsync(user);

                    var vendor = new Vendor();
                    vendor.UserID    = user.Id;
                    vendor.Latitude  = latLng.Item1;
                    vendor.Longitude = latLng.Item2;

                    model.SetValuesToVendor(vendor);
                    await _vendorService.CreateAsync(vendor);

                    var serviceOperatingHours = model.VendorHours;

                    foreach (var elem in serviceOperatingHours)
                    {
                        if (elem.IsWorking)
                        {
                            if (elem.CloseTime < elem.OpenTime)
                            {
                                ModelState.AddModelError("", "Invalid work hours");
                                _toastNotification.AddErrorToastMessage("Choose appropriate working hours");
                                await _vendorService.DeleteAsync(vendor);

                                return(View(model));
                            }

                            var workHours = new WorkHours(vendor.ID, elem.IsWorking, elem.Day, elem.OpenTime,
                                                          elem.CloseTime);
                            await _vendorService.AddWorkHoursAsync(workHours);
                        }
                        else
                        {
                            var timeSpan  = new TimeSpan(0);
                            var workHours = new WorkHours(vendor.ID, elem.IsWorking, elem.Day, timeSpan, timeSpan);
                            await _vendorServiceADO.InsertWorkHoursAsync(workHours);
                        }
                    }

                    _toastNotification.AddSuccessToastMessage(_localizer["Service Created"]);
                    return(RedirectToAction("MyVendors"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                _toastNotification.AddErrorToastMessage(ex.Message);
            }

            return(View(model));
        }