public async Task <int> AddNewPart(AutoPartViewModel model)
        {
            var newPart = new AutoPart()
            {
                Name           = model.autoPart.Name,
                ListPrice      = model.autoPart.ListPrice,
                Price          = model.autoPart.Price,
                Price50        = model.autoPart.Price50,
                Price100       = model.autoPart.Price100,
                Description    = model.autoPart.Description,
                SellerComments = model.autoPart.Description,
                MainImageUrl   = model.autoPart.MainImageUrl,
                CreatedOn      = DateTime.UtcNow,
                CategoryId     = model.autoPart.CategoryId,
                SubCategoryId  = model.autoPart.SubCategoryId
            };

            newPart.PartGallery = new List <PartGallery>();

            foreach (var file in model.Gallery)
            {
                newPart.PartGallery.Add(new PartGallery()
                {
                    Name = file.Name,
                    URL  = file.URL
                });
            }

            await _db.AutoParts.AddAsync(newPart);

            await _db.SaveChangesAsync();

            return(newPart.Id);
        }
        //[Route("part-details/{id:int:min(1)}", Name = "partDetailsRoute")]
        public async Task <ViewResult> GetPart(int id)
        {
            var data = await _autoPartRepository.GetPArtById(id);

            var autopartViewModel = new AutoPartViewModel()
            {
                autoPart      = data,
                CoverImageUrl = data.MainImageUrl,
                Gallery       = data.PartGallery.Select(g => new GalleryModel()
                {
                    Id   = g.Id,
                    Name = g.Name,
                    URL  = g.URL
                }).ToList(),
            };

            return(View(autopartViewModel));
        }
        public async Task <IActionResult> AddNewPart(AutoPartViewModel partModel)
        {
            if (ModelState.IsValid)
            {
                if (partModel.CoverPhoto != null)
                {
                    string folder = "parts/cover/";
                    partModel.CoverImageUrl = await UploadImage(folder, partModel.CoverPhoto);
                }

                if (partModel.GalleryFiles != null)
                {
                    string folder = "parts/gallery/";

                    partModel.Gallery = new List <GalleryModel>();

                    foreach (var file in partModel.GalleryFiles)
                    {
                        var gallery = new GalleryModel()
                        {
                            Name = file.FileName,
                            URL  = await UploadImage(folder, file)
                        };
                        partModel.Gallery.Add(gallery);
                    }
                }


                int id = await _autoPartRepository.AddNewPart(partModel);

                if (id > 0)
                {
                    return(RedirectToAction(nameof(AddNewPart), new { isSuccess = true, bookId = id }));
                }
            }

            return(View());
        }
        public IActionResult AddNewPart(bool isSuccess = false, int partId = 0)
        {
            IEnumerable <Category>    CatList    = _unitOfWork.Category.GetAll();
            IEnumerable <SubCategory> SubCatList = _unitOfWork.SubCategory.GetAll();

            AutoPartViewModel autoPartViewModel = new AutoPartViewModel()
            {
                CategoryList = CatList.Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                SubCategoryList = SubCatList.Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
            };
            var model = autoPartViewModel;

            ViewBag.IsSuccess = isSuccess;
            ViewBag.PartId    = partId;
            return(View(model));
        }