public async Task <ActionResult> AddVehicle(AddVehicleDto model) { try { var brand = await _unitOfWork.BrandRepository.GetBrandByNameAsync(model.Brand); if (brand == null) { return(BadRequest($"Brand {model.Brand} does not excist in system")); } var vehicleModel = await _unitOfWork.VehicleModelRepository.GetModelByNameAsync(model.Model); if (vehicleModel == null) { return(BadRequest($"Model {model.Model} does not excist in system")); } //kolla att regnummer ej finns så bilar ej blir duplicerade _unitOfWork.VehicleRepository.Add(model); if (await _unitOfWork.Complete()) { return(StatusCode(201)); } return(StatusCode(500, "Not able to save vehicle")); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void Add(AddVehicleDto vehicle) { var vehicleToAdd = _mapper.Map <Vehicle>(vehicle, opt => { opt.Items["repo"] = _context; }); _context.Entry(vehicleToAdd).State = EntityState.Added; }
/// <summary> /// Dodaje pojazd do bazy danych. /// </summary> /// <param name="newVehicle"></param> /// <returns></returns> public IHttpActionResult Post(AddVehicleDto newVehicle) { if (!ModelState.IsValid) { return(BadRequest()); } var model = context.VehicleModels.Find(newVehicle.VehicleModelId); var vehicle = Mapper.Map <Vehicle>(newVehicle); vehicle.VehicleModel = model; context.Vehicles.Add(vehicle); context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> AddVehicleToDriver([FromBody] AddVehicleDto vehicle) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var driverId = User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.DriverId)?.Value; var vehicleEntity = _mapper.Map <Vehicle>(vehicle); var res = await _usersRepository.AddVehicleToDriver(Guid.Parse(driverId), vehicleEntity); if (res != true) { return(Conflict()); } var vehicleToReturn = _mapper.Map <VehicleToReturnDto>(vehicleEntity); return(CreatedAtRoute("GetVehicle", new { id = vehicleEntity.Id }, vehicleToReturn)); }
public void AddVehicle_ShouldAlwaysAddVehicle() { var vehicleModel = new VehicleModel { Id = 1 }; var vehicles = new List <Vehicle>().AsQueryable(); var vehiclesMockSet = MockDbSetHelper.CreateMockSet(vehicles); var vehicleToAdd = new AddVehicleDto { Id = 1, VehicleModelId = 1, RegistrationNumber = "" }; var mockContext = new Mock <InstantDeliveryContext>(); mockContext.Setup(c => c.Vehicles).Returns(vehiclesMockSet.Object); mockContext.Setup(c => c.VehicleModels).ReturnsDbSet(vehicleModel); var controller = new VehiclesController(mockContext.Object); controller.Post(vehicleToAdd); vehiclesMockSet.Verify(m => m.Add(It.Is((Vehicle v) => v.Id == vehicleToAdd.Id)), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
/// <summary> /// Konstruktor modelu widoku /// </summary> /// <param name="service"></param> public VehiclesAddViewModel(VehiclesServiceProxy service) { vehiclesService = service; NewVehicle = new AddVehicleDto(); LoadModels(); }
/// <summary> /// Dodaje pojazd do bazy danych. /// </summary> /// <param name="vehicle"></param> /// <returns></returns> public async Task AddVehicle(AddVehicleDto vehicle) { await PostAsJson("", vehicle); }