Пример #1
0
        public async Task <ActionResult> DeleteCake(Guid?id)
        {
            if (id.HasValue)
            {
                IcedMemories.Domain.Models.Cake _cake = await WorkManager.CakeManager.LoadAsync(id.Value);

                if (_cake != null)
                {
                    WorkManager.BeginWork();
                    try
                    {
                        try
                        {
                            System.IO.File.Delete(Server.MapPath(_cake.ImageLink));
                        }
                        catch (Exception exImage)
                        { }
                        await WorkManager.SearchCategorySelectionManager.DeleteForCakeAsync(_cake.Id);

                        await WorkManager.CakeManager.DeleteAsync(_cake.Id);

                        WorkManager.CommitWork();
                    }
                    catch (Exception ex)
                    {
                        WorkManager.RollbackWork();
                    }
                }
            }
            return(RedirectToAction("Cakes"));
        }
Пример #2
0
        public async Task <ActionResult> CakeDetails(Models.CakeViewModel model)
        {
            IcedMemories.Domain.Models.Cake _cake = await WorkManager.CakeManager.LoadAsync(model.Id);

            String _imagePath      = "";
            String _imageExtension = "";

            if (_cake == null)
            {
                _cake = new Domain.Models.Cake();
            }
            if (_cake.Id == Guid.Empty)
            {
                _cake.DateAdded = System.DateTime.Now;
            }
            _cake.Title       = model.Title;
            _cake.Description = model.Description;
            if (model.ImageUpload != null)
            {
                if (model.ImageUpload.FileName.LastIndexOf(".") > -1)
                {
                    try
                    {
                        _imageExtension = model.ImageUpload.FileName.Substring(model.ImageUpload.FileName.LastIndexOf("."));
                        if ((_cake.ImageLink == null) || (_cake.ImageLink.Trim() == "") || (_cake.ImageLink.LastIndexOf(".") == -1))
                        {
                            _imagePath = "/Images/Cakes/" + _cake.DateAdded.Year.ToString("0000") + "/" + _cake.DateAdded.Month.ToString("00") + "/" + Guid.NewGuid().ToString() + _imageExtension;
                        }
                        else
                        {
                            _imagePath = _cake.ImageLink.Substring(0, _cake.ImageLink.LastIndexOf(".")) + _imageExtension;
                        }

                        if (System.IO.Directory.Exists(Server.MapPath("/Images/Cakes/" + _cake.DateAdded.Year.ToString("0000"))) == false)
                        {
                            System.IO.Directory.CreateDirectory(Server.MapPath("/Images/Cakes/" + _cake.DateAdded.Year.ToString("0000")));
                        }
                        if (System.IO.Directory.Exists(Server.MapPath("/Images/Cakes/" + _cake.DateAdded.Year.ToString("0000") + "/" + _cake.DateAdded.Month.ToString("00"))) == false)
                        {
                            System.IO.Directory.CreateDirectory(Server.MapPath("/Images/Cakes/" + _cake.DateAdded.Year.ToString("0000") + "/" + _cake.DateAdded.Month.ToString("00")));
                        }
                        try
                        {
                            if (System.IO.File.Exists(Server.MapPath(_imagePath)))
                            {
                                System.IO.File.Delete(Server.MapPath(_imagePath));
                            }
                            model.ImageUpload.SaveAs(Server.MapPath(_imagePath));
                            _cake.ImageLink = _imagePath;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    catch (Exception exUpload)
                    {
                    }
                }
            }
            else
            {
            }
            await WorkManager.CakeManager.SaveAsync(_cake);

            foreach (Models.SearchCategorySelection _sCat in model.Categories)
            {
                foreach (Models.SearchCategoryOptionSelection _sOption in _sCat.Options)
                {
                    if (_sOption.Selected == true)
                    {
                        if (_sOption.SelectionId == Guid.Empty)
                        {
                            IcedMemories.Domain.Models.SearchCategorySelection _newSelection = new Domain.Models.SearchCategorySelection();
                            _newSelection.CakeId           = _cake.Id;
                            _newSelection.CategoryOptionId = _sOption.Id;
                            await WorkManager.SearchCategorySelectionManager.SaveAsync(_newSelection);
                        }
                    }
                    else
                    {
                        if (_sOption.SelectionId != Guid.Empty)
                        {
                            await WorkManager.SearchCategorySelectionManager.DeleteAsync(_sOption.SelectionId);
                        }
                    }
                }
            }
            Models.CakeViewModel _returnCake = Mapper.Map <IcedMemories.Domain.Models.Cake, Models.CakeViewModel>(_cake);
            try
            {
                _returnCake.Categories = Mapper.Map <IList <IcedMemories.Domain.Models.SearchCategory>, IList <Models.SearchCategorySelection> >(await WorkManager.SearchCategoryManager.GetCategoriesAsync());
                IList <IcedMemories.Domain.Models.SearchCategoryOption> _selectedOptions = await WorkManager.SearchCategoryOptionManager.GetCategoryOptionsForCakeAsync(_cake.Id);

                IList <IcedMemories.Domain.Models.SearchCategorySelection> _selections = await WorkManager.SearchCategorySelectionManager.GetCategorySelectionsForCakeAsync(_cake.Id);

                foreach (Models.SearchCategorySelection _category in _returnCake.Categories)
                {
                    _category.Options = Mapper.Map <IList <IcedMemories.Domain.Models.SearchCategoryOption>, IList <Models.SearchCategoryOptionSelection> >(await WorkManager.SearchCategoryOptionManager.GetCategoryOptionsAsync(_category.Id));
                    foreach (IcedMemories.Domain.Models.SearchCategorySelection _selection in _selections)
                    {
                        foreach (Models.SearchCategoryOptionSelection _option in _category.Options)
                        {
                            if (_option.Id == _selection.CategoryOptionId)
                            {
                                _option.SelectionId = _selection.Id;
                                _option.Selected    = true;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            //return PartialView("CakeDetailsPartial", _returnCake);
            return(RedirectToAction("Cakes"));
        }