示例#1
0
        public async Task <CarModel> UpdateCar(string id, UpdateCarModel model)
        {
            _validator.Validate(model);

            var dbCarModel = await GetCar(id);

            if (model.Name != null)
            {
                dbCarModel.Name = model.Name;
            }

            if (model.Description != null)
            {
                dbCarModel.Description = model.Description;
            }

            if (model.RegistrationNumber != null)
            {
                dbCarModel.RegistrationNumber = model.RegistrationNumber;
            }

            await _carRepository.UpdateCar(dbCarModel);

            return(dbCarModel);
        }
示例#2
0
        public IActionResult UpdateCar(UpdateCarModel model)
        {
            var updateCar = _carRepository.GetWithId(model.Id);

            if (ModelState.IsValid)
            {
                if (model.Image != null)
                {
                    var path         = Path.GetExtension(model.Image.FileName);
                    var newImageName = Guid.NewGuid() + path;
                    var uploadPath   = Path.Combine(Directory.GetCurrentDirectory
                                                        (), "wwwroot/img/" + newImageName);

                    var stream = new FileStream(uploadPath, FileMode.Create);
                    model.Image.CopyTo(stream);

                    updateCar.Image = newImageName;
                }

                updateCar.Brand      = model.Brand;
                updateCar.Plate      = model.Plate;
                updateCar.Start_date = model.EntryDate;
                updateCar.End_date   = model.ExpireDate;
                _carRepository.Update(updateCar);
                return(RedirectToAction("Index", "Home"));
            }
            return(View(model));
        }
        /// <summary>
        /// BAD, It will not update existing entity, or possible bugs
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task <bool> UpdateCarAsync(UpdateCarModel car, long userId)
        {
            var cardb = await _context.Cars.Where(x => x.Id == car.Id && x.UserId == userId).FirstOrDefaultAsync();


            cardb.UserId       = car.UserId;
            cardb.Model        = car.Model;
            cardb.Manufacturer = car.Manufacturer;
            cardb.PlateNo      = car.PlateNo;
            cardb.Photo        = car.Photo;
            cardb.Year         = car.Year;
            cardb.Color        = car.Color;
            cardb.IsActive     = car.IsActive;
            _context.Cars.Update(cardb);
            return(await _context.SaveChangesAsync() > 0);

            //var Caar = new Car
            //{
            //    Id = car.Id,
            //    UserId = car.UserId,
            //    Model = car.Model,
            //    Manufacturer = car.Manufacturer,
            //    PlateNo = car.PlateNo,
            //    Photo = car.Photo,
            //    Year = car.Year,
            //    Color = car.Color,
            //    IsActive = car.IsActive

            //};
            //_context.Cars.Update(Caar) ;
            //return await _context.SaveChangesAsync() > 0;
        }
示例#4
0
        public IHttpActionResult Post([FromBody] UpdateCarRecordModel model)
        {
            var carProfileId = storageCarRegister.Persons.AddProfile(
                new AddProfileModel
            {
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                Patronymic  = model.Patronymic,
                PhoneNumber = model.PhoneNamber
            }
                );

            var updateCarModel = new UpdateCarModel
            {
                CarBrandId     = model.CarBrandId,
                CarModelId     = model.CarModelId,
                CarNumber      = model.CarNumber,
                Id             = model.CarId,
                OwnerProfileId = carProfileId
            };

            storageCarRegister.Cars.UpdateCar(updateCarModel);

            return(Ok());
        }
示例#5
0
        public async Task <IActionResult> Patch(int id, [FromBody] UpdateCarModel updatedCar)
        {
            var car = await _carService.UpdateCar(id, updatedCar);

            var carDto = _mapper.Map <CarModel>(car);

            return(Ok(carDto));
        }
示例#6
0
        private bool UpdateCar(CarsRepository repository, long carId, long carBrandId, long carBrandModelId)
        {
            var updatedCarNumber = "testUpdate";
            var updateCarModel   = new UpdateCarModel
            {
                Id             = carId,
                OwnerProfileId = null,
                CarBrandId     = carBrandId,
                CarModelId     = carBrandModelId,
                CarNumber      = updatedCarNumber
            };

            return(repository.UpdateCar(updateCarModel));
        }
示例#7
0
        public IActionResult UpdateCar(int id)
        {
            var incomingCar = _carRepository.GetWithId(id);

            UpdateCarModel model = new UpdateCarModel
            {
                Id         = incomingCar.Id,
                Brand      = incomingCar.Brand,
                Plate      = incomingCar.Plate,
                EntryDate  = incomingCar.Start_date,
                ExpireDate = incomingCar.End_date
            };

            return(View(model));
        }
示例#8
0
        public async Task <IActionResult> Update([FromBody] UpdateCarModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var car     = Mapper.Map <Car>(model);
            var updated = await _carService.UpdateAsync(car);

            if (!updated)
            {
                return(NotFound());
            }

            return(Ok());
        }
        public async Task <IActionResult> Update([FromBody] UpdateCarModel car)
        {
            try
            {
                //Added user verify
                var updateResult = await _carRepository.UpdateCarAsync(car, this.UserId());

                if (updateResult)
                {
                    return(Ok("Car is updated"));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message + "" + ex.InnerException));
            }
        }
