Пример #1
0
        public IActionResult EditLot(EditLotViewModel loteModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var lote = new Input.Lote
                    {
                        Id           = loteModel.LotId,
                        Descripcion  = loteModel.Descripcion,
                        NombreImagen = loteModel.Imagen?.FileName,
                        Imagen       = loteModel.Imagen != null?ConvertFileToByte(loteModel.Imagen) : null
                    };

                    TempData["SuccesMessage"] = "El lote se ha editado correctamente";

                    _loteManager.Save(lote);

                    return(RedirectToAction("Index"));
                }

                return(View(loteModel));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, string.Empty);
                return(RedirectToAction("Status", "Error", new { code = 404 }));
            }
        }
Пример #2
0
        public async Task <IActionResult> EditLot(EditLotViewModel model)
        {
            if (ModelState.IsValid && (model.FinishDate > model.PublicationDate))
            {
                await _lotService.UpdateLotPost(model.Id, model);

                return(RedirectToAction("Details", new { id = model.Id }));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Лот не найден");
            }

            return(View(model));
        }
Пример #3
0
        public async Task UpdateLotPost(int id, EditLotViewModel model)
        {
            var lot = await _context.Lots.FindAsync(model.Id);

            lot.Name          = model.Name;
            lot.Description   = model.Description;
            lot.StartCurrency = model.StartCurrency;
            lot.CategoryId    = model.CategoryId;
            lot.Category      = await _context.Categories.FindAsync(lot.CategoryId);

            lot.PublicationDate = model.PublicationDate.ToUniversalTime();
            lot.FinishDate      = model.FinishDate.ToUniversalTime();

            _context.Lots.Update(lot);
            await _context.SaveChangesAsync();
        }
Пример #4
0
        public async Task <IActionResult> EditLot(int id)
        {
            var lot = await _context.Lots.FindAsync(id);

            if (lot == null)
            {
                return(NotFound());
            }

            EditLotViewModel model = new EditLotViewModel
            {
                Name            = lot.Name,
                Description     = lot.Description,
                StartCurrency   = lot.StartCurrency,
                CategoryId      = lot.CategoryId,
                Category        = await _context.Categories.FindAsync(lot.CategoryId),
                PublicationDate = lot.PublicationDate,
                FinishDate      = lot.FinishDate
            };

            ViewData["CategoryId"] = new SelectList(_context.Set <Category>(), "Id", "Name");
            return(View(model));
        }
Пример #5
0
        public async Task <IActionResult> EditLot(EditLotViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(model.Id))
                {
                    return(NotFound());
                }

                AuctionLot lotToEdit = new AuctionLot
                {
                    Id          = model.Id,
                    Name        = model.Name,
                    Description = model.Description,
                    StartDate   = model.StartDate.Value,
                    EndDate     = model.EndDate.Value,
                    Status      = LotStatusProvider.GetOnModerationStatus(),
                    PriceInfo   = new PriceInfo
                    {
                        StartPrice = model.StartPrice.Value,
                        BidStep    = model.BidStep.Value
                    }
                };

                string newDbPath = $"/images/{User.Identity.Name}/{model.Name}/photo{Path.GetExtension(model.Photo.FileName)}";
                lotToEdit.PhotoSrc = newDbPath;

                try
                {
                    await lotLogic.Update(lotToEdit);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                    return(View(model));
                }

                string oldPath = $"{environment.WebRootPath + Path.GetDirectoryName(model.OldPhotoSrc)}";
                if (Directory.Exists(oldPath))
                {
                    Directory.Delete(oldPath, true);
                }

                string newPhysicalDirectory = Path.GetDirectoryName($"{environment.WebRootPath + newDbPath}");

                if (!Directory.Exists(newPhysicalDirectory))
                {
                    Directory.CreateDirectory(newPhysicalDirectory);
                }

                using (FileStream fs = new FileStream($"{environment.WebRootPath + newDbPath}", FileMode.Create))
                {
                    await model.Photo.CopyToAsync(fs);
                }

                return(View("Redirect", new RedirectModel
                {
                    InfoMessages = RedirectionMessageProvider.LotUpdatedMessages(),
                    RedirectUrl = "/Home/Lots",
                    SecondsToRedirect = ApplicationConstantsProvider.GetMaxRedirectionTime()
                }));
            }
            return(View(model));
        }