public static async Task <TResult> Command <TEntity, TResult>(
            this IRepository <TEntity> repository,
            Func <IRepository <TEntity>, Task <TResult> > command,
            ServiceResponseWrapper wrapper)
            where TEntity : IEntity
        {
            var data            = default(TResult);
            var serviceResponse = wrapper.ServiceResponse;

            try
            {
                data = await command(repository);
            }
            catch (Exception ex)
            {
                serviceResponse.Result  = ServiceResult.Error;
                serviceResponse.Message = "Unlnown error while command repository";
                wrapper.Exception       = ex;
                return(data);
            }

            if (data == null)
            {
                serviceResponse.Result  = ServiceResult.BadOrMissingData;
                serviceResponse.Message = "Failed to command repository";
                return(data);
            }
            serviceResponse.PayloadObject = data;

            return(data);
        }
        public static async Task <TResult> Query <TEntity, TResult>(
            this IRepository <TEntity> repository,
            Func <IRepository <TEntity>, Task <TResult> > command,
            ServiceResponseWrapper wrapper)
            where TEntity : IEntity
        {
            var data            = default(TResult);
            var serviceResponse = wrapper.ServiceResponse;

            try
            {
                data = await command(repository);
            }
            catch (Exception ex)
            {
                wrapper.Exception       = ex;
                serviceResponse.Result  = ServiceResult.Error;
                serviceResponse.Message = "Unlnown error command repository:\n\t" + ex.Message;
                return(data);
            }

            if (data == null && !typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
            {
                serviceResponse.Result  = ServiceResult.NotFound;
                serviceResponse.Message = "Item not found in repository";
            }

            serviceResponse.PayloadObject = data;
            return(data);
        }
Ejemplo n.º 3
0
        public virtual async Task <ServiceResponse <TEntity> > Delete(string id)
        {
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Start delete flow for id: {id}");
            var serviceResponse = new ServiceResponse <TEntity>();

            if (!await Validator.ValidateForDelete(id, serviceResponse))
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.Validation, "Entity did not pass validation"));
            }

            Logger.LogDebug(LoggingEvents.Repository, "Repository - Fetch entity");
            var wrapper = new ServiceResponseWrapper(serviceResponse);

            var dbEntry = await Repository.Query(r => r.GetById(id), wrapper);

            if (dbEntry == null)
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.BusinessLogicFlow, "Entity not exists"));
            }

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Delete, id))
            {
                return(serviceResponse);
            }

            if (CurrentEntityConfigRecord.Metadata.IsSoftDeleted && (dbEntry as ISoftDelete).Deleted)
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.BusinessLogicFlow, "Entity already deleted"));
            }

            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, "Prepare for deletion");
            await ModelPreparar.PrepareForDelete(dbEntry);

            TEntity deletedModel;

            if (CurrentEntityConfigRecord.Metadata.IsSoftDeleted)
            {
                (dbEntry as ISoftDelete).Deleted = true;
                Logger.LogDebug(LoggingEvents.Repository, $"Repository - soft deletion (update) {nameof(ISoftDelete)} entity");
                deletedModel = await Repository.Command(r => r.Update(dbEntry), wrapper);
            }
            else
            {
                Logger.LogDebug(LoggingEvents.Repository, "Repository - delete entity with id " + id);
                deletedModel = await Repository.Command(r => r.Delete(dbEntry), wrapper);
            }

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Delete, id))
            {
                return(serviceResponse);
            }

            Publish(CurrentEntityConfigRecord.EventKeys.Delete, serviceResponse.Payload);
            serviceResponse.Result = ServiceResult.Ok;
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}");
            return(serviceResponse);
        }