示例#10
0
        public static bool UpdateCarModel(CarDbContext context, UpdateCarModel updateCarModel)
        {
            var carBrand = context.CarBrands.SingleOrDefault(cb => cb.Id == updateCarModel.CarBrandId);

            if (carBrand == null)
            {
                return(false);
            }

            var carModel = context.CarModels.SingleOrDefault(cb => cb.Id == updateCarModel.Id);

            if (carModel == null)
            {
                return(false);
            }

            carModel.Name       = updateCarModel.Name;
            carModel.Photo      = updateCarModel.Photo;
            carModel.CarBrand   = carBrand;
            carModel.CarBrandId = carBrand.Id;
            context.SaveChanges();
            return(true);
        }
示例#11
0
 public JsonResult Put([FromBody] UpdateCarModel updateCarModel)
 {
     CarDbCommand.UpdateCarModel(_context, updateCarModel);
     return(new JsonResult(null));
 }
        public bool UpdateCar(UpdateCarModel model)
        {
            var inCarId = new SqlParameter
            {
                ParameterName = "CarId",
                Value         = model.Id,
                DbType        = System.Data.DbType.Int64,
                Direction     = System.Data.ParameterDirection.Input
            };
            var inOwnerProfileId =
                model.OwnerProfileId == null ?
                new SqlParameter
            {
                ParameterName = "OwnerProfileId",
                Value         = DBNull.Value,
                DbType        = System.Data.DbType.Int64,
                Direction     = System.Data.ParameterDirection.Input
            }
                :
            new SqlParameter
            {
                ParameterName = "OwnerProfileId",
                Value         = model.OwnerProfileId,
                DbType        = System.Data.DbType.Int64,
                Direction     = System.Data.ParameterDirection.Input
            };
            var inCarBrandId = new SqlParameter
            {
                ParameterName = "CarBrandId",
                Value         = model.CarBrandId,
                DbType        = System.Data.DbType.Int64,
                Direction     = System.Data.ParameterDirection.Input
            };
            var inCarModelId = new SqlParameter
            {
                ParameterName = "CarModelId",
                Value         = model.CarModelId,
                DbType        = System.Data.DbType.Int64,
                Direction     = System.Data.ParameterDirection.Input
            };
            var inCarNumber =
                string.IsNullOrEmpty(model.CarNumber) ?
                new SqlParameter
            {
                ParameterName = "CarNumber",
                Value         = DBNull.Value,
                DbType        = System.Data.DbType.String,
                Direction     = System.Data.ParameterDirection.Input
            }
                :
            new SqlParameter
            {
                ParameterName = "CarNumber",
                Value         = model.CarNumber,
                DbType        = System.Data.DbType.String,
                Direction     = System.Data.ParameterDirection.Input
            };
            var outResult = new SqlParameter
            {
                ParameterName = "Result",
                DbType        = System.Data.DbType.Boolean,
                Direction     = System.Data.ParameterDirection.Output
            };

            var sql = "exec UpdateCar @CarId, @OwnerProfileId, @CarBrandId, @CarModelId, @CarNumber, @Result OUT";

            using (var dbContext = new CarsContext())
            {
                _ = dbContext.Database.ExecuteSqlCommand(sql, inCarId, inOwnerProfileId, inCarBrandId, inCarModelId, inCarNumber, outResult);
            }

            if (!Boolean.TryParse(outResult.Value.ToString(), out bool result))
            {
                return(false);
            }

            return(result);
        }
示例#13
0
 public Task <CarModel> UpdateCar(string id, [FromBody] UpdateCarModel model)
 {
     return(_carService.UpdateCar(id, model));
 }
示例#14
0
        public async Task <Car> UpdateCar(int id, UpdateCarModel updatedCar)
        {
            var car = await _carRepository.GetById(id);

            if (car == null)
            {
                throw new NotFoundException("A car with such Id was not found!");
            }

            if (!string.IsNullOrEmpty(updatedCar.RegistrationNumber))
            {
                if (await CheckIfRegistrationNumberExists(updatedCar.RegistrationNumber))
                {
                    throw new EntryAlreadyExistsException("A car with such Registration Number already exists!");
                }

                car.RegistrationNumber = updatedCar.RegistrationNumber;
            }

            if (!string.IsNullOrEmpty(updatedCar.Color))
            {
                car.Color = updatedCar.Color;
            }

            if (updatedCar.FuelId.HasValue)
            {
                car.FuelId = (int)updatedCar.FuelId.Value;
            }

            if (updatedCar.TransmissionId.HasValue)
            {
                car.TransmissionId = (int)updatedCar.TransmissionId.Value;
            }

            if (updatedCar.FabricationYear.HasValue)
            {
                car.FabricationYear = updatedCar.FabricationYear.Value;
            }

            if (!string.IsNullOrEmpty(updatedCar.Model))
            {
                car.Model = updatedCar.Model;
            }

            if (!string.IsNullOrEmpty(updatedCar.Brand))
            {
                car.Brand = updatedCar.Brand;
            }

            if (updatedCar.CarBodyId.HasValue)
            {
                car.CarBodyId = (int)updatedCar.CarBodyId.Value;
            }

            if (updatedCar.AirCoditioning.HasValue)
            {
                car.AirCoditioning = updatedCar.AirCoditioning.Value;
            }

            if (updatedCar.PricePerDay.HasValue)
            {
                car.PricePerDay = updatedCar.PricePerDay.Value;
            }

            if (updatedCar.NumberOfDoors.HasValue)
            {
                car.NumberOfDoors = updatedCar.NumberOfDoors.Value;
            }

            if (updatedCar.NumberOfSeats.HasValue)
            {
                car.NumberOfSeats = updatedCar.NumberOfSeats.Value;
            }

            _carRepository.Update(car);
            await _carRepository.SaveAll();

            return(car);
        }