Esempio n. 1
0
        public async Task <CommandResult <bool> > DeleteDiveLocationAsync(DiveLocationDto diveLocationDto)
        {
            var cmd = new CommandResult <bool>();

            cmd.DataResult = false; // Default return value
            try
            {
                var diveLocation = _objectMapper.Map <DiveLocation>(diveLocationDto); // Map the DTO to an entity

                using (var scope = UnitOfWorkScopeFactory.Create())                   // Always use a Unit of Work/Transaction for multi-step operations.
                {
                    // Build the detail object from the DTO. We could also do this from AutoMapper if we wanted to.
                    var diveDetail = new DiveLocationDetail()
                    {
                        DiveLocationId = diveLocationDto.Id, ImageData = diveLocationDto.ImageData
                    };

                    var detailCmd = await _diveLocationService.DeleteDiveLocationDetailsAsync(diveDetail); // Remove the detail record

                    var locationCmd = await _diveLocationService.DeleteAsync(diveLocation);                // Remove the primary DiveLocation record

                    if (detailCmd.ValidationResult.IsValid && !detailCmd.HasException &&
                        locationCmd.ValidationResult.IsValid && !locationCmd.HasException) // If all our business logic succeeds and there are no exceptions then commit
                    {
                        scope.Commit();                                                    // Commit the transaction
                        cmd.DataResult = true;
                        cmd.Message    = "Dive location successfully deleted.";            //TODO: should put this message in a resource file possibly for translation
                        this.Logger.LogInformation(cmd.Message, diveLocationDto);          // Log the info
                    }
                    else
                    {
                        cmd.Message = "An Error occured while deleting this dive location."; //TODO: should put this message in a resource file possibly for translation
                        this.Logger.LogWarning(cmd.Message, diveLocationDto);                // Log a warning because the error will be handled. We may still be able to recover.
                    }
                }
            }
            catch (BusinessException ex) // The exception was handled at a lower level if we get BusinessException
            {
                cmd.Exception = ex;
                this.ExceptionManager.HandleException(ex, DefaultExceptionPolicies.ApplicationReplacePolicy);
            }
            catch (AutoMapperMappingException ex) // Mapping Exception
            {
                this.ExceptionManager.HandleException(ex, DefaultExceptionPolicies.ApplicationWrapPolicy);
            }
            catch (ApplicationException ex) // We didn't do a good job handling exceptions at a lower level or have failed logic in this class
            {
                cmd.Exception = ex;
                this.ExceptionManager.HandleException(ex, DefaultExceptionPolicies.ApplicationWrapPolicy);
            }

            return(cmd);
        }
        public async Task <CommandResult <bool> > UpdateDiveLocationDetailsAsync(DiveLocationDetail locationDetail)
        {
            var result = new CommandResult <bool>();

            try
            {
                await _diveLocationDetailRepository.UpdateAsync(locationDetail);

                this.Logger.LogInformation("Updating Dive Location Details {0}.", locationDetail);
                result.DataResult = true;
            }
            catch (ApplicationException ex)
            {
                result.DataResult = false;
                result.Exception  = ex;
                this.ExceptionManager.HandleException(ex, DefaultExceptionPolicies.BusinessWrapPolicy);
            }
            return(result);
        }