public async Task <ActionResult> Create(AuctionEditVM editVM, int?id, HttpPostedFileBase upload)//
        {
            var    userId  = Session["accountId"] ?? 0;
            string message = "";

            if (id == null)
            {
                Auction auction = new Auction();
                if (ModelState.IsValid) //AuctionVM
                {
                    if ((int)userId != 0)
                    {
                        await BuilderModels.CreateEntity(editVM, auction, userId, upload);

                        message = "Данные записаны!";
                    }
                    else
                    {
                        message = "Ошибка. Данные не записаны!";
                    }

                    return(new JsonResult {
                        Data = message, JsonRequestBehavior = JsonRequestBehavior.DenyGet
                    });
                }

                return(View(auction));
            }
            else
            {
                //Auction auction = await db.Auctions.FindAsync(id);
                AuctionBO auctionBO = DependencyResolver.Current.GetService <AuctionBO>();
                auctionBO = auctionBO.LoadAsNoTracking((int)id);//.Load((int)id);
                if (auctionBO == null)
                {
                    return(HttpNotFound());
                }
                message = "Данные перезаписаны!";
                BuilderModels.mapper = mapper;
                await BuilderModels.EditEntityAsync(editVM, auctionBO, userId, upload);    //

                return(new JsonResult {
                    Data = message, JsonRequestBehavior = JsonRequestBehavior.DenyGet
                });
            }
        }
Пример #2
0
        //------------------- E.D.I.T.--------------------------------------------
        public static async Task EditEntityAsync(AuctionEditVM editVM, AuctionBO auctionBO, object userId, HttpPostedFileBase upload) //
        {
            var editBO = mapper.Map <AuctionBO>(editVM);                                                                              //1)из формы

            EditEntity(auctionBO, editBO, 0);

            //2)Product-----------------
            ProductBO productBO = DependencyResolver.Current.GetService <ProductBO>();

            productBO = productBO.LoadAll().FirstOrDefault(p => p.Title == editVM.Title);
            ProductBO productEdit = mapper.Map <ProductBO>(editVM);

            EditEntity(productBO, productEdit, 1);

            //3)BetAuction----------
            BetAuctionBO betAuctionBO = DependencyResolver.Current.GetService <BetAuctionBO>();

            betAuctionBO = betAuctionBO.LoadAll().Where(b => b.AuctionId == auctionBO.Id && b.ClientId == auctionBO.ActorId).FirstOrDefault();
            BetAuctionBO betEdit = mapper.Map <BetAuctionBO>(editVM);

            EditEntity(betAuctionBO, betEdit, 4);

            //4)Image----------------
            ImageBO imageBO = null;

            if (upload != null)
            {
                imageBO = DependencyResolver.Current.GetService <ImageBO>();
                imageBO = imageBO.LoadAll().FirstOrDefault(i => i.Id == auctionBO.Product.ImageId);
                if (imageBO != null)
                {
                    Image   image       = CreateImageEntity(editVM, upload, true); //true->edit
                    ImageBO editImageBO = mapper.Map <ImageBO>(image);
                    EditEntity(imageBO, editImageBO, 3);
                    imageBO.Save(imageBO);

                    //4)~recurs           //при редактир. нет новой записи ->переустанов. ссылок не треб.
                    //productBO.ImageId = newImageBO.Id;
                    productBO.Image = imageBO;
                }
            }
            productBO.Save(productBO);
            auctionBO.Product = productBO;
            auctionBO.Save(auctionBO);
        }