private static void CreateBetAuctionEntity(AuctionEditVM editVM, Auction auction, Client client) { BetAuction betAuction = new BetAuction { Auction = auction, Bet = editVM.Price, Client = client }; db.BetAuction.Add(betAuction); }
private static Product CreateProductEntity(AuctionEditVM editVM, Image image) { Product product = new Product { Image = image, Price = editVM.Price, Title = editVM.Title }; db.Products.Add(product); return(product); }
public static async Task CreateEntity(AuctionEditVM editVM, Auction auction, object userId, HttpPostedFileBase upload) { Image image = CreateImageEntity(editVM, upload); Product product = CreateProductEntity(editVM, image); var client = db.Clients.Where(c => c.AccountId == (int)userId).FirstOrDefault(); CreateAuctionEntity(editVM, auction, product, client); CreateBetAuctionEntity(editVM, auction, client); await db.SaveChangesAsync(); }
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 }); } }
//------------------- 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); }
//Create->flag:false, Edit->flag:true private static Image CreateImageEntity(AuctionEditVM editVM, HttpPostedFileBase upload, bool flag = false) { //byte[] myBytes = new byte[editVM.Upload.ContentLength]; //editVM.Upload.InputStream.Read(myBytes, 0, editVM.Upload.ContentLength); //Image image = new Image { FileName = editVM.Title, ImageData = myBytes }; byte[] myBytes = new byte[upload.ContentLength]; upload.InputStream.Read(myBytes, 0, upload.ContentLength); Image image = new Image { FileName = editVM.Title, ImageData = myBytes }; if (flag == false) { db.Images.Add(image); } return(image); }
private static void CreateAuctionEntity(AuctionEditVM editVM, Auction auction, Product product, Client client) { auction.ActorId = (int)client.Id; auction.Product = product; if (editVM.Step != null || editVM.Step == 0) { auction.Step = (decimal)editVM.Step; } else { auction.Step = editVM.Price * (decimal)0.1; } auction.RedemptionPrice = editVM.RedemptionPrice == 0 ? auction.Product.Price * 3 : editVM.RedemptionPrice; auction.Description = editVM.Description; auction.BeginTime = editVM.DayBegin + editVM.TimeBegin; auction.EndTime = auction.BeginTime + TimeSpan.FromHours((int)editVM.Duration); auction.WinnerId = (int)client.Id; // в начале! - потом перезапишется db.Auctions.Add(auction); }
//+Edit public ActionResult Create(AuctionEditVM data, int?imageId) //data, imageId - возвр. JSON ajax-метод Detai.html->click("Edit") { var accountId = Session["accountId"] ?? 0; if ((int)accountId == 0) { //Create return(RedirectToAction("Index", new { alert = "Вы сейчас не можете создать лот. Залогинтесь!" })); } //сначала надо проверить явл. ли юзер moder'om? var roleAccountLinks = db.RoleAccountLinks.Where(r => r.AccountId == (int)accountId && r.Role.RoleName.Contains("moder")).ToList(); if (roleAccountLinks == null || roleAccountLinks.Count() == 0) { return(RedirectToAction("Index", new { alert = "Вы пока не можете создать лот. Проверте ваш баланс!" })); } else { //Edit if (data != null && data.Id != 0) { var imageBO = DependencyResolver.Current.GetService <ImageBO>(); ImageVM imageVM = null; if (imageId != null) { ImageBO image = imageBO.Load((int)imageId); imageVM = mapper.Map <ImageVM>(image); } //подготов. данных для 2го захода->потом из ajax снова в этот контроллер, в котор. вызвать объект из сессии Session["editImg"] = imageVM; Session["auctionEdit"] = data; return(new JsonResult { Data = "Форма редактирования подготовлена!", JsonRequestBehavior = JsonRequestBehavior.AllowGet }); } //данные для 2-го захода var editVM = Session["auctionEdit"]; ViewBag.editImg = Session["editImg"]; ViewBag.Title = "Edit"; return(View(editVM)); } }