public async Task <IActionResult> Create(CreateLotViewModel model) { if (ModelState.IsValid) { Car car = new Car { Name = model.Name, Desc = model.Desc, Year = model.Year, Mileage = model.Milleage, Transmission = (ushort)CarData.transmission.IndexOf(model.Transmission), Fuel = (ushort)CarData.fuel.IndexOf(model.Fuel), Body = (ushort)CarData.body.IndexOf(model.Body), Drive = (ushort)CarData.drive.IndexOf(model.Drive), EngineVolume = double.Parse(model.EngineVolume), Image = null }; User user = await _userManager.GetUserAsync(HttpContext.User); bool check = false; var roles = await _userManager.GetRolesAsync(user); if (roles.Any(r => r == "admin")) { check = true; } CarLot carLot = new CarLot { Name = model.Name, StartPrice = model.Price, Price = model.Price, Exposing = DateTime.UtcNow, Ending = DateTime.UtcNow.AddDays(model.Duration), User = await _userManager.GetUserAsync(HttpContext.User), Car = car, Applyed = check }; carRepository.AddCar(car); string src = "/images/" + car.Id + ".jpg"; using (var fileStream = new FileStream(_appEnviroment.WebRootPath + src, FileMode.Create)) { await model.Image.CopyToAsync(fileStream); } car.Image = src; carRepository.Update(car); carLotsRepository.AddLotDB(carLot); return(RedirectToAction("Actual")); } return(View()); }
public async Task <IActionResult> CreateLot(CreateLotViewModel model) { if (ModelState.IsValid) { AuctionLot toAdd = new AuctionLot { Name = model.Name, User = new User { UserName = User.Identity.Name }, Description = model.Description, StartDate = model.StartDate.Value, EndDate = model.EndDate.Value, PriceInfo = new PriceInfo { StartPrice = model.StartPrice.Value, CurrentPrice = model.StartPrice.Value, BidStep = model.BidStep.Value } }; string dbPhotoPath = $"/images/{User.Identity.Name}/{model.Name}/photo{Path.GetExtension(model.Photo.FileName)}"; toAdd.PhotoSrc = dbPhotoPath; try { await lotLogic.Create(toAdd); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(View(model)); } string physicalDirectory = Path.GetDirectoryName($"{environment.WebRootPath + dbPhotoPath}"); if (!Directory.Exists(physicalDirectory)) { Directory.CreateDirectory(physicalDirectory); } using (FileStream fs = new FileStream($"{environment.WebRootPath + dbPhotoPath}", FileMode.Create)) { await model.Photo.CopyToAsync(fs); } return(View("Redirect", new RedirectModel { InfoMessages = RedirectionMessageProvider.LotCreatedMessages(), RedirectUrl = "/Home/Lots", SecondsToRedirect = ApplicationConstantsProvider.GetMaxRedirectionTime() })); } return(View(model)); }
public IActionResult CreateNewLot(CreateLotViewModel input) { if (!ModelState.IsValid) { ViewBag.Products = SetProductsToSelectListItems(); return(this.View()); } var lot = _deliveriesService.CreateNewLot(input.SerialNumber, input.PackagesCount, input.PackagesWeight, input.ProductName); return(this.Redirect("/Deliveries/Create")); }
public async Task <IActionResult> Create(CreateLotViewModel model) { if (ModelState.IsValid) { Lot lot = new Lot { Name = model.Name, Year = model.Year, EngineVolume = double.Parse(model.EngineVolume), Milleage = model.Mileage, Price = model.Price, Transmission = (ushort)transmission.IndexOf(model.Transmission), Fuel = (ushort)fuel.IndexOf(model.Fuel), Body = (ushort)body.IndexOf(model.Body), Drive = (ushort)drive.IndexOf(model.Drive), Desc = model.Desc, Exposing = DateTime.Now, Ending = DateTime.Now.AddDays(model.Duration), User = await _userManager.GetUserAsync(HttpContext.User) }; db.Lots.Add(lot); db.SaveChanges(); if (model.Image != null) { // путь к папке Files string path = "/img/" + lot.Id + ".jpg"; // сохраняем файл в папку Files в каталоге wwwroot using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await model.Image.CopyToAsync(fileStream); } lot.Image = path; } db.Lots.Update(lot); db.SaveChanges(); return(RedirectToAction("Actual")); } return(View()); }
public async Task <IActionResult> CreateLot(CreateLotViewModel model) { var user = await _userManager.FindByNameAsync(model.UserName); var hours = int.Parse(user.TimeZone); if (ModelState.IsValid && model.FinishDate > model.PublicationDate && model.PublicationDate > DateTime.UtcNow.AddHours(hours) && model.FinishDate > DateTime.UtcNow.AddHours(hours)) { var lot = new Lot { Name = model.Name, CategoryId = model.CategoryId, Category = await _context.Categories.FindAsync(model.CategoryId), Description = model.Description, StartCurrency = model.StartCurrency, User = user, PublicationDate = model.PublicationDate.AddHours(-1 * hours), FinishDate = model.FinishDate.AddHours(-1 * hours), IsEmailSended = false }; await _context.Lots.AddAsync(lot); await _context.SaveChangesAsync(); double diff = (model.FinishDate - DateTime.UtcNow.AddHours(hours)).TotalMinutes + 1; BackgroundJob.Schedule <BackgroundEndLotCheking>(x => x.ChekLot(lot.Id), TimeSpan.FromMinutes(diff)); return(RedirectToAction("Index")); } return(RedirectToAction("CreateLot")); }
public IActionResult CreateLot(CreateLotViewModel loteModel) { try { if (ModelState.IsValid) { var lote = new Input.Lote { Id = loteModel.LotId, Descripcion = loteModel.Descripcion, NombreImagen = loteModel.Imagen?.FileName, Imagen = ConvertFileToByte(loteModel.Imagen) }; TempData["SuccesMessage"] = "Se ha creado un lote"; _loteManager.Save(lote); return(RedirectToAction("Index")); } else { if (loteModel.Imagen == null) { ModelState.AddModelError("Image", "Error al cargar la imagen"); } return(View(loteModel)); } } catch (Exception ex) { _logger.LogError(ex, string.Empty); return(RedirectToAction("Status", "Error", new { code = 404 })); } }
public ActionResult Create(CreateLotViewModel createLot, HttpPostedFileBase uploadImage) { if (ModelState.IsValid) { if (uploadImage != null) { uploadImage.SaveAs( Server.MapPath("~/Content/Image/" + System.IO.Path.GetFileName(uploadImage.FileName))); createLot.LotImageUrl = "/Content/Image/" + System.IO.Path.GetFileName(uploadImage.FileName); } var lot = Mapper.Map <CreateLotViewModel, Lot>(createLot); lot.StarLotSaleDate = DateTime.Today; lot.CurrentPrice = lot.StartLotPrice; lot.IsSold = false; var currentCustomer = dataBase.Customers.GetByName(User.Identity.Name); CustomerLot customerLot = new CustomerLot() { Lot = lot, LotId = lot.LotId, Customer = currentCustomer, CustomerId = currentCustomer.CustomerId, CustomerBet = 0, LotMaker = true, WinAuction = false }; dataBase.CustomerLots.Create(customerLot); dataBase.Save(); return(RedirectToAction("Index", "Customer")); } else { ModelState.AddModelError("", "Данные введены некоректно. Пожалуйста исправте ошибки!"); } return(View("AddNewLot", createLot)); }