Ejemplo n.º 4
0
        public virtual async Task <ServiceResponse <TEntity> > Update(string id, TEntity entity)
        {
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Start update flow for id: {id}, entity: {entity}");
            entity.Id = id;
            var serviceResponse = new ServiceResponse <TEntity>();

            if (!await Validator.ValidateForUpdate(entity, serviceResponse))
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.Validation, "Entity did not pass validation"));
            }

            Logger.LogDebug(LoggingEvents.Repository, "Repository - Fetch entity");
            var wrapper = new ServiceResponseWrapper(serviceResponse);

            var dbEntry = await Repository.Query(async r => await r.GetById(id), wrapper);

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Update, id))
            {
                return(serviceResponse);
            }

            if (CurrentEntityConfigRecord.Metadata.IsSoftDeleted && (dbEntry as ISoftDelete).Deleted)
            {
                Logger.LogDebug(LoggingEvents.Audity, "entity already deleted");
                serviceResponse.Result = ServiceResult.BadOrMissingData;
                return(serviceResponse);
            }
            await ModelPreparar.PrepareForUpdate(dbEntry, entity);

            Logger.LogDebug(LoggingEvents.Repository, $"Update entity in repository");
            var updateResponse = await Repository.Command(r => r.Update(entity), wrapper);

            Logger.LogDebug(LoggingEvents.Repository, $"Repository update response: {updateResponse}");
            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Update, entity))
            {
                return(serviceResponse);
            }

            //if (entity is IFileContainer)
            //{
            //    var fileContainer = (entity as IFileContainer);
            //    await FileStorageManager.Delete((dbEntry as IFileContainer).Files);
            //    (updateResponse as IFileContainer).Files = fileContainer.Files;
            //    Logger.LogDebug(LoggingEvents.BusinessLogicFlow, "Start file uploads");
            //    await UploadFiles(fileContainer, serviceResponse);
            //}
            Publish(CurrentEntityConfigRecord.EventKeys.Update, new EntityUpdatedDomainEvent(dbEntry, entity));
            serviceResponse.Result = ServiceResult.Ok;
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}");
            return(serviceResponse);
        }
Ejemplo n.º 5
0
        public virtual async Task <ServiceResponse <Pagination <TEntity> > > GetAll(Pagination <TEntity> pagination)
        {
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, "Start get all flow");
            var serviceResponse = new ServiceResponse <Pagination <TEntity> > {
                Payload = pagination
            };

            if (!await Validator.ValidateForGet(pagination, serviceResponse))
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.Validation, "Request did not pass validation"));
            }

            if ((pagination = await NormalizePagination(pagination)) == null)
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.BusinessLogicFlow, "Missing query data"));
            }

            Logger.LogDebug(LoggingEvents.Repository, "Get all from repository using paginate = " + pagination);
            var wrapper = new ServiceResponseWrapper(new ServiceResponse <IEnumerable <TEntity> >());
            var data    = await Repository.Query(r => r.GetAll(pagination), wrapper);

            Logger.LogDebug(LoggingEvents.Repository, $"Repository response: {data}");

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Read, pagination))
            {
                var wSrvRes = wrapper.ServiceResponse;
                serviceResponse.Message = wSrvRes.Message;
                serviceResponse.TraceId = wSrvRes.TraceId;
                serviceResponse.Result  = wSrvRes.Result;
                return(serviceResponse);
            }

            pagination.Data = data ?? new TEntity[] { };
            if (serviceResponse.Result == ServiceResult.NotSet)
            {
                serviceResponse.Payload = pagination;
                serviceResponse.Result  = ServiceResult.Ok;
                Expression <Func <TEntity, bool> > exp = x => pagination.QueryFunc(x);
                pagination.QueryOrFilter = exp.ToString();

                Publish(CurrentEntityConfigRecord.EventKeys.Read, pagination);
            }
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}");
            return(serviceResponse);
        }
