public async Task <ActionResult> CloneGroupCustomTaskForUsers() { var travelGroupandUser = (TravelGroupandUser)TempData["travelGroupandUser"]; var HabiticaORM = new habiticatravelEntities(); var travelGroupUsers = HabiticaORM.TravelGroupUsers.Where(u => u.TravelGroupId == travelGroupandUser.TravelGroup.TravelGroupId).ToList(); foreach (var user in travelGroupUsers) { var taskAndItems = (TaskAndItems)TempData["taskAndItems"]; taskAndItems.CustomTask.UserId = user.UserId; var MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == user.UserId); taskAndItems.CustomTask.TaskTag = MyHabUser.TaskTagId; HabiticaORM.TravelGroups.Find(user.TravelGroupId).CustomTasks.Add(taskAndItems.CustomTask); HabiticaORM.SaveChanges(); var TaskConfirm = JObject.FromObject(await HabiticaHTTP.PostNewHabiticaTask(taskAndItems.CustomTask, MyHabUser)); // assigning the taskId from habitica to the current task we are working with. we are doing this so // when we submit this task back to habitica, users will have updated exp and stats. taskAndItems.CustomTask.HabiticaTaskId = (string)TaskConfirm["data"]["id"]; if (taskAndItems.CustomTaskItem != null) { foreach (var item in taskAndItems.CustomTaskItem) { // we are adding each checklist item id from habiticas api and storing it in the current // task that we are working with, then we are adding each of those task items into the database // which again contains that checklist id. we are doing this so we can submit this task // back to habitica so we can updat their exp. var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.PostNewChecklistItem(item, MyHabUser, taskAndItems.CustomTask)); List <Checklist> AllChecklistItems = ItemConfirm["data"]["checklist"].ToObject <List <Checklist> >(); foreach (Checklist checkItem in AllChecklistItems) { if (checkItem.text == item.ItemName) { item.HabiticaItemId = checkItem.id; } } item.TaskId = taskAndItems.CustomTask.TaskId; HabiticaORM.CustomTaskItems.Add(item); } } else { taskAndItems.CustomTask.CustomTaskItems = new List <CustomTaskItem>(); } HabiticaORM.SaveChanges(); } var route = new RouteValueDictionary(new { controller = "Tasks", action = "ShowGroupTasks", travelGroupId = travelGroupandUser.TravelGroup.TravelGroupId }); return(RedirectToAction("ShowGroupTasks", route)); }
public async Task <ActionResult> SubmitTask(int TaskId) { var HabiticaORM = new habiticatravelEntities(); var task = HabiticaORM.CustomTasks.Find(TaskId); var data = (JObject)JObject.FromObject(await HabiticaHTTP.PostScoreATask(task, "up")); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { // stored the outpout, which is a string, into and then did the classic JObject parsing, // so we can navigate the key,values dictionary style. var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var JSON = (JObject)JObject.FromObject(await HabiticaHTTP.PostRegisterNewUser(user, model)); if (bool.Parse(JSON["success"].ToString())) { var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); // RegisterNewUser() TempData["JSON"] = JSON; TempData["user"] = user; //var routeValues = new RouteValueDictionary //{ // { "user" , user }, // { "model", model } //}; return(RedirectToAction("RegisterNewUser", "HabiticaAccount")); } AddErrors(result); } else { ViewBag.Message = "You already have an account with Habitica. Would you like to sign in with your Habitica account?"; return(View("../Habitica/AlreadyRegisteredWithHabitica")); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RemoveGroupTask(TravelGroupandUserTaskandItems model, int TaskId) { habiticatravelEntities HabiticaORM = new habiticatravelEntities(); CustomTask selectedTask = HabiticaORM.CustomTasks.Single(t => t.TaskId == TaskId && t.UserId == null); var selectedTaskItems = HabiticaORM.CustomTaskItems.Where(t => t.TaskId == TaskId).ToList(); if (selectedTaskItems.Count != 0) { foreach (var item in selectedTaskItems) { HabiticaORM.CustomTaskItems.Remove(item); } } HabiticaORM.CustomTasks.Remove(selectedTask); List <TravelGroupUser> GroupUsers = new List <TravelGroupUser>(); GroupUsers = HabiticaORM.TravelGroupUsers.Where(u => u.TravelGroupId == model.TravelGroupandUser.TravelGroup.TravelGroupId).ToList(); foreach (TravelGroupUser user in GroupUsers) { HabiticaUser MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == user.UserId); CustomTask selectedUserTask = HabiticaORM.CustomTasks.Single(t => t.TravelGroupId == model.TaskAndItems.CustomTask.TravelGroupId && t.UserId == user.UserId && t.TaskName == model.TaskAndItems.CustomTask.TaskName); var selectedUserTaskItems = HabiticaORM.CustomTaskItems.Where(t => t.TaskId == selectedUserTask.TaskId).ToList(); if (selectedUserTaskItems.Count != 0) { foreach (var item in selectedUserTaskItems) { var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.DeleteChecklistItem(selectedUserTask, item, MyHabUser)); HabiticaORM.CustomTaskItems.Remove(item); } } var ItemConfirm2 = (JObject)JObject.FromObject(await HabiticaHTTP.DeleteATask(selectedUserTask, MyHabUser)); HabiticaORM.CustomTasks.Remove(selectedUserTask); } HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> RegisterNewUser() { // test var JSON = (JObject)TempData["JSON"]; var user = (ApplicationUser)TempData["user"]; // these are both of our ORM copies, so the the first one is our Habitica User, // second ORM is basicaly the an object that wraps around our ApplicationUser, the reason // we have to do it this way is because UserManager allows us to preform CRUD operation // to our Identity database. var HabiticaORM = new habiticatravelEntities(); userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); // simply finding the user in our Identity database by whatever the user put into // the User Name form field. var idenUser = userManager.FindByName(user.UserName); // creating a new HabiticaUser object which will store the required Non-NULL values, we did // not include the data for HabiticaUserID because that will auto increment in the database. var habiticaUser = new HabiticaUser { ApiToken = (string)JSON["data"]["apiToken"], Uuid = (string)JSON["data"]["id"], UserId = idenUser.Id }; var tagKey = (JObject)JObject.FromObject(await HabiticaHTTP.PostCreateTag(habiticaUser)); habiticaUser.TaskTagId = (string)tagKey["data"]["id"]; // finishing off our CRUD operation, we are adding the new HabiticaUser into our database // and saving our changes so it wil reflect in the database. then we are simply returning // the HomePage view for now. HabiticaORM.HabiticaUsers.Add(habiticaUser); HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> SaveCustomTaskChanges(TaskAndItems NewTaskAndItems) { var HabiticaORM = new habiticatravelEntities(); string UserId = User.Identity.GetUserId(); HabiticaUser MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == UserId); int TaskId = NewTaskAndItems.CustomTask.TaskId; CustomTask DBTask = HabiticaORM.CustomTasks.Find(TaskId); List <CustomTaskItem> DBItemsList = new List <CustomTaskItem>(); if (HabiticaORM.CustomTasks.Find(TaskId).CustomTaskItems != null) { DBItemsList = HabiticaORM.CustomTasks.Find(TaskId).CustomTaskItems.ToList(); } CustomTask MyTask = NewTaskAndItems.CustomTask; List <CustomTaskItem> MyItemsList = new List <CustomTaskItem>(); if (NewTaskAndItems.CustomTaskItem != null && DBItemsList.Count != 0) { foreach (CustomTaskItem T in NewTaskAndItems.CustomTaskItem) { MyItemsList.Add(T); HabiticaORM.Entry(HabiticaORM.CustomTaskItems.Find(T.TaskItemsId)).CurrentValues.SetValues(T); var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.PutUpdateChecklistItem(T, MyHabUser, MyTask)); } } MyTask.CustomTaskItems = MyItemsList; HabiticaORM.Entry(DBTask).CurrentValues.SetValues(MyTask); var ItemConfirm2 = (JObject)JObject.FromObject(await HabiticaHTTP.PutUpdateTask(MyTask, MyHabUser)); HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> RemoveTask(int TaskId) { var HabiticaORM = new habiticatravelEntities(); string UserId = User.Identity.GetUserId(); HabiticaUser MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == UserId); var selectedTask = HabiticaORM.CustomTasks.Where(t => t.TaskId == TaskId).FirstOrDefault(); var selectedTaskItems = HabiticaORM.CustomTaskItems.Where(t => t.TaskId == TaskId).ToList(); if (selectedTaskItems.Count != 0) { foreach (var item in selectedTaskItems) { var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.DeleteChecklistItem(selectedTask, item, MyHabUser)); HabiticaORM.CustomTaskItems.Remove(item); } } var ItemConfirm2 = (JObject)JObject.FromObject(await HabiticaHTTP.DeleteATask(selectedTask, MyHabUser)); HabiticaORM.CustomTasks.Remove(selectedTask); HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> HabiticaLoginHandler(RegisterViewModel model) { var JSON = (JObject)JObject.FromObject(await HabiticaHTTP.PostUserLogin(model.Email, model.Password)); if (bool.Parse(JSON["success"].ToString())) { var UpdatedHabiticaUser = new HabiticaUser { Uuid = (string)JSON["data"]["id"], ApiToken = (string)JSON["data"]["apiToken"], }; var HabiticaORM = new habiticatravelEntities(); userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); // This is for two purposes, if this returns null, than we make a new user, // if it does not, we will find this user in the Habitica table by ID. ApplicationUser User = userManager.FindByEmail(model.Email); if (User == null) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); var newUser = userManager.FindByEmail(user.Email); UpdatedHabiticaUser.UserId = newUser.Id; // pulling user data so we can check if that user has the HabiticaAbroad tag, reason we care // is we do not want to create duplicate tags on their habitica account. var userData = (JObject)JObject.FromObject(await HabiticaHTTP.GetUserData(UpdatedHabiticaUser)); List <Tag> tags = userData["data"]["tags"].ToObject <List <Tag> >(); // var isTagged = tags.Where(t => t.id == "Habitica Abroad"); var tagKey = JObject.FromObject(await HabiticaHTTP.PostCreateTag(UpdatedHabiticaUser)); UpdatedHabiticaUser.TaskTagId = (string)tagKey["data"]["id"]; HabiticaORM.HabiticaUsers.Add(UpdatedHabiticaUser); HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); } AddErrors(result); return(View(model)); } else { // simply updating the habitica User that is not null with the // HabiticaUser object that was created on line 88 var result = await SignInManager.PasswordSignInAsync(User.UserName, model.Password, false, shouldLockout : false); switch (result) { case SignInStatus.Success: return(RedirectToAction("Index", "Home")); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } } } else { // this is just a place holder, we need to pass a view that tells user // that ViewBag.Message = "You do not have an account with Habitica. Please Register."; return(View("../HabiticaAccount/AlreadyRegisteredWithHabitica")); } }
public async Task <ActionResult> SaveCustomGroupTaskChanges(TaskAndItems NewTaskAndItems) { habiticatravelEntities HabiticaORM = new habiticatravelEntities(); int TaskId = NewTaskAndItems.CustomTask.TaskId; CustomTask DBTask = HabiticaORM.CustomTasks.Find(TaskId); List <CustomTaskItem> DBItemsList = new List <CustomTaskItem>(); if (DBTask.CustomTaskItems != null) { DBItemsList = HabiticaORM.CustomTasks.Find(TaskId).CustomTaskItems.ToList(); } CustomTask MyTask = NewTaskAndItems.CustomTask; List <CustomTaskItem> MyItemsList = new List <CustomTaskItem>(); if (NewTaskAndItems.CustomTaskItem != null && DBItemsList.Count != 0) { foreach (CustomTaskItem T in NewTaskAndItems.CustomTaskItem) { MyItemsList.Add(T); HabiticaORM.Entry(HabiticaORM.CustomTaskItems.Find(T.TaskItemsId)).CurrentValues.SetValues(T); } } MyTask.CustomTaskItems = MyItemsList; HabiticaORM.Entry(DBTask).CurrentValues.SetValues(MyTask); HabiticaORM.SaveChanges(); //Above changes the Group Item only. Below we change the values for each Group Member List <TravelGroupUser> GroupUsers = new List <TravelGroupUser>(); GroupUsers = HabiticaORM.TravelGroupUsers.Where(u => u.TravelGroupId == NewTaskAndItems.CustomTask.TravelGroupId).ToList(); foreach (TravelGroupUser user in GroupUsers) { HabiticaUser MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == user.UserId); CustomTask UserTask = HabiticaORM.CustomTasks.Single(u => u.TravelGroupId == NewTaskAndItems.CustomTask.TravelGroupId && u.UserId == user.UserId && u.TaskName == DBTask.TaskName); List <CustomTaskItem> UserItemsList = new List <CustomTaskItem>(); if (UserTask.CustomTaskItems != null) { UserItemsList = HabiticaORM.CustomTasks.Find(UserTask.TaskId).CustomTaskItems.ToList(); } CustomTask MyUserTask = NewTaskAndItems.CustomTask; List <CustomTaskItem> MyUserItemsList = new List <CustomTaskItem>(); if (NewTaskAndItems.CustomTaskItem != null && DBItemsList.Count != 0) { foreach (CustomTaskItem T in NewTaskAndItems.CustomTaskItem) { MyUserItemsList.Add(T); HabiticaORM.Entry(HabiticaORM.CustomTaskItems.Find(T.TaskItemsId)).CurrentValues.SetValues(T); var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.PutUpdateChecklistItem(T, MyHabUser, MyTask)); } } MyUserTask.CustomTaskItems = MyUserItemsList; HabiticaORM.Entry(UserTask).CurrentValues.SetValues(MyUserTask); var ItemConfirm2 = (JObject)JObject.FromObject(await HabiticaHTTP.PutUpdateTask(MyTask, MyHabUser)); HabiticaORM.SaveChanges(); } return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> AddCustomTask(TaskAndItems model) { string userId = User.Identity.GetUserId(); var HabiticaORM = new habiticatravelEntities(); List <CustomTaskItem> items; if (model.CustomTaskItem != null) { items = model.CustomTaskItem; HabiticaORM.CustomTaskItems.AddRange(items); } else { items = new List <CustomTaskItem> { new CustomTaskItem() }; HabiticaORM.CustomTaskItems.AddRange(items); } model.CustomTask.UserId = userId; HabiticaUser MyHabUser = HabiticaORM.HabiticaUsers.Single(u => u.UserId == userId); model.CustomTask.TaskTag = MyHabUser.TaskTagId; model.CustomTask.TaskType = "todo"; model.CustomTaskItem = items; var TaskConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.PostNewHabiticaTask(model.CustomTask, MyHabUser)); HabiticaORM.CustomTasks.Add(model.CustomTask); try { HabiticaORM.SaveChanges(); } catch (DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } var currentTask = HabiticaORM.CustomTasks.Where(t => model.CustomTask.TaskId == t.TaskId).FirstOrDefault(); var TestItem = (string)TaskConfirm["data"]["id"]; currentTask.HabiticaTaskId = (string)TaskConfirm["data"]["id"]; string habtaskid = currentTask.HabiticaTaskId; HabiticaORM.SaveChanges(); if (model.CustomTask.CustomTaskItems.Count != 0) { var taskItems = model.CustomTaskItem.ToList(); foreach (var item in taskItems) { var ItemConfirm = (JObject)JObject.FromObject(await HabiticaHTTP.PostNewChecklistItem(item, MyHabUser, currentTask)); List <Checklist> AllChecklistItems = ItemConfirm["data"]["checklist"].ToObject <List <Checklist> >(); foreach (Checklist list in AllChecklistItems) { if (list.text == item.ItemName) { item.HabiticaItemId = list.id; } } item.TaskId = currentTask.TaskId; } currentTask.CustomTaskItems = taskItems; } else { currentTask.CustomTaskItems = new List <CustomTaskItem>(); } HabiticaORM.SaveChanges(); return(RedirectToAction("Index", "Home")); }