Example #1
0
        public virtual async Task <IServiceResponse <U> > AddAsync(U dto)
        {
            if (dto == null)
            {
                return(new ServiceResponse <U>(false, ServiceResponseMessage.Add_Null_Error <T>()));
            }

            var entity = _mapper.Map <T>(dto);

            var dataValidationResult = await ExecuteWithoutTransactionAsync <IEnumerable <IDataValidationFailure> >(async() => await _unitOfWork.Repository <T>().CanAddAsync(entity));

            if (dataValidationResult.Any())
            {
                return(new ServiceResponse <U>(false, $"Error whilst persisting {typeof(U).Name} to Database", null, dataValidationResult));
            }

            var id = await ExecuteWithTransactionAsync <int>(async() => {
                var returnValue = await _unitOfWork.Repository <T>().AddAsync(entity);
                return(returnValue);
            });

            if (id == 0)
            {
                return(new ServiceResponse <U>(false, $"Error whilst persisting {typeof(U).Name} to Database,Please contact System Administrator"));
            }

            dynamic responseDTO = GenerateResponseDTO(dto, id);

            return(new ServiceResponse <U>(true, ServiceResponseMessage.Add_Success <T>(), responseDTO));
        }
Example #2
0
        public virtual async Task <IServiceResponse <int> > AddRangeAsync(IEnumerable <U> dtos)
        {
            if (dtos == null || !dtos.Any())
            {
                return(new ServiceResponse <int>(false, ServiceResponseMessage.Add_Null_Error <U>()));
            }

            var validDtos = dtos.Where(x => x != null);
            var entities  = _mapper.Map <IEnumerable <T> >(validDtos);
            var response  = await ExecuteWithTransactionAsync <int>(async() =>
            {
                var result = await _unitOfWork.Repository <T>().AddRangeAsync(entities);

                return(result);
            });

            return(new ServiceResponse <int>(true, ServiceResponseMessage.Add_Success <T>(), response));
        }