/// <summary> /// Gets collection result from data source using default query builder /// </summary> /// <typeparam name="TEntity">Entity model</typeparam> /// <param name="predicate">Custom filter predicate</param> /// <param name="includeProperties">Included into result objects properties</param> public static Task <List <TEntity> > LoadCollectionAsync <TEntity>(this IUnitOfWorkAsync unitOfWorkAsync, Expression <Func <TEntity, bool> > predicate, params Expression <Func <TEntity, object> >[] includeProperties) where TEntity : class { var repository = unitOfWorkAsync.GetRepository <TEntity>(); return(repository.GetCollectionAsync(new QueryBuilder <TEntity>().Build(p => p.Query <TEntity>().Where(predicate), includeProperties))); }
/// <summary> /// Remove entity item from data source using default query builder /// </summary> /// <typeparam name="TEntity">Entity model</typeparam> /// <param name="unitOfWorkAsync"></param> /// <param name="predicate">Custom filter predicate</param> public static async Task RemoveAsync <TEntity>(this IUnitOfWorkAsync unitOfWorkAsync, Expression <Func <TEntity, bool> > predicate) where TEntity : class { var repository = unitOfWorkAsync.GetRepository <TEntity>(); var entity = await unitOfWorkAsync.LoadAsync(predicate); repository.Remove(entity); }
public async Task <ReadResponseMessage <TEntity> > Handle() { var repository = _unitOfWork.GetRepository <TEntity>(); // First get all assets, then filter by those that are in var allData = await repository.GetAll(); var response = ResponseFactory.BuildReadResponse($"{allData.Count()} Records returned", allData); return(response); }
/// <summary> /// Handles getting a list of users that are information asset owners and returns the ones the specified user has access to. /// </summary> /// <param name="userId">accepts an <see cref="int"/> which specifies the id of the user that wants to view the list of Information Asset Owners</param> /// <returns></returns> public async Task <ReadResponseMessage <User> > Handle(string businessAreaName) { var repository = _unitOfWork.GetRepository <User>(); var users = await repository.GetAll(); var results = users.Where(x => x.IsIAO == true && x.BusinessArea.BusinessAreaName == businessAreaName); var response = ResponseFactory.BuildReadResponse($"{results.Count()} records returned", results); return(response); }
public async Task <SaveInformationAssetResponse> Handle(IInformationAsset message) { // Build the Domain.Asset model based on the data supplied var newModel = BuildAssetModel(message); // Validate the new Asset conforms to the Domain rules var validationResult = newModel.Validate(); // Prep response var response = new SaveInformationAssetResponse { ValidationResult = validationResult }; // If the model is valid then attempt to save it if (validationResult.IsValid) { try { var repo = _unitOfWork.GetRepository <Asset>(); if (message.AssetId > 0) { await repo.Edit(newModel); } else { await repo.Create(newModel); } await _unitOfWork.Save(); response.Asset = newModel; } catch (Exception ex) { // Failed to save the asset so copy error to response so clients can see what has gone wrong. response.InnerException = ex; response.ResponseType = ResponseTypes.Failure; response.OutputMessage = "Failed to save the asset. See exception for details."; } } return(response); }
public async Task <ReadResponseMessage <Asset> > Handle(int userId) { try { var userRepo = _unitOfWork.GetRepository <User>(); var user = await userRepo.Get(userId); var repository = _unitOfWork.GetRepository <Asset>(); // First get all assets, then filter by those that are in var allAssets = await repository.GetAll(); var myAssets = allAssets.Where(x => user.LinkedUsers.Any(u => u.LinkedUser.UserId == x.InformationAssetOwner.UserId)); var response = ResponseFactory.BuildReadResponse($"{myAssets.Count()} Information Assets Returned", myAssets); return(response); } catch (Exception ex) { return(new ReadResponseMessage <Asset>() { InnerException = ex, OutputMessage = "Failed to get a list of Information Assets", ResponseType = ResponseTypes.Error }); } }
/// <summary> /// Handles getting a list of users that are information asset owners and returns the ones the specified user has access to. /// </summary> /// <param name="userId">accepts an <see cref="int"/> which specifies the id of the user that wants to view the list of Information Asset Owners</param> /// <returns></returns> public async Task <ReadResponseMessage <User> > Handle(int userId) { var repository = _unitOfWork.GetRepository <User>(); var user = await repository.Get(userId); var iaoUsers = new List <User>(); foreach (var item in user.LinkedUsers) { if (item.LinkedUser.IsIAO) { iaoUsers.Add(item.LinkedUser); } } var response = ResponseFactory.BuildReadResponse($"{user.LinkedUsers.Count()} records returned", iaoUsers); return(response); }
/// <summary> /// Remove entity item from data source /// </summary> /// <typeparam name="TEntity">Entity model</typeparam> /// <param name="unitOfWorkAsync"></param> /// <param name="entity"></param> public static void Remove <TEntity>(this IUnitOfWorkAsync unitOfWorkAsync, TEntity entity) where TEntity : class { var repository = unitOfWorkAsync.GetRepository <TEntity>(); repository.Remove(entity); }
/// <summary> /// Remove entity items from data source using default query builder /// </summary> /// <typeparam name="TEntity">Entity model</typeparam> /// <param name="unitOfWorkAsync"></param> /// <param name="entities">List of entities to remove</param> public static void Remove <TEntity>(this IUnitOfWorkAsync unitOfWorkAsync, IEnumerable <TEntity> entities) where TEntity : class { var repository = unitOfWorkAsync.GetRepository <TEntity>(); repository.Remove(entities); }
/// <summary> /// Loads single result found by key from previous requests /// </summary> /// <typeparam name="TEntity">Entity model</typeparam> /// <typeparam name="TKey">Entity key type</typeparam> /// <param name="unitOfWorkAsync"></param> /// <param name="key">Entity key</param> public static Task <TEntity> LoadAsync <TEntity, TKey>(this IUnitOfWorkAsync unitOfWorkAsync, TKey key) where TEntity : class { var repository = unitOfWorkAsync.GetRepository <TEntity>(); return(repository.GetAsync(key)); }
public virtual IRepository <T> GetRepository <T>() where T : class { return(_unitOfWork.GetRepository <T>()); }