public async Task<BlResult<IEnumerable<NodeModel>>> GetByGraphsAsync(int graphId) { BlResult<IEnumerable<NodeModel>> result = new BlResult<IEnumerable<NodeModel>>(); try { if (graphId == 0) { throw new ArgumentException(nameof(graphId)); } //EnsureTransaction(); var entity = await _nodeRepository.GetNodesByGraphIdAsync(graphId); IEnumerable<NodeModel> nodeModels = _mapper.Map<IEnumerable<NodeModel>>(entity); result.Success(nodeModels); } catch (ArgumentException) { result.Fail(BLErrorCodeTypeEnum.CantFetchUncreatedEntity); } catch (Exception ex) { result.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex); } return result; }
public async Task <BlResult> DeleteAsync(int id) { BlResult blResult = new BlResult(); try { var entity = await _connectionTypeRepository.GetByIdWithReferencAsync(id); if (entity != null) { EnsureTransaction(); _connectionTypeRepository.Delete(id); await SaveChangesAsync(); blResult.Success(); } else { throw new ArgumentException(); } } catch (ArgumentException) { blResult.Fail(BLErrorCodeTypeEnum.NoSuchEntityExistsWithID); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return(blResult); }
public async Task <BlResult <IEnumerable <UserModel> > > GetUsers() { var result = new BlResult <IEnumerable <UserModel> >(); var entities = await _usersRepository.GetAllAsync(); var models = _mapper.Map <IEnumerable <UserModel> >(entities); result.Success(models); return(result); }
public async Task <BlResult <UserModel> > GetUser(int id) { var result = new BlResult <UserModel>(); var entities = await _usersRepository.GetByIdAsync(id); var models = _mapper.Map <UserModel>(entities); result.Success(models); return(result); }
public async Task <BlResult <bool> > UserExists(string email) { var result = new BlResult <bool>(); if (string.IsNullOrWhiteSpace(email)) { throw new ArgumentNullException(nameof(email)); } var exists = await _usersRepository.AnyAsync(x => x.Email == email.ToLower(Thread.CurrentThread.CurrentCulture)); result.Success(exists); return(result); }
public async Task<BlResult<Dictionary<int, string>>> GetNodesIdAndNameAsync() { BlResult<Dictionary<int, string>> result = new BlResult<Dictionary<int, string>>(); try { var entity = await _nodeRepository.GetNodesIdAndNameAsync(); result.Success(entity); } catch (Exception ex) { result.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex); } return result; }
public async Task<BlResult<NodeModel>> GetByIdAsync(int id) { BlResult<NodeModel> blResult = new BlResult<NodeModel>(); try { var entity = await _nodeRepository.GetByIdWithReferencAsync(id); blResult.Success(_mapper.Map<NodeModel>(entity)); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return blResult; }
public async Task <BlResult <IEnumerable <ConnectionTypeModel> > > GetAllAsync() { BlResult <IEnumerable <ConnectionTypeModel> > blResult = new BlResult <IEnumerable <ConnectionTypeModel> >(); try { var entity = await _connectionTypeRepository.GetAllWithReferencsAsync(); blResult.Success(_mapper.Map <IEnumerable <ConnectionTypeModel> >(entity)); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return(blResult); }
public async Task <BlResult <ConnectionTypeModel> > GetByIdAsync(int id) { BlResult <ConnectionTypeModel> blResult = new BlResult <ConnectionTypeModel>(); try { var entity = await _connectionTypeRepository.GetByIdAsync(id); blResult.Success(_mapper.Map <ConnectionTypeModel>(entity)); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return(blResult); }
public async Task <BlResult <AttributeDescriptionModel> > SaveAsync(AttributeDescriptionModel attributeDescriptionModel) { BlResult <AttributeDescriptionModel> blResult = new BlResult <AttributeDescriptionModel>(); try { if (attributeDescriptionModel is null) { throw new ArgumentNullException(); } if (attributeDescriptionModel.Id == 0 && await _attribuetDescriptionRepository.ExistsEntityWithSuchAName(attributeDescriptionModel.Name)) { throw new Exception("Duplicated Attribute Name"); } EnsureTransaction(); var fetchedEntity = await _attribuetDescriptionRepository.GetByIdAsync(attributeDescriptionModel.Id); fetchedEntity = _mapper.Map(attributeDescriptionModel, fetchedEntity); if (attributeDescriptionModel.Id > 0) { _attribuetDescriptionRepository.Edit(fetchedEntity); } else { fetchedEntity = _attribuetDescriptionRepository.Add(fetchedEntity); } await SaveChangesAsync(); blResult.Success(_mapper.Map <AttributeDescriptionModel>(fetchedEntity)); } catch (ArgumentNullException) { blResult.Fail(ConstDictionary.CantSaveNullModel); } catch (Exception ex) { blResult.Fail(ex); } return(blResult); }
public async Task <BlResult <IEnumerable <AttributeDescriptionModel> > > GetAllAsync() { BlResult <IEnumerable <AttributeDescriptionModel> > blResult = new BlResult <IEnumerable <AttributeDescriptionModel> >(); try { var entity = await _attribuetDescriptionRepository.GetAllAsync(); blResult.Success(_mapper.Map <IEnumerable <AttributeDescriptionModel> >(entity)); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return(blResult); }
public async Task <BlResult <IEnumerable <SimpleModel> > > GetAllByNameAndIdAsync() { BlResult <IEnumerable <SimpleModel> > blResult = new BlResult <IEnumerable <SimpleModel> >(); try { var entity = await _connectionRepository.GetConnectionsByNameAndIdAsync(); var result = entity.ToList().Select(x => new SimpleModel(x.Key, x.Value)).ToList(); blResult.Success(result); } catch (Exception ex) { blResult.Fail(ex); } return(blResult); }
public async Task <BlResult <ConnectionModel> > SaveAsync(ConnectionModel connectionModel) { BlResult <ConnectionModel> blResult = new BlResult <ConnectionModel>(); try { if (connectionModel is null) { throw new ArgumentNullException(nameof(connectionModel)); } EnsureTransaction(); var fetchedEntity = await _connectionRepository.GetByIdWithReferencAsync(connectionModel.Id); fetchedEntity = _mapper.Map(connectionModel, fetchedEntity); fetchedEntity.OverwritesId = null; if (connectionModel.Id > 0) { _connectionRepository.Edit(fetchedEntity); } else { fetchedEntity = _connectionRepository.Add(fetchedEntity); } await SaveChangesAsync(); blResult.Success(_mapper.Map <ConnectionModel>(fetchedEntity)); } catch (ArgumentNullException) { blResult.Fail(BLErrorCodeTypeEnum.ArgumentIsNull); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex); } return(blResult); }
public async Task <BlResult <UserModel> > RegisterUser(RegisterModel registerModel) { byte[] passwordHash, PasswordSalt; var result = new BlResult <UserModel>(); if (registerModel is null) { throw new ArgumentNullException(nameof(registerModel)); } var emailExists = await UserExists(registerModel.Email); if (emailExists.Value) { string message = string.Format("Sorry, Email '{0}' is already in use.", registerModel.Email); throw new UniqueConstraintException(message); } DateTime creationDate = DateTime.Now; passwordHash = _usersRepository.CreatePasswordHash(creationDate, registerModel.Password, out PasswordSalt); var entity = _mapper.Map <UserEntity>(registerModel); entity.UniqueID = Guid.NewGuid(); entity.PasswordHash = passwordHash; entity.PasswordSalt = PasswordSalt; entity.Email = entity.Email.ToLower(Thread.CurrentThread.CurrentCulture); entity.CreationDate = creationDate; _usersRepository.Add(entity); await _usersRepository.SaveChangesAsync(); Array.Clear(entity.PasswordHash, 0, entity.PasswordHash.Length); Array.Clear(entity.PasswordSalt, 0, entity.PasswordSalt.Length); result.Success(_mapper.Map <UserModel>(entity)); return(result); }
public async Task<BlResult> DeleteAsync(int id) { BlResult blResult = new BlResult(); try { var entity = await _nodeRepository.GetByIdWithReferencAsync(id); if (entity != null) { if (entity.ConnectionsFrom.Count + entity.ConnectionsTo.Count > 0) { blResult.Fail(ConstDictionary.CantDeleteNodeWithConnections); } else { EnsureTransaction(); _nodeRepository.Delete(id); await SaveChangesAsync(); blResult.Success(); } } else { throw new ArgumentException(); } } catch (ArgumentException) { blResult.Fail(BLErrorCodeTypeEnum.NoSuchEntityExistsWithID); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex); } return blResult; }
public async Task <BlResult <UserModel> > Login(LoginModel loginModel) { var result = new BlResult <UserModel>(); if (loginModel is null) { throw new ArgumentNullException(nameof(loginModel)); } var entity = await _usersRepository.FirstOrDefaultAsync(x => x.Email == loginModel.Email.ToLower(Thread.CurrentThread.CurrentCulture)); if (entity is null) { throw new InvalidEmailOrPasswordException(); } _usersRepository.VerifyPasswordHash(loginModel.Password, entity.PasswordHash, entity.PasswordSalt, entity.CreationDate); entity = await _usersRepository.GetByIdAsync(entity.Id); result.Success(_mapper.Map <UserModel>(entity)); return(result); }
public async Task <BlResult <GraphModel> > SaveAsync(GraphModel graphModel) { BlResult <GraphModel> blResult = new BlResult <GraphModel>(); try { if (graphModel is null) { throw new ArgumentException(); } EnsureTransaction(); var fetchedEntity = await _graphRepository.GetByIdWithReferencAsync(graphModel.Id); fetchedEntity = _mapper.Map(graphModel, fetchedEntity); if (graphModel.Id > 0) { _graphRepository.Edit(fetchedEntity); } else { fetchedEntity = _graphRepository.Add(fetchedEntity); await SaveChangesAsync(); NodeModel nodeModel = new NodeModel() { Name = fetchedEntity.Name, GraphId = fetchedEntity.Id, Data = "Entery node for graph " + fetchedEntity.Name, NodeAttributes = new System.Collections.ObjectModel.ObservableCollection <AttributeDescriptionModel>() { new AttributeDescriptionModel() { Id = (int)AttributesTypesEnum.EnteryNode } } }; if (fetchedEntity.IsAbstract) { nodeModel.NodeAttributes.Add(new AttributeDescriptionModel() { Id = (int)AttributesTypesEnum.Abstract }); } var result = await _nodeService.SaveAsync(nodeModel); if (result.Succeed) { EnsureTransaction(); fetchedEntity.EnteryNodeId = result.Value.Id; _graphRepository.Edit(fetchedEntity); } else { RollbackTransaction(); throw result.Exception; } } await SaveChangesAsync(); blResult.Success(_mapper.Map <GraphModel>(fetchedEntity)); } catch (ArgumentException) { blResult.Fail(ConstDictionary.CantSaveNullModel); } catch (Exception ex) { blResult.Fail(ex); } return(blResult); }
public async Task <BlResult <ConnectionTypeModel> > SaveAsync(ConnectionTypeModel connectionTypeModel) { BlResult <ConnectionTypeModel> blResult = new BlResult <ConnectionTypeModel>(); try { if (connectionTypeModel is null) { throw new ArgumentException(); } EnsureTransaction(); var fetchedEntity = await _connectionTypeRepository.GetByIdAsync(connectionTypeModel.Id); fetchedEntity = _mapper.Map(connectionTypeModel, fetchedEntity); var attributesToDelete = fetchedEntity.ConnectionTypeAttributes. Where(x => !connectionTypeModel.ConnectionTypeAttributes.Any(y => y.Id == x.Id)).ToList(); var attributesToAdd = connectionTypeModel.ConnectionTypeAttributes. Where(x => !fetchedEntity.ConnectionTypeAttributes.Any(y => y.Id == x.Id)).Select(z => z.Id); if (connectionTypeModel.Id > 0) { _connectionTypeRepository.Edit(fetchedEntity); } else { fetchedEntity = _connectionTypeRepository.Add(fetchedEntity); } await SaveChangesAsync(false); if (attributesToDelete != null && attributesToDelete.Count() > 0) { foreach (var item in attributesToDelete) { fetchedEntity.ConnectionTypeAttributes.Remove(item); } await SaveChangesAsync(false); } if (attributesToAdd != null && attributesToAdd.Count() > 0) { await _connectionTypeRepository.AddAttribuets(fetchedEntity, attributesToAdd); await SaveChangesAsync(false); } await SaveChangesAsync(); blResult.Success(_mapper.Map <ConnectionTypeModel>(fetchedEntity)); } catch (ArgumentException) { blResult.Fail(ConstDictionary.CantSaveNullModel); } catch (Exception ex) { blResult.Fail(ex); } return(blResult); }
public async Task<BlResult<NodeModel>> SaveAsync(NodeModel nodeModel) { BlResult<NodeModel> blResult = new BlResult<NodeModel>(); try { if (nodeModel is null) { throw new ArgumentNullException(nameof(NodeModel)); } EnsureTransaction(); var fetchedEntity = await _nodeRepository.GetByIdAsync(nodeModel.Id); fetchedEntity = _mapper.Map(nodeModel, fetchedEntity); var attributesToDelete = fetchedEntity.NodeAttributes. Where(x => !nodeModel.NodeAttributes.Any(y => y.Id == x.Id)).ToList(); var attributesToAdd = nodeModel.NodeAttributes. Where(x => !fetchedEntity.NodeAttributes.Any(y => y.Id == x.Id)).Select(z => z.Id); if (nodeModel.Id > 0) { _nodeRepository.Edit(fetchedEntity); } else { fetchedEntity = _nodeRepository.Add(fetchedEntity); } await SaveChangesAsync(false); if (attributesToDelete != null && attributesToDelete.Count() > 0) { foreach (var item in attributesToDelete) { fetchedEntity.NodeAttributes.Remove(item); } await SaveChangesAsync(false); } if (attributesToAdd != null && attributesToAdd.Count() > 0) { await _nodeRepository.AddAttribuets(fetchedEntity, attributesToAdd); await SaveChangesAsync(false); } await SaveChangesAsync(); blResult.Success(_mapper.Map<NodeModel>(fetchedEntity)); } catch (ArgumentNullException) { blResult.Fail(BLErrorCodeTypeEnum.ArgumentIsNull); } catch (Exception ex) { blResult.Fail(BLErrorCodeTypeEnum.Unknown, ex.Message, ex); } return blResult; }