コード例 #1
0
        public async Task <IHttpActionResult> PutDriver(int id, Driver driver)
        {
            try
            {
                string.Format("PutDriver({0}, Driver driver)", id);

                if (!ModelState.IsValid)
                {
                    //collect all the errors
                    foreach (ModelState modelState in ModelState.Values)
                    {
                        foreach (ModelError error in modelState.Errors)
                        {
                            LogUtil.Error(error.Exception);
                        }
                    }

                    return(BadRequest(ModelState));
                }

                if (id != driver.Id)
                {
                    return(BadRequest());
                }
                //Validate the data sent
                ValidateDriverDetails(driver);

                db.Entry(driver).State = EntityState.Modified;

                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {
                if (!DriverExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    LogUtil.Error(e);
                    throw new Exception(string.Format("The following error has occurred: {0}", e.Message));
                }
            }
            catch (Exception e)
            {
                LogUtil.Error(e);
                throw new Exception(string.Format("The following error has occurred: {0}", e.Message));
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        public async Task <Unit> Handle(CreateDriverCommand request, CancellationToken cancellationToken)
        {
            var driver = _mapper.Map <Driver>(request.DriverDto);

            driver.Address.Coordinates = await _geocoder.GetCoordinatesForAddress(driver.Address);

            _driverContext.Drivers.Add(driver);

            await _driverContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #3
0
        public async Task <Unit> Handle(DeleteDriverCommand request, CancellationToken cancellationToken)
        {
            var driver = await _driverContext.Drivers.FindAsync(
                new object[] { request.DriverId }, cancellationToken
                );

            if (driver == null)
            {
                throw new NotFoundException(nameof(Driver), request.DriverId.ToString());
            }

            _driverContext.Drivers.Remove(driver);

            await _driverContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #4
0
        public async Task <Unit> Handle(UpdateDriverCommand request, CancellationToken cancellationToken)
        {
            var driver = await _driverContext.Drivers.FindAsync(
                new object[] { request.DriverId }, cancellationToken
                );

            if (driver == null)
            {
                throw new NotFoundException(nameof(Driver), request.DriverId.ToString());
            }

            _mapper.Map(request.DriverDto, driver, typeof(ModifyDriverDto), typeof(Driver));

            driver.Address.Coordinates = await _geocoder.GetCoordinatesForAddress(driver.Address);

            await _driverContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #5
0
ファイル: GreeterService.cs プロジェクト: KiyEA/Service
        public override Task <Response> AddDriver(Driver request, ServerCallContext context)
        {
            Response response = new Response();

            try
            {
                var optionsBuilder = new DbContextOptionsBuilder <DriverContext>();
                optionsBuilder.UseSqlite("Data sourse = tbd.mdf");
                using var con = new DriverContext(optionsBuilder.Options);
                con.Drivers.Add(request);
                con.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogInformation(e.Message);
                response.ModificationResponse = Changes.AddFailed;
                return(Task.FromResult(response));
            }
            response.ModificationResponse = Changes.AddOk;
            return(Task.FromResult(response));
        }
コード例 #6
0
        public UpdateDriverCommandHandlerTest()
        {
            var rnd = new Random();

            _driverContext = new DriverContext(new DbContextOptionsBuilder <DriverContext>()
                                               .UseInMemoryDatabase("Test" + rnd.NextDouble().ToString())
                                               .Options);
            var driver = new Driver
            {
                FirstName = "Fulano",
                LastName  = "da Silva",
                Car       = new Car
                {
                    Brand             = "Ford",
                    Model             = "Focus",
                    RegistrationPlate = "LUJ1234"
                },
                Address = new Address
                {
                    Line1        = "Av Brasil, 9020",
                    Line2        = "Olaria",
                    Municipality = "Rio de Janeiro",
                    State        = "RJ",
                    ZipCode      = "21030-001",
                    Coordinates  = new Coordinates(-22.8370646, -43.2556345)
                }
            };

            _driverContext.Drivers.Add(driver);
            _driverContext.SaveChangesAsync();
            _driverId = driver.DriverId;

            _mapper = new Mapper(new MapperConfiguration(
                                     cfg => cfg.AddProfile(new DomainProfile())));
            _geocoder = new DummyGeocoder(new Coordinates(20, -30));
        }