public ActionResult Index(ClientUser cuser) { //if (ModelState.IsValid) //{ // db.Entry(cuser).State = EntityState.Modified; // db.SaveChanges(); // return RedirectToAction("Index"); //} //return View(cuser); var currentUser = User.Identity.Name; var cuserToUpdate = db.ClientUsers.FirstOrDefault(p => p.Username == currentUser); if (TryUpdateModel(cuserToUpdate, "", new string[] { "ContactName", "Email" })) { try { cuserToUpdate.ContactNumber = cuser.ContactNumber; cuserToUpdate.Email = cuser.Email; db.SaveChanges(); ViewBag.Message = "Record successfully Updated"; //return RedirectToAction("Index"); } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } } return View(cuserToUpdate); }
private void AddBoard(int? uplineId, ClientUser user, Position? position) { Context.BoardOne.Add(new BoardOne() { ClientUserId = user.Id, DateCreated = DateTime.UtcNow.AddHours(8), Finished = false, Position = position, UplineUserId = uplineId }); }
private bool VerifyToBoard(BoardOne board, ClientUser attachThis) { //2 var level1Left = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == board.ClientUserId && p.Position == Position.Left); if (level1Left == null) { AddBoard(board.ClientUserId, attachThis, Position.Left); return false; } var level1Right = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == board.ClientUserId && p.Position == Position.Right); if (level1Right == null) { AddBoard(board.ClientUserId, attachThis, Position.Right); return false; } //4 var level2Left1 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level1Left.ClientUserId && p.Position == Position.Left); if (level2Left1 == null) { AddBoard(level1Left.ClientUserId, attachThis, Position.Left); return false; } var level2Right1 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level1Left.ClientUserId && p.Position == Position.Right); if (level2Right1 == null) { AddBoard(level1Left.ClientUserId, attachThis, Position.Right); return false; } var level2Left2 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level1Right.ClientUserId && p.Position == Position.Left); if (level2Left2 == null) { AddBoard(level1Right.ClientUserId, attachThis, Position.Left); return false; } var level2Right2 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level1Right.ClientUserId && p.Position == Position.Right); if (level2Right2 == null) { AddBoard(level1Right.ClientUserId, attachThis, Position.Right); return false; } //8 var level3Left1 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Left1.ClientUserId && p.Position == Position.Left); if (level3Left1 == null) { AddBoard(level2Left1.ClientUserId, attachThis, Position.Left); return false; } var level3Right1 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Left1.ClientUserId && p.Position == Position.Right); if (level3Right1 == null) { AddBoard(level2Left1.ClientUserId, attachThis, Position.Right); return false; } var level3Left2 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Right1.ClientUserId && p.Position == Position.Left); if (level3Left2 == null) { AddBoard(level2Right1.ClientUserId, attachThis, Position.Left); return false; } var level3Right2 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Right1.ClientUserId && p.Position == Position.Right); if (level3Right2 == null) { AddBoard(level2Right1.ClientUserId, attachThis, Position.Right); return false; } var level3Left3 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Left2.ClientUserId && p.Position == Position.Left); if (level3Left3 == null) { AddBoard(level2Left2.ClientUserId, attachThis, Position.Left); return false; } var level3Right3 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Left2.ClientUserId && p.Position == Position.Right); if (level3Right3 == null) { AddBoard(level2Left2.ClientUserId, attachThis, Position.Right); return false; } var level3Left4 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Right2.ClientUserId && p.Position == Position.Left); if (level3Left4 == null) { AddBoard(level2Right2.ClientUserId, attachThis, Position.Left); return false; } var level3Right4 = Context.BoardOne.FirstOrDefault(p => p.UplineUserId == level2Right2.ClientUserId && p.Position == Position.Right); if (level3Right4 == null) { AddBoard(level2Right2.ClientUserId, attachThis, Position.Right); } return true; }
public async Task<string> CreateAdmin(RegisterUserViewModel clientUser) { using (var databaseTransaction = Context.Database.BeginTransaction()) { try { var model = new ClientUser() { BirthDate = clientUser.BirthDate, CardId = clientUser.CardId, ContactNumber = clientUser.ContactNumber, Email = clientUser.EmailAddress, FirstName = clientUser.FirstName, MiddleName = clientUser.MiddleName, LastName = clientUser.LastName, Password = clientUser.Password, Username = clientUser.UserName, UplineId = clientUser.UplineId, DateCreated = DateTime.Today }; var user = new ApplicationUser() { UserName = model.Username, Email = model.Email }; var result = UserManager.Create(user, model.Password); if (!result.Succeeded) return "Can't create user"; var thisUser = UserManager.Find(model.Username, model.Password); var roleResult = UserManager.AddToRole(thisUser.Id, "User"); if (!roleResult.Succeeded) return "Can't add to role"; var cardResult = Context.Cards.FirstOrDefault(p => p.Id == clientUser.CardId && p.Pin == clientUser.PinCode); if (cardResult == null) return "Invalid card number"; var existCard = Context.ClientUsers.FirstOrDefault(p => p.CardId == cardResult.Id); if (existCard != null) return "Card in use"; cardResult.Active = false; Context.Entry(cardResult).State = EntityState.Modified; model.ReferenceId = thisUser.Id; var sponsor = await Context.ClientUsers.FirstOrDefaultAsync(); Context.ClientUserSponsorships.Add(new ClientUserSponsorship() { Sponsor = sponsor, UplineUser = null, ClientUser = model, Username = model.Username, Position = clientUser.Position }); Context.ClientUsers.Add(model); var changes = await Context.SaveChangesAsync(); if (changes > 0) { databaseTransaction.Commit(); return ""; } } catch (Exception ex) { databaseTransaction.Rollback(); return ex.Message; } } return "Error"; }
public async Task<String> Create(RegisterUserViewModel clientUser) { using (var databaseTransaction = Context.Database.BeginTransaction()) { try { var model = new ClientUser() { BirthDate = clientUser.BirthDate, CardId = clientUser.CardId, ContactNumber = clientUser.ContactNumber, Email = clientUser.EmailAddress, FirstName = clientUser.FirstName, MiddleName = clientUser.MiddleName, LastName = clientUser.LastName, Password = clientUser.Password, Username = clientUser.UserName, UplineId = clientUser.UplineId, DateCreated = DateTime.UtcNow.AddHours(8) }; var user = new ApplicationUser() { UserName = model.Username, Email = model.Email }; if (MinValue > clientUser.BirthDate) { return string.Format("Invalid Date Input. Birthdate mus be greater than or equal to " + MinValue.ToShortDateString()); } var error = new StringBuilder(); var result = UserManager.Create(user, model.Password); if (!result.Succeeded) { foreach (var item in result.Errors) { error.AppendLine(item); } return error.ToString(); } var thisUser = UserManager.Find(model.Username, model.Password); var roleResult = UserManager.AddToRole(thisUser.Id, "User"); if (!roleResult.Succeeded) return "Can't add to role"; var cardResult = Context.Cards.FirstOrDefault(p => p.Id == clientUser.CardId && p.Pin == clientUser.PinCode); if (cardResult == null) return "Invalid card number"; var existCard = Context.ClientUsers.FirstOrDefault(p => p.CardId == cardResult.Id); if (existCard != null) return "Card in use"; cardResult.Active = false; Context.Entry(cardResult).State = EntityState.Modified; model.ReferenceId = thisUser.Id; var upline = await Context.ClientUsers.FirstOrDefaultAsync(p => p.Username == clientUser.UplineId); if (upline == null) { return string.Format("Can't add blank upline"); } var leftOrRight = Context.ClientUserSponsorships.FirstOrDefault(p => p.UplineUserId == upline.Id && p.Position == clientUser.Position); if (leftOrRight != null) return string.Format("Can't add user as upline '{0}'", upline.Username); var sponsor = await Context.ClientUsers.FirstOrDefaultAsync(p => p.ReferenceId == clientUser.SponsorId); Context.ClientUserSponsorships.Add(new ClientUserSponsorship() { Sponsor = sponsor, UplineUser = upline, ClientUser = model, Username = model.Username, Position = clientUser.Position, DateCreated = DateTime.UtcNow.AddHours(8) }); Context.ClientUsers.Add(model); var changes = await Context.SaveChangesAsync(); if (changes > 0) { LevelUpBoard(); databaseTransaction.Commit(); return ""; } } catch (Exception ex) { databaseTransaction.Rollback(); } } return "Error, try again. If error persist, please contact administrator"; }
private ActionResult RedirectToLocal(string returnUrl, ClientUser clientUser) { if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } return RedirectToAction("Index", "ClientUser"); }