Ejemplo n.º 6
0
        public virtual async Task <ServiceResponse <TEntity> > Create(TEntity entity)
        {
            Logger.LogInformation(LoggingEvents.BusinessLogicFlow, $"Start create flow for entity: {entity}");

            var serviceResponse = new ServiceResponse <TEntity>();

            if (!await Validator.ValidateForCreate(entity, serviceResponse))
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.Validation, "Entity did not pass validation"));
            }

            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Prepare entity to be inserted to database");
            await ModelPreparar.PrepareForCreate(entity);

            Logger.LogDebug(LoggingEvents.Repository, $"Insert entity to repository");
            if (CurrentEntityConfigRecord.Metadata.IsSoftDeleted)
            {
                (entity as ISoftDelete).Deleted = false;
            }

            var wrapper = new ServiceResponseWrapper(serviceResponse);
            var dbData  = await Repository.Command(r => r.Insert(entity), wrapper);

            Logger.LogDebug(LoggingEvents.Repository, $"Repository insert response: {dbData}");

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Create, entity))
            {
                return(serviceResponse);
            }

            Publish(CurrentEntityConfigRecord.EventKeys.Create, serviceResponse.Payload);
            serviceResponse.Result = ServiceResult.Ok;

            //if (entity is IFileContainer)
            //{
            //    Logger.LogDebug(LoggingEvents.BusinessLogicFlow, "Start file uploads");
            //    await UploadFiles(dbData as IFileContainer, serviceResponse);
            //}
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}");
            return(serviceResponse);
        }
Ejemplo n.º 7
0
        private bool IsNotFoundOrBadOrMissingDataOrError(ServiceResponseWrapper wrapper, string eventKey, object requestData)
        {
            var serviceResponse = wrapper.ServiceResponse;

            Logger.LogDebug(LoggingEvents.Repository, $"Has {serviceResponse.Result} response");
            if (serviceResponse.Result == ServiceResult.BadOrMissingData || serviceResponse.Result == ServiceResult.NotFound)
            {
                return(true);
            }
            if (wrapper.Exception != null || serviceResponse.Result == ServiceResult.Error)
            {
                Logger.LogDebug(LoggingEvents.Repository, "Repository response is null");
                Logger.LogDebug(LoggingEvents.Repository, $"Repository returned with exception. {nameof(ServiceResponse.TraceId)}: {serviceResponse.TraceId}");
                serviceResponse.TraceId = WorkContext.TraceId;

                EventBus.PublishException(eventKey, wrapper.Exception, requestData, WorkContext);

                return(true);
            }
            return(false);
        }
Ejemplo n.º 8
0
        //public virtual async Task UploadFiles(IFileContainer fileContainer, ServiceResponse serviceResponse)
        //{
        //    var files = fileContainer?.Files;
        //    if (files == null || !files.Any())
        //        return;

        //    foreach (var f in files)
        //    {
        //        f.ParentId = fileContainer.Id;
        //        f.ParentKey = WorkContext.CurrentEntityConfigRecord.EntityKey;
        //    }
        //    var uploadResponses = await FileStorageManager.Upload(files);
        //    serviceResponse.Data = new { entity = serviceResponse.Data, filesUploadStatus = uploadResponses };
        //}
        public virtual async Task <ServiceResponse <TEntity> > GetById(string id)
        {
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Start get by id with id = {id}");

            var serviceResponse = new ServiceResponse <TEntity>();

            if (!await Validator.ValidateForGet(id, serviceResponse))
            {
                return(SetServiceResponse(serviceResponse, ServiceResult.BadOrMissingData, LoggingEvents.Validation, "Entity did not pass validation"));
            }

            Logger.LogDebug(LoggingEvents.Repository, "Get by Id from repository");

            var wrapper = new ServiceResponseWrapper(serviceResponse);
            var data    = await Repository.Query(r => r.GetById(id), wrapper);

            Logger.LogDebug(LoggingEvents.Repository, $"Repository response: {data}");

            if (IsNotFoundOrBadOrMissingDataOrError(wrapper, CurrentEntityConfigRecord.EventKeys.Read, id))
            {
                return(serviceResponse);
            }
            if (!CurrentEntityConfigRecord.Metadata.ShowSoftDeleted && CurrentEntityConfigRecord.Metadata.IsSoftDeleted && (data as ISoftDelete).Deleted)
            {
                serviceResponse.Result = ServiceResult.BadOrMissingData;
                return(serviceResponse);
            }

            if (data != null && serviceResponse.Result == ServiceResult.NotSet)
            {
                serviceResponse.Payload = data;
                serviceResponse.Result  = ServiceResult.Ok;

                Publish(CurrentEntityConfigRecord.EventKeys.Read, serviceResponse.Payload);
            }
            Logger.LogDebug(LoggingEvents.BusinessLogicFlow, $"Service Response: {serviceResponse}");
            return(serviceResponse);
        }