public HttpResponse Add(CarCreateModel model) { if (this.userService.IsMechanic(this.User.Id)) { return(Unauthorized()); } var modelErrors = this.validator.ValidateCars(model); if (modelErrors.Any()) { return(Error(modelErrors)); } var car = new Car { Model = model.Model, Year = model.Year, PictureUrl = model.Image, PlateNumber = model.PlateNumber, OwnerId = this.User.Id }; this.data.Cars.Add(car); this.data.SaveChanges(); return(Redirect("/Cars/All")); }
public string AddCar(CarCreateModel model) { var userId = this.currentUserService.CurrentUser()?.Id; var numOtherCars = Context.OwnedCars.Where(x => x.UserId == userId).Count(); if (numOtherCars > 1 && !this.currentUserService.IsPremium()) { throw new UnauthorizedAccessException("Need to be premium to access more cars"); } var car = new OwnedCarDto() { Vin = model.Vin, UserId = currentUserService.UserId(), ManufacturedDate = model.ManufacturedDate, Nickname = model.Nickname }; Context.OwnedCars.Add(car); Context.SaveChanges(); Context.MileageRecordings.Add(new MileageRecordingDto() { Mileage = "0", RecordingDate = model.ManufacturedDate, OwnedCarId = car.Id }); if (model.CurrentMileage.HasValue) { Context.MileageRecordings.Add(new MileageRecordingDto() { Mileage = model.CurrentMileage.Value.ToString(), RecordingDate = DateTime.UtcNow, OwnedCarId = car.Id }); } Context.SaveChanges(); if (model.NextService != null) { var workId = this.workItemService.AddItem(new AddWorkItem() { ServiceTypeId = ServiceTypeDto.GeneralService, Vin = model.Vin }); this.repeatingItemService.AddRepeatingItem(new AddRepeatingSettings() { Amount = "12", Id = workId, Offset = model.NextService, TypeId = RepeatTypeDto.Age }); } return(car.Id); }
public ActionResult Create(CarCreateModel model) { if (ModelState.IsValid) { Database.Add(model.Make, model.Year); return(RedirectToAction("Index").WithNotification(Status.Success, "Car has been created succesfully")); } return(View(model)); }
public void CorollaCarCheck() { var model = new CarCreateModel() { Vin = VinCollection.Corolla, Nickname = "Corolla" }; var result = Service.AddCar(model); var CarModel = Service.Get(model.Vin); Assert.NotNull(result); Assert.NotNull(CarModel); Assert.Equal(model.Vin, CarModel.Vin); Assert.NotNull(CarModel.Base.Manufacturer); Assert.NotNull(CarModel.Base.CountryOfOrigin); }
public IActionResult CreateCar([FromBody] CarCreateModel model) { try { var newCar = Mapper.Map <CarDetailsModel>(model); newCar.Id = Guid.NewGuid(); Store.Add(newCar); Logger.LogInformation($"New Car has been added with ID {newCar.Id}"); return(CreatedAtRoute("CarDetails", new { id = newCar.Id })); }catch (Exception exception) { Logger.LogError(exception, "Error while creating new car"); return(StatusCode(500)); } }
public ICollection <string> ValidateCars(CarCreateModel car) { var errors = new List <string>(); if (car.Model.Length < 5 || car.Model.Length > 20) { errors.Add("Car model must be between 5 and 20 symbols long."); } if (car.Year < 1900 || car.Year > 2100) { errors.Add("Year is not valid!"); } if (!Regex.IsMatch(car.PlateNumber, CarPlateNumberRegularExpression)) { errors.Add("Plate number is not valid!"); } return(errors); }
public IActionResult CreateCar([FromBody] CarCreateModel model) => ReturnResult(() => this.carService.AddCar(model));
public ActionResult Create() { var model = new CarCreateModel(); return(View(model)); }