public ActionResult RegisterClientUser(string UplineUser, Position position) { var model = new RegisterUserViewModel() { UplineId = UplineUser }; return View(model); }
public async Task<ActionResult> RegisterClientUser(RegisterUserViewModel model) { model.SponsorId = User.Identity.GetUserId(); if (ModelState.IsValid) { var result = await ClientUserManager.Create(model); if (result != "") { ModelState.AddModelError("", result); return View(model); } } return RedirectToAction("Index", "ClientUser"); }
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"; }