public ActionResult <BusDriver> Edit(BusDriverDTO busDriverDTO) { var busDriver = _busDriverRepository.GetBusDriverByDayIdBusColorAndTimeOfDay(busDriverDTO.DayId, busDriverDTO.BusColor, busDriverDTO.TimeOfDay); if (busDriver == null) { return(NotFound()); } var newDriver = _driverRepository.GetById(busDriverDTO.DriverId); if (newDriver == null) { return(NotFound()); } try { busDriver.Driver = newDriver; busDriver.DriverId = newDriver.DriverId; _busDriverRepository.SaveChanges(); return(Ok(busDriver)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void Edit_WrongDayIdAndWrongDriverId_ReturnsNotFound() { BusDriverDTO busDriverDTO = new BusDriverDTO() { DayId = 69, DriverId = 30, BusColor = BusColor.BEIGE, TimeOfDay = TimeOfDay.OCHTEND }; _busDriverRepository.Setup(b => b.GetBusDriverByDayIdBusColorAndTimeOfDay(1, BusColor.BEIGE, busDriverDTO.TimeOfDay)).Returns(_dummyDBContext.BusDriver1); _driverRepository.Setup(d => d.GetById(3)).Returns(_dummyDBContext.Driver3); ActionResult <BusDriver> actionResult = _controller.Edit(busDriverDTO); Assert.IsType <NotFoundResult>(actionResult?.Result); _busDriverRepository.Verify(a => a.SaveChanges(), Times.Never()); }
public ActionResult <BusDriver> Add(BusDriverDTO busDriverDTO) { var existingBusDriver = _busDriverRepository.GetBusDriverByDayIdBusColorAndTimeOfDay( busDriverDTO.DayId, busDriverDTO.BusColor, busDriverDTO.TimeOfDay); if (existingBusDriver != null) { return(BadRequest("Er is al een chauffeur die op die dag rijdt!")); } Day day = _customDayRepository.GetById(busDriverDTO.DayId); if (day == null) { day = _dayRepository.GetById(busDriverDTO.DayId); if (day == null) { return(NotFound()); } } var driver = _driverRepository.GetById(busDriverDTO.DriverId); if (driver == null) { return(NotFound()); } try { BusDriver newBusDriver = new BusDriver(day, driver, busDriverDTO.TimeOfDay, busDriverDTO.BusColor); _busDriverRepository.Add(newBusDriver); _busDriverRepository.SaveChanges(); return(Ok(newBusDriver)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void Edit_AllInfoValid_EditsBusDriver() { BusDriverDTO busDriverDTO = new BusDriverDTO() { DayId = _dummyDBContext.Day1.DayId, DriverId = 3, BusColor = BusColor.BEIGE, TimeOfDay = TimeOfDay.OCHTEND }; _busDriverRepository.Setup(b => b.GetBusDriverByDayIdBusColorAndTimeOfDay(busDriverDTO.DayId, busDriverDTO.BusColor, busDriverDTO.TimeOfDay)) .Returns(_dummyDBContext.BusDriver1); _driverRepository.Setup(d => d.GetById(busDriverDTO.DriverId)).Returns(_dummyDBContext.Driver3); ActionResult <BusDriver> actionResult = _controller.Edit(busDriverDTO); var response = actionResult?.Result as OkObjectResult; BusDriver editedBusDriver = response?.Value as BusDriver; Assert.Equal("Makker", editedBusDriver.Driver.Name); _busDriverRepository.Verify(a => a.SaveChanges(), Times.Once()); }