Ejemplo n.º 1
0
        public async Task <bool> AddCar(CarAnnounceModel announce)
        {
            try
            {
                CarMake carMake = await _dbContext.CarMakes.FindAsync(announce.CarMakeId);

                CarModel carModel = await _dbContext.CarModels.FindAsync(announce.CarModelId);

                DateTime addedDate = DateTime.Now;

                Car unicodeCar = await _dbContext.Cars.FindAsync(_dbContext.Cars.Max(c => c.Id));

                string unicode = (Int32.Parse(unicodeCar.AnnounceUniqueCode) + 1).ToString();

                Car car = new Car(carMake, carModel, announce.CarYear)
                {
                    AnnounceAddedDate  = addedDate,
                    AnnounceTypeId     = announce.AnnounceTypeId,
                    Price              = announce.Price,
                    CityId             = announce.CityId,
                    Description        = announce.Description,
                    PersonTypeId       = announce.PersonTypeId,
                    Email              = announce.Email,
                    PhoneNumber        = announce.PhoneNumber,
                    CarBodyTypeId      = announce.CarBodyTypeId,
                    CarKilometer       = announce.CarKilometer,
                    CarMotor           = announce.CarMotor,
                    CarMotorStrength   = announce.CarMotorStrength,
                    Color              = announce.Color,
                    Condition          = announce.CarCondition,
                    AnnounceUniqueCode = unicode,
                    TransmissionId     = announce.TransmissionId,
                    SpeedBoxId         = announce.SpeedBoxId,
                    FuelTypeId         = announce.FuelTypeId,
                };
                await _dbContext.Cars.AddAsync(car);

                foreach (var autoEquipmentId in announce.AutoEquipmentId)
                {
                    CarAutoEquipment carAutoEquipment = new CarAutoEquipment
                    {
                        CarId           = car.Id,
                        AutoEquipmentId = autoEquipmentId,
                    };
                    await _dbContext.CarAutoEquipments.AddAsync(carAutoEquipment);
                }

                //car files upload start
                AddDataPhoto(announce.Paths, car.Id, "lib/images/transport/car", FindTable.Car);


                //car files upload end
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return(true);
        }
        public async Task <IActionResult> Car(CarAnnounceModel announce)
        {
            if (ModelState.IsValid)
            {
                announce.Paths = filesPaths;
                var t = await _announceToAdd.AddCar(announce);

                if (!t)
                {
                    DeleteFile(filesPaths);
                }

                filesPaths.Clear();
                return(RedirectToAction("Index", "Home"));
            }
            DeleteFile(filesPaths);
            return(View(await _dataFind.SettingDataAsync("", announce.CarMakeId)));
        }