public JsonData Update(UserViewModel newRecord, string userId)
        {
            try
            {
                using (var db = new DataContext())
                {
                    if (newRecord == null) throw new ArgumentNullException("The data" + " record is null");

                    var oRecord = db.Users.FirstOrDefault(p => p.Id == newRecord.Id);
                    if (oRecord == null) throw new Exception("User was not found");
                    oRecord.FullName = newRecord.FullName;
                    oRecord.PhoneNumber = newRecord.PhoneNumber;
                    oRecord.Email = newRecord.Email;
                    oRecord.DateOfBirth = newRecord.DateOfBirth;
                    oRecord.Updated = DateTime.Now;

                    var roles = db.Roles.Select(x => x.Name).ToArray();
                    var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
                    foreach (var role in roles)
                    {
                        userMan.RemoveFromRole(oRecord.Id, role);
                    }
                    userMan.AddToRole(oRecord.Id, newRecord.Roles);

                    db.SaveChanges();
                    return DataHelpers.ReturnJsonData(null, true, "Updated successfully", 1);
                }
            }
            catch (Exception e)
            {
                return DataHelpers.ExceptionProcessor(e);
            }
        }
        public JsonData Get(string userId)
        {
            try
            {
                var filter = new UserFilter { UserId = userId };
                using (var db = new DataContext())
                {
                    var roles = db.Roles.ToDictionary(x => x.Id);

                    var data = filter.BuildQuery(db.Users).Include(x => x.Roles).First();

                    if (data == null) return DataHelpers.ReturnJsonData(null, false, "No Data Found", 0);
                    var activeUser = new UserViewModel
                    {
                        UserName = data.UserName,
                        FullName = data.FullName,
                        Created = data.Created,
                        Email = data.Email,
                        PhoneNumber = data.PhoneNumber,
                        DateOfBirth = data.DateOfBirth,
                        Id = data.Id,
                        IsActive = data.IsActive,
                        Updated = data.Updated,
                        Roles = roles.First(x => x.Key == data.Roles.First().RoleId).Value.Name
                    };
                    return DataHelpers.ReturnJsonData(activeUser, true, "Loaded successfully", 1);
                }
            }
            catch (Exception e)
            {
                return DataHelpers.ExceptionProcessor(e);
            }
        }
 public JsonData Update(UserViewModel data)
 {
     return new UserRepo().Update(data, User.Identity.GetUserId());
 }
 public async Task<JsonData> Reset(UserViewModel model)
 {
     try
     {
         var db = new DataContext();
         var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
         userMan.UserValidator = new UserValidator<MyUser>(userMan)
         {
             AllowOnlyAlphanumericUserNames =
                 false
         };
         var user = await userMan.FindByEmailAsync(model.Email);
         if (user == null) throw new Exception("please check the email address");
         //todo: generate a unique password and email it to the user
         var newPassword = user.FullName.Substring(2, 3) + user.PasswordHash.Substring(0, 5);
         var result = await userMan.RemovePasswordAsync(user.Id);
         if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
         var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
         if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
         //todo: Email the new password to the user
         return DataHelpers.ReturnJsonData(null, true, "A new password has been emailed to your email address");
     }
     catch (Exception e)
     {
         return DataHelpers.ExceptionProcessor(e);
     }
 }
        public async Task<JsonData> Register(UserViewModel model)
        {
            try
            {
                using (var db = new DataContext())
                {
                    var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
                    userMan.UserValidator = new UserValidator<MyUser>(userMan)
                    {
                        AllowOnlyAlphanumericUserNames =
                            false
                    };

                    var user = new MyUser
                    {
                        UserName = model.UserName.Trim(),
                        FullName = model.FullName,
                        IsActive = model.IsActive,
                        PhoneNumber = model.PhoneNumber,
                        Email = model.Email,
                        DateOfBirth = model.DateOfBirth,
                        Created = DateTime.Now,
                        Updated = DateTime.Now
                    };

                    var result = await userMan.CreateAsync(user, model.Password.Trim());

                    if (!result.Succeeded)
                    {
                        var msg = string.Join(", ", result.Errors);
                        msg = msg.Replace("Name", "User Name");
                        msg = msg.Replace("is already taken.", "is aready in use.");
                        throw new Exception(msg);
                    }

                    userMan.AddToRole(user.Id, model.Roles);

                    db.SaveChanges();
                    return DataHelpers.ReturnJsonData(user.Id, true, "Registration was successful. Please Verify your account");
                }
            }
            catch (Exception e)
            {
                return DataHelpers.ExceptionProcessor(e);
            }
        }