Exemplo n.º 1
0
        CrudResult <T> Forbidden <T>(Exception ex)
        {
            var result = CrudResult <T> .Create(ex);

            result.Status = CrudResultStatus.Forbidden;
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper mehtod to create failed CrudResult
        /// </summary>
        /// <typeparam name="T">Data type for the CrudResult</typeparam>
        /// <param name="ex">Exception that caused the failed result</param>
        /// <returns></returns>
        CrudResult <T> Fail <T>(Exception ex)
        {
            var result = CrudResult <T> .Create(ex);

            result.Status = CrudResultStatus.Error;
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Delete an entity
        /// </summary>
        /// <param name="keys">Key(s) of entity to delete</param>
        /// <returns></returns>
        public virtual async Task <CrudResult> Delete(params object[] keys)
        {
            try
            {
                var set    = Context.Set <TEntity>();
                var entity = await DeleteQuery(keys);

                var result = CrudResult.Create();

                await DeleteLogic(result, entity);

                if (result.IsValid)
                {
                    if (entity is ISoftDelete)
                    {
                        var softDelete = entity as ISoftDelete;
                        softDelete.IsDeleted = true;
                    }
                    else
                    {
                        set.Remove(entity);
                    }
                    await Context.SaveChangesAsync(UserContext.UserName, default);
                }
                else
                {
                    result.Status = CrudResultStatus.Invalid;
                }

                return(result);
            }
            catch (UnauthorizedAccessException ex)
            {
                Logger.LogError(ex, $"Unauthorized access deleting {nameof(TEntity)}");
                return(Forbidden <TData>(ex));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Could not delete {nameof(TEntity)}: {ToCommmaDelimitedList(keys)}");
                return(CrudResult.Create(ex));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Update an entity and prisist it to the db
        /// </summary>
        /// <param name="data">Data to map to the updating entity</param>
        /// <param name="map">Flag to allow configure a full mapping of data from the data object to the entity</param>
        /// <param name="keys">Key(s) of the entity to update</param>
        /// <returns></returns>
        public virtual async Task <CrudResult <TData> > Update(TData data, bool map, params object[] keys)
        {
            try
            {
                var result = CrudResult <TData> .Create();

                var set = Context.Set <TEntity>();

                var entity = await UpdateQuery(data, keys);

                UpdateMap(data, entity, map);

                await UpdateLogic(data, entity, result);

                if (result.IsValid)
                {
                    await Context.SaveChangesAsync(UserContext.UserName, default);

                    result.Data = Mapper.Map <TData>(entity);

                    await GetByKeyLogic(result.Data, entity);
                }
                else
                {
                    result.Status = CrudResultStatus.Invalid;
                }

                return(result);
            }
            catch (UnauthorizedAccessException ex)
            {
                Logger.LogError(ex, $"Unauthorized access updating {nameof(TEntity)}");
                return(Forbidden <TData>(ex));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Error Creating {nameof(TEntity)}");
                return(Fail <TData>(ex));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create new entity by passing data object to map to new entity
        /// </summary>
        /// <param name="data">Data to be used to create new entity</param>
        /// <returns>Data object that represents the created entity</returns>
        public virtual async Task <CrudResult <TData> > Create(TData data)
        {
            try
            {
                var result = CrudResult <TData> .Create();

                var entity = CreateMap(data);

                await CreateLogic(data, entity, result);

                if (result.ModelState.IsValid)
                {
                    var set = Context.Set <TEntity>();
                    set.Add(entity);
                    await Context.SaveChangesAsync(UserContext.UserName, default);

                    result.Data = Mapper.Map <TData>(entity);

                    await GetByKeyLogic(result.Data, entity);
                }
                else
                {
                    result.Status = CrudResultStatus.Invalid;
                }

                return(result);
            }
            catch (UnauthorizedAccessException ex)
            {
                Logger.LogError(ex, $"Unauthorized access creating {nameof(TEntity)}");
                return(Forbidden <TData>(ex));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Error Creating {nameof(TEntity)}");
                return(Fail <TData>(ex));
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Helper method to create Successful CrudResult
 /// </summary>
 /// <typeparam name="T">Data type for the CrudResult</typeparam>
 /// <param name="data">Data to be included with result</param>
 /// <returns></returns>
 CrudResult <T> Success <T>(T data)
 {
     return(CrudResult <T> .Create(data));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Execute any logic that needs to happen when an entity is deteled such as deleting relationships and validating entity can be deleted.
 /// </summary>
 /// <param name="entity">Entity to be deleted</param>
 protected virtual async Task DeleteLogic(CrudResult result, TEntity entity)
 {
     await Task.FromResult <object>(null);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Execute any logic when updating an entity such as validation
 /// </summary>
 /// <param name="data">Data object used to update entity</param>
 /// <param name="entity">Entity being updated</param>
 /// <param name="result">Result object that warnings and exceptions can be added to</param>
 protected virtual async Task UpdateLogic(TData data, TEntity entity, CrudResult <TData> result)
 {
     await Task.FromResult <object>(null);
 }