Exemplo n.º 1
0
        public async Task <IActionResult> Add(CreateAutopartInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.CarManufacturers = this.carsService.GetAllCarManufacturersAsKeyValuePairs();
                input.CarTypes         = this.carsService.GetAllCarTypesAsKeyValuePairs();
                input.Categories       = this.autopartsCharacteristicsService.GetAllAutopartCategories().Select(x => new KeyValuePair <string, string>(x.Id.ToString(), x.Value));
                input.Conditions       = this.autopartsCharacteristicsService.GetAllAutopartConditions().Select(x => new KeyValuePair <string, string>(x.Id.ToString(), x.Value));

                return(this.View(input));
            }

            var autopartDTO = new CreateAutopartDTO
            {
                Name              = input.Name,
                Price             = input.Price,
                Description       = input.Description,
                CarManufacturerId = input.CarManufacturerId,
                ModelId           = input.ModelId,
                CarTypeId         = input.CarTypeId,
                CategoryId        = input.CategoryId,
                ConditionId       = input.ConditionId,
                MakeYear          = input.CarMakeYear,
                Images            = input.Images,
            };

            var userId    = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var imagePath = $"{this.env.WebRootPath}/Images";

            await this.autopartsService.CreateAsync(autopartDTO, userId, imagePath);

            return(this.Redirect("/"));
        }
        public IActionResult Create(CreateAutopartInputModel input)
        {
            var autopartDto = new CreateAutopartDTO
            {
                Name       = input.Name,
                Price      = input.Price,
                CategoryId = input.CategoryId,
                MakeId     = input.MakeId,
                ModelId    = input.ModelId
            };

            autopartsService.Create(autopartDto);
            return(RedirectToAction("All"));
        }
Exemplo n.º 3
0
        public async Task CreateAsync(CreateAutopartDTO autopart, string userId, string imagePath)
        {
            var autopartEntity = new Autopart
            {
                Name        = autopart.Name,
                Price       = autopart.Price,
                Description = autopart.Description,
                CategoryId  = autopart.CategoryId,
                ConditionId = autopart.ConditionId,
                OwnerId     = userId,
            };

            var carEntity = this.carRepository.All()
                            .FirstOrDefault(x =>
                                            x.ModelId == autopart.ModelId &&
                                            x.CarTypeId == autopart.CarTypeId &&
                                            x.MakeYear == autopart.MakeYear);

            if (carEntity == null)
            {
                carEntity = new Car
                {
                    ModelId   = autopart.ModelId,
                    CarTypeId = autopart.CarTypeId,
                    MakeYear  = autopart.MakeYear,
                };
            }

            autopartEntity.Car = carEntity;

            foreach (var image in autopart.Images)
            {
                var extension = this.imageService.GetExtension(image.FileName);

                var dbImage = new Image
                {
                    OwnerId   = userId,
                    Extension = extension,
                };

                autopartEntity.Images.Add(dbImage);
                await this.imageService.Save(image, imagePath, dbImage.Id);
            }

            await this.autopartRepository.AddAsync(autopartEntity);

            await this.autopartRepository.SaveChangesAsync();
        }
        public void Create(CreateAutopartDTO autopart)
        {
            if (autopart == null)
            {
                return;
            }

            var autopartEntity = new Autopart
            {
                Name         = autopart.Name,
                CarModelId   = autopart.ModelId,
                CategoryId   = autopart.CategoryId,
                DateAdded    = DateTime.UtcNow,
                ModifiedDate = DateTime.UtcNow,
                Price        = autopart.Price
            };

            autopartRepository.Add(autopartEntity);
            autopartRepository.SaveChanges();
        }