public List <Node> FindNodesByValue(string searchTerm, bool caseSensitive = false) { List <string> searchTermList = searchTerm.Split().ToList(); List <NODE> dbNodesList = new List <NODE>(); using (IdeaStorageEntities context = new IdeaStorageEntities()) { foreach (string term in searchTermList) { dbNodesList.AddRange( caseSensitive ? context.NODES.Where(n => n.Text.Contains(term)) : context.NODES.Where(n => n.Text.ToLower().Contains(term.ToLower()))); } List <NODE> dbResult = (dbNodesList.GroupBy(n => n, n => n.NodeId) .Select(g => new { g, count = g.Count() }) .OrderByDescending(@t => @t.count) .Select(@t => @t.g.Key)).ToList(); List <Node> result = dbResult.Select(n => n.ToModel()).ToList(); return(result); } }
/// <summary> /// Updates the user password and salt. /// </summary> /// <param name="userId">The user's id.</param> /// <param name="oldPassword">The old password which should be updated.</param> /// <param name="newPassword">The new password for user.</param> /// <exception cref="System.Security.Authentication.AuthenticationException">Invalid email or password.</exception> public void UpdateUserCredentials(int userId, string oldPassword, string newPassword) { Log.DebugFormat("Begin UpdateUserCredentials(userId:'{0}')", userId); string passwordFromDataBase; string oldSalt; this.GetUserCredentials(userId, out passwordFromDataBase, out oldSalt); ICryptographyManager cryptographyManager = new CryptographyManager(); oldPassword = cryptographyManager.HashPassword(oldPassword, oldSalt); if (oldPassword != passwordFromDataBase) { const string Message = "Invalid email or password."; Log.Debug(Message); throw new AuthenticationException(Message); } using (IdeaStorageEntities context = new IdeaStorageEntities()) { USER user = context.USERS.Single(u => u.UserId == userId); user.Salt = cryptographyManager.CreateSalt(12); user.Password = cryptographyManager.HashPassword(newPassword, user.Salt); context.SaveChanges(); } Log.DebugFormat("Credentials updated for user with ID:'{0}'", userId); }
/// <summary> /// Creates the user using model and password string. /// </summary> /// <param name="newUser">Model of the new user.</param> /// <param name="password">The string with password for new user.</param> /// <returns>Id of newly created user.</returns> public int CreateUser(User newUser, string password) { Log.DebugFormat("Begin CreateUser('{0}')", newUser); UserValidator userValidator = new UserValidator(); userValidator.ValidateAndThrow(newUser); if (this.FindUserByEmail(newUser.Email) != null) { string message = string.Format("User with email:'{0}' already exists in data base.", newUser.Email); Log.Debug(message); throw new DuplicateEntityException(message); } using (IdeaStorageEntities context = new IdeaStorageEntities()) { ICryptographyManager cryptography = new CryptographyManager(); string salt = cryptography.CreateSalt(12); USER user = new USER { FirstName = newUser.FirstName, SecondName = newUser.SecondName, IsDeleted = newUser.IsDeleted, Email = newUser.Email.ToLower(), Salt = salt, Password = cryptography.HashPassword(password, salt) }; context.USERS.Add(user); context.SaveChanges(); Log.DebugFormat("Created user's ID:'{0}'", user.UserId); return(user.UserId); } }
public Node CreateNode(Node newNode) { NodeValidator nodeValidator = new NodeValidator(); nodeValidator.Validate(newNode); using (IdeaStorageEntities context = new IdeaStorageEntities()) { NODE newDbNode = new NODE { Created = newNode.Created, IsDeleted = newNode.IsDeleted, Modified = newNode.Modified, OwnerId = newNode.OwnerId, Text = newNode.Text, Title = newNode.Title }; TagManager tagManager = new TagManager(); foreach (Tag tag in newNode.Tags) { tag.TagId = tagManager.CreateTag(tag).TagId; context.TAGSETS.Add(new TAGSET { NodeId = newDbNode.NodeId, TagId = tag.TagId }); } context.NODES.Add(newDbNode); context.SaveChanges(); return(newDbNode.ToModel()); } }
/// <summary> /// Updates the user in data base according the model. /// </summary> /// <param name="user">An <see cref="User" /> instance.</param> public void UpdateUser(User user) { Log.DebugFormat("Begin UpdateUser('{0}')", user); UserValidator userValidator = new UserValidator(); userValidator.ValidateAndThrow(user); using (IdeaStorageEntities context = new IdeaStorageEntities()) { USER userFromDataBase = context.USERS.SingleOrDefault(u => u.UserId == user.UserId); if (userFromDataBase == null) { string message = string.Format("User with ID:'{0}' doesn't exist in data base.", user.UserId); Log.Debug(message); throw new EntityDoesNotExistException(message); } if (context.USERS.SingleOrDefault(u => u.Email == user.Email.ToLower() && u.UserId != user.UserId) != null) { string message = string.Format("User with email:'{0}' already exists in data base.", user.Email); Log.Debug(message); throw new DuplicateEntityException(message); } userFromDataBase.Email = user.Email.ToLower(); userFromDataBase.FirstName = user.FirstName; userFromDataBase.IsDeleted = user.IsDeleted; userFromDataBase.SecondName = user.SecondName; context.SaveChanges(); } Log.DebugFormat("User updated '{0}'", user); }
public Tag FindTagByName(string name) { using (var context = new IdeaStorageEntities()) { Tag tag = context.TAGS.SingleOrDefault(t => t.Name.Equals(name)).ToModel(); Log.DebugFormat("Found tag:'{0}'", tag); return(tag); } }
public List <Node> FindNodesByTags(string searchTerm, bool caseSens = false) { List <FuzzyStringComparisonOptions> options = new List <FuzzyStringComparisonOptions>(); // Choose which algorithms should weigh in for the comparison options.Add(FuzzyStringComparisonOptions.UseOverlapCoefficient); options.Add(FuzzyStringComparisonOptions.UseLongestCommonSubsequence); options.Add(FuzzyStringComparisonOptions.UseLongestCommonSubstring); options.Add(FuzzyStringComparisonOptions.UseLevenshteinDistance); // Choose the relative strength of the comparison - is it almost exactly equal? or is it just close? FuzzyStringComparisonTolerance tolerance = FuzzyStringComparisonTolerance.Normal; List <string> searchTermList = new List <string>(); List <TAG> searchTagsList = new List <TAG>(); List <NODE> dbNodesList = new List <NODE>(); using (IdeaStorageEntities context = new IdeaStorageEntities()) { List <TAG> allTagList = context.TAGS.Select(t => t).ToList(); if (!string.IsNullOrEmpty(searchTerm) && !string.IsNullOrWhiteSpace(searchTerm)) { searchTermList = searchTerm.Split(',').Select(a => a.Trim()).ToList(); foreach (string tagString in searchTermList) { searchTagsList.AddRange( allTagList.Where(t => t.Name.ApproximatelyEquals(tagString, options, tolerance))); } } List <TAGSET> dbTagSets = new List <TAGSET>(); foreach (TAG tag in searchTagsList) { dbTagSets.AddRange(context.TAGSETS.Where(ts => ts.TagId == tag.TagId).ToList()); } foreach (TAGSET dbTagSet in dbTagSets) { dbNodesList.Add(context.NODES.Single(n => n.NodeId == dbTagSet.NodeId)); } List <NODE> dbResult = (dbNodesList.GroupBy(n => n, n => n.NodeId) .Select(g => new { g, count = g.Count() }) .OrderByDescending(@t => @t.count) .Select(@t => @t.g.Key)).ToList(); List <Node> result = dbResult.Select(n => n.ToModel()).ToList(); return(result); } }
/// <summary> /// Finds existent user by email. /// </summary> /// <param name="email">The users's email.</param> /// <param name="includeDeleted">if set to <c>true</c> then deleted users will be included in search strategy.</param> /// <returns> /// An <see cref="User" /> instance if user can be found by email. /// </returns> public User FindUserByEmail(string email, bool includeDeleted = false) { Log.DebugFormat("Begin FindUserByEmail(email:'{0}', includeDeleted:'{1}')", email, includeDeleted); using (IdeaStorageEntities context = new IdeaStorageEntities()) { User foundUser = context.USERS.SingleOrDefault(u => u.Email.Equals(email.ToLower()) && (!u.IsDeleted || includeDeleted)) .ToModel(); Log.DebugFormat("Found user:'******'", foundUser); return(foundUser); } }
public Tag GetTagById(int id) { using (IdeaStorageEntities context = new IdeaStorageEntities()) { Tag tag = context.TAGS.SingleOrDefault(u => u.TagId == id) .ToModel(); if (tag == null) { string message = string.Format("Tag with id:'{0}' doesn't exist in data base.", id); Log.Debug(message); throw new EntityDoesNotExistException(message); } Log.DebugFormat("Returned: '{0}'", tag); return(tag); } }
/// <summary> /// Soft deletes the user. Change isDeleted flag to true. /// </summary> /// <param name="id">The identifier for user which should be deleted.</param> public void DeleteUser(int id) { Log.DebugFormat("Begin DeleteUser(id:'{0}')", id); using (IdeaStorageEntities context = new IdeaStorageEntities()) { USER user = context.USERS.SingleOrDefault(u => u.UserId == id); if (user == null) { string message = string.Format("User with id:'{0}' doesn't exist in data base.", id); Log.Debug(message); throw new EntityDoesNotExistException(message); } user.IsDeleted = true; context.SaveChanges(); } Log.DebugFormat("User with ID:'{0}' deleted", id); }
/// <summary> /// Gets user credentials from database (hashed password and salt). /// </summary> /// <param name="userId">The user identifier.</param> /// <param name="hash">The hashed password.</param> /// <param name="salt">The password's salt.</param> public void GetUserCredentials(int userId, out string hash, out string salt) { Log.DebugFormat("Begin GetUserCredentials(userId:'{0}')", userId); using (IdeaStorageEntities context = new IdeaStorageEntities()) { USER user = context.USERS.SingleOrDefault(u => u.UserId == userId); if (user == null) { string message = string.Format("User with ID:'{0}' doesn't exist in data base.", userId); Log.Debug(message); throw new EntityDoesNotExistException(message); } hash = user.Password; salt = user.Salt; } Log.DebugFormat("Credentials returned for user with ID:'{0}'", userId); }
/// <summary> /// Gets the user from data base by id. /// </summary> /// <param name="id">The UserId.</param> /// <param name="includeDeleted">if set to <c>true</c> then deleted users will be included in search strategy.</param> /// <returns> /// An <see cref="User" /> instance if user can be found by identifier. /// </returns> public User GetUserById(int id, bool includeDeleted = false) { Log.DebugFormat("Begin GetUserById(id:'{0}', includeDeleted:'{1}')", id, includeDeleted); using (IdeaStorageEntities context = new IdeaStorageEntities()) { User user = context.USERS.SingleOrDefault(u => u.UserId == id && (!u.IsDeleted || includeDeleted)) .ToModel(); if (user == null) { string message = string.Format("User with id:'{0}' doesn't exist in data base.", id); Log.Debug(message); throw new EntityDoesNotExistException(message); } Log.DebugFormat("Returned: '{0}'", user); return(user); } }
public void DeleteNode(int id) { Log.DebugFormat("Begin DeleteNode(id:'{0}')", id); using (IdeaStorageEntities context = new IdeaStorageEntities()) { NODE node = context.NODES.SingleOrDefault(u => u.NodeId == id); if (node == null) { string message = string.Format("Node with id:'{0}' doesn't exist in data base.", id); Log.Debug(message); throw new EntityDoesNotExistException(message); } node.IsDeleted = true; context.SaveChanges(); } Log.DebugFormat("Node with ID:'{0}' deleted", id); }
public Tag CreateTag(Tag tag) { Tag foundTag = this.FindTagByName(tag.Name); if (foundTag != null) { return(foundTag); } using (var context = new IdeaStorageEntities()) { TAG newTag = new TAG { Name = tag.Name, }; context.TAGS.Add(newTag); context.SaveChanges(); return(newTag.ToModel()); } }
public void UpdateNode(Node node) { NodeValidator nodeValidator = new NodeValidator(); nodeValidator.Validate(node); using (IdeaStorageEntities context = new IdeaStorageEntities()) { NODE dbNode = context.NODES.FirstOrDefault(n => n.NodeId == node.NodeId); if (dbNode == null) { string message = string.Format("Node with ID:'{0}' doesn't exist in data base.", node.NodeId); Log.Debug(message); throw new EntityDoesNotExistException(message); } dbNode.Modified = node.Modified; dbNode.Text = node.Text; dbNode.Title = node.Title; List <TAGSET> dbDeleteTagsets = context.TAGSETS.Where(ts => ts.NodeId == node.NodeId).ToList(); context.TAGSETS.RemoveRange(dbDeleteTagsets); TagManager tagManager = new TagManager(); foreach (Tag tag in node.Tags) { tag.TagId = tagManager.CreateTag(tag).TagId; context.TAGSETS.Add(new TAGSET { NodeId = dbNode.NodeId, TagId = tag.TagId }); } context.NODES.AddOrUpdate(dbNode); context.SaveChanges(); } }