public User CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetUserByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User { Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), DateCreated = DateTime.Now }; _userRepository.Add(user); _dbContext.SaveChanges(); if (roles != null && roles.Length > 0) { foreach (var role in roles) { AddUserToRole(user, role); } } _dbContext.SaveChanges(); return(user); }
private void LogError(Exception ex) { try { var error = new Error { Message = ex.Message, StackTrace = ex.StackTrace, DateCreated = DateTime.Now }; _errorRepository.Add(error); _dbContext.SaveChanges(); } catch { } }
public void RemoveItem(string removeCartId, int removeProdId) { try { using (var db = new HomeCinemaContext()) { // i need here to select the item and the movieId then i can remove cart var myItem = (from c in db.Orders where c.CartId == removeCartId select c).FirstOrDefault(); if (myItem != null) { // remove from DB db.Orders.Remove(myItem); db.SaveChanges(); } } } catch (Exception ex) { throw new Exception("Error: unable to Remove item " + ex.Message); } }