Ejemplo n.º 1
0
        public async Task <IActionResult> Create(AddSpareCategoriesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var spareCategories = await _context.SpareCategories.FirstOrDefaultAsync
                                          (sc => sc.Name.ToUpper() == model.Name.ToUpper());

                if (spareCategories != null)
                {
                    var error = "The Spare Catergory can't be Added because it already exists.";
                    //return RedirectToAction("Index", new RouteValueDictionary(new { Controller = "SpareCategories", error }));
                    ModelState.AddModelError(string.Empty, error);
                    return(View(model));
                }

                var path = string.Empty;

                if (model.ImageFile != null)
                {
                    path = await _imageHelper.UploadImageAsync(model.ImageFile, "SpareCategories");
                }

                spareCategories = _converterHelper.ToSpareCategoryEntity(model, path);

                _context.SpareCategories.Add(spareCategories);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Ejemplo n.º 2
0
 public SpareCategoryEntity ToSpareCategoryEntity(AddSpareCategoriesViewModel model, string path)
 {
     return(new SpareCategoryEntity
     {
         Id = model.Id,
         Name = model.Name,
         ImageUrl = path
     });
 }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, AddSpareCategoriesViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var path = model.ImageUrl;

                if (model.ImageFile != null)
                {
                    path = await _imageHelper.UploadImageAsync(model.ImageFile, "SpareCategories");

                    //TODO: investigar borrar imagen anterior
                    _imageHelper.DeleteImageAsync(model.ImageUrl);
                }

                var sparecategoryEntity = _converterHelper.ToSpareCategoryEntity(model, path);

                try
                {
                    _context.SpareCategories.Update(sparecategoryEntity);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SpareCategoryEntityExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var model = await _context.SpareCategories.FindAsync(id);

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

            var sparecategoryviewmodel = new AddSpareCategoriesViewModel
            {
                Id = model.Id,
                BrandCategories = model.BrandCategories,
                ImageUrl        = model.ImageUrl,
                Name            = model.Name
            };

            return(View(sparecategoryviewmodel));
        }