public async Task <bool> Handle(PatchRideRequestCommand request, CancellationToken cancellationToken)
            {
                // add logger or use decorator
                if (request.PatchDoc == null)
                {
                    // log error
                    throw new ApiException("Invalid patch document.");
                }

                var rideRequestToUpdate = await _db.RideRequests
                                          .FirstOrDefaultAsync(r => r.RideRequestId == request.RideRequestId);

                if (rideRequestToUpdate == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                var rideRequestToPatch = _mapper.Map <RideRequestForUpdateDto>(rideRequestToUpdate); // map the rideRequest we got from the database to an updatable rideRequest model

                request.PatchDoc.ApplyTo(rideRequestToPatch);                                        // apply patchdoc updates to the updatable rideRequest

                var validationResults = new CustomPatchRideRequestValidation().Validate(rideRequestToPatch);

                if (!validationResults.IsValid)
                {
                    throw new ValidationException(validationResults.Errors);
                }

                _mapper.Map(rideRequestToPatch, rideRequestToUpdate);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the requested changes. Please check the logs for more information.");
                }

                return(true);
            }
Esempio n. 2
0
            public async Task <RideRequestDto> Handle(AddRideRequestCommand request, CancellationToken cancellationToken)
            {
                if (await _db.RideRequests.AnyAsync(r => r.RideRequestId == request.RideRequestToAdd.RideRequestId))
                {
                    throw new ConflictException("RideRequest already exists with this primary key.");
                }

                var rideRequest = _mapper.Map <RideRequest> (request.RideRequestToAdd);

                _db.RideRequests.Add(rideRequest);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (saveSuccessful)
                {
                    return(await _db.RideRequests
                           .ProjectTo <RideRequestDto>(_mapper.ConfigurationProvider)
                           .FirstOrDefaultAsync(r => r.RideRequestId == rideRequest.RideRequestId));
                }
                else
                {
                    // add log
                    throw new Exception("Unable to save the new record. Please check the logs for more information.");
                }
            }
            public async Task <bool> Handle(UpdateRideRequestCommand request, CancellationToken cancellationToken)
            {
                // add logger or use decorator

                var recordToUpdate = await _db.RideRequests
                                     .FirstOrDefaultAsync(r => r.RideRequestId == request.RideRequestId);

                if (recordToUpdate == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                _mapper.Map(request.RideRequestToUpdate, recordToUpdate);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the requested changes. Please check the logs for more information.");
                }

                return(true);
            }
            public async Task <bool> Handle(DeleteRideRequestCommand request, CancellationToken cancellationToken)
            {
                // add logger (and a try catch with logger so i can cap the unexpected info)........ unless this happens in my logger decorator that i am going to add?

                var recordToDelete = await _db.RideRequests
                                     .FirstOrDefaultAsync(r => r.RideRequestId == request.RideRequestId);

                if (recordToDelete == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                _db.RideRequests.Remove(recordToDelete);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the new record. Please check the logs for more information.");
                }

                return(true);
            }