Пример #1
0
        public static async Task <BaseResult <object> > ServiceDeleteEntityHelper <TEntity>(
            object id, IUnityOfWork_EF uow) where TEntity : AuditableEntityBase
        {
            var response = new BaseResult <object>();

            // Obtener la entidad desde la BD para eliminarla (lógicamente)
            var entityFromDb = await uow.GetRepository <TEntity>().GetById(id);

            // Si la entidad ya está eliminada o no existe, no hacer nada y devolver null
            if (entityFromDb == null || entityFromDb.IsDeleted)
            {
                response.Data = null;
                return(response);
            }

            // Actualizar propiedades de la entidad
            AppHelpers.SetFieldsForEntityDeletion(entityFromDb);

            // Guardar la entidad eliminada lógicamente
            await uow.GetRepository <TEntity>().Update(entityFromDb);

            await uow.SaveAsync();

            // Solo devolver el ID de la entidad eliminada lógicamente
            response.Data = id;

            return(response);
        }
Пример #2
0
 public BaseService(IOptionsSnapshot <MyAppConfig> myAppConfig, ILogger logger, IUnityOfWork_EF uow)
 {
     this._myAppConfig = myAppConfig;
     this._logger      = logger;
     this._uow         = uow;
     this._connection  = _uow.GetConnection;
     _logger.LogDebug($"*** SERVICE Constructor of {this.GetType()} at {DateTime.Now}");
 }
Пример #3
0
        public static async Task <bool> ServiceEntityExists <TEntity>(object id, IUnityOfWork_EF uow)
            where TEntity : AuditableEntityBase
        {
            // Try to get the entity from DB
            var entityFromDb = await uow.GetRepository <TEntity>().GetById(id);

            if (entityFromDb == null)
            {
                return(false);
            }
            return(true);
        }
Пример #4
0
        public static async Task <BaseResult <TDto> > ServiceCreateEntityHelper <TEntity, TDto, TIdPropertyType>(
            TEntity newEntity, string propertyName, IUnityOfWork_EF uow,
            string GetById_Command) where TEntity : AuditableEntityBase
        {
            var response = new BaseResult <TDto>();

            // Actualizar propiedades de la entidad
            AppHelpers.SetFieldsForEntityUpdate(newEntity);

            // Guardar la nueva entidad
            await uow.GetRepository <TEntity>().Add(newEntity);

            await uow.SaveAsync();

            // Devolver la entidad recien guardada desde la BD
            response.Data = await uow.GetConnection.QuerySingleOrDefaultAsync <TDto>(GetById_Command,
                                                                                     new { id1 = AppHelpers.GetPropertyValue(newEntity, propertyName) });

            return(response);
        }
Пример #5
0
        public static async Task <BaseResult <TDto> > ServiceUpdateEntityHelper <TEntity, TDto, TIdPropertyType>(
            TEntity updatedEntity, TIdPropertyType id, string propertyName, IUnityOfWork_EF uow,
            string GetById_Command) where TEntity : AuditableEntityBase
        {
            var response = new BaseResult <TDto>();

            // Detectar inconsistencia entre el Id y la entidad (deben ser iguales en valores)
            if (!((TIdPropertyType)id).Equals((TIdPropertyType)AppHelpers.GetPropertyValue(updatedEntity, propertyName)))
            {
                throw new Exception(AppMessages.UPDATE_ID_ERROR);
            }

            // Obtener la entidad desde la BD para comparar con la recibida
            var entityFromDb = await uow.GetRepository <TEntity>().GetById(id);

            // Si la entidad ya está eliminada o no existe, no hacer nada y devolver null
            if (entityFromDb == null || entityFromDb.IsDeleted)
            {
                response.Data = default(TDto);
                return(response);
            }

            // Comparar con la entidad de la BD y detectar un cambio no permitido
            if (AppHelpers.NotAllowedChanges(entityFromDb, updatedEntity))
            {
                throw new Exception(AppMessages.UPDATE_FORBIDDEN_FIELDS);
            }

            // Actualizar propiedades de la entidad
            AppHelpers.SetFieldsForEntityUpdate(updatedEntity);

            // Guardar la entidad actualizada
            await uow.GetRepository <TEntity>().Update(updatedEntity);

            await uow.SaveAsync();

            // Devolver la entidad recien guardada desde la BD
            response.Data = await uow.GetConnection.QuerySingleOrDefaultAsync <TDto>(GetById_Command, new { id1 = id });

            return(response);
        }
Пример #6
0
        public static async Task <bool> ServiceUpdateEntityCheckHelper <TEntity, TDto, TIdPropertyType>(TEntity updatedEntity,
                                                                                                        BaseResult <TDto> response, object id, string propertyName, IUnityOfWork_EF uow) where TEntity : AuditableEntityBase
        {
            // Detectar inconsistencia entre el Id y la entidad (deben ser iguales en valores)
            if (!((TIdPropertyType)id).Equals((TIdPropertyType)AppHelpers.GetPropertyValue(updatedEntity, propertyName)))
            {
                throw new Exception(AppMessages.UPDATE_ID_ERROR);
            }

            // Obtener la entidad desde la BD para comparar con la recibida
            var entityFromDb = await uow.GetRepository <TEntity>().GetById(id);

            // Si la entidad ya está eliminada o no existe, no hacer nada y devolver null
            if (entityFromDb == null || entityFromDb.IsDeleted)
            {
                response.Data = default(TDto);
                return(false);
            }

            // Comparar con la entidad de la BD y detectar un cambio no permitido
            if (AppHelpers.NotAllowedChanges(entityFromDb, updatedEntity))
            {
                throw new Exception(AppMessages.UPDATE_FORBIDDEN_FIELDS);
            }

            // Actualizar propiedades de la entidad
            AppHelpers.SetFieldsForEntityUpdate(updatedEntity);

            return(true);
        }
 public MyNoteService(IOptionsSnapshot <MyAppConfig> myAppConfig, ILogger <MyNoteService> logger, IUnityOfWork_EF uow)
     : base(myAppConfig, logger, uow)
 {
 }