Beispiel #1
0
        public static async Task <dynamic> PostNewChecklistItem(CustomTaskItem item, HabiticaUser user, CustomTask task)
        {
            string str1 = "checklist";

            try
            {
                return(await "https://habitica.com/api/v3/tasks/"
                       .WithHeaders(new
                {
                    x_api_user = user.Uuid,
                    x_api_key = user.ApiToken
                })
                       .AppendPathSegment(task.HabiticaTaskId, true)
                       .AppendPathSegment(str1, true)
                       .PostJsonAsync(new
                {
                    text = item.ItemName,
                })
                       .ReceiveJson());
            }
            catch (FlurlHttpException ex)
            {
                return(ex.GetResponseJson());
            }
        }
Beispiel #2
0
        public static async Task <dynamic> PutUpdateTask(CustomTask task, HabiticaUser user)
        {
            var data = new SendTask
            {
                text      = task.TaskName,
                type      = task.TaskType,
                tags      = new string[] { user.TaskTagId },
                notes     = task.TaskNotes,
                date      = task.TaskDueDate.ToString(),
                reminders = new Reminder[1]
                {
                    new Reminder()
                    {
                        id = user.Uuid, startDate = task.ReminderStartDate.ToString(), time = task.ReminderTime.ToString()
                    }
                },
                priority = task.TaskDifficulty.ToString()
            };

            try
            {
                return(await $"https://habitica.com/api/v3/tasks/{task.HabiticaTaskId}"
                       .WithHeaders(new
                {
                    x_api_user = user.Uuid,
                    x_api_key = user.ApiToken
                })
                       .PostJsonAsync(data)
                       .ReceiveJson());
            }
            catch (FlurlHttpException ex)
            {
                return(ex.GetResponseJson());
            }
        }
Beispiel #3
0
        public static async Task <JObject> GetUserStats(string id)
        {
            // string CurrentUser = "", CurrentApiToken = "";

            habiticatravelEntities MyHabitica     = new habiticatravelEntities();
            HabiticaUser           MyHabiticaUser = MyHabitica.HabiticaUsers.Where(y => y.UserId == id).FirstOrDefault();

            return(JObject.FromObject(await HabiticaHTTP.GetUserData(MyHabiticaUser)));
        }
Beispiel #4
0
 public static async Task <dynamic> GetUserData(HabiticaUser model)
 {
     try
     {
         return(await "https://habitica.com/api/v3/user"
                .WithHeaders(new
         {
             x_api_key = model.ApiToken,
             x_api_user = model.Uuid,
         })
                .GetJsonAsync());
     }
     catch (FlurlHttpException ex)
     {
         return(ex.GetResponseJson());
     }
 }
Beispiel #5
0
 public static async Task <dynamic> PostClearCompletedToDos(HabiticaUser user)
 {
     try
     {
         return(await "https://habitica.com/api/v3/tasks/clearCompletedTodos"
                .WithHeaders(new
         {
             x_api_key = user.ApiToken,
             x_api_user = user.Uuid,
         })
                .GetJsonAsync());
     }
     catch (FlurlHttpException ex)
     {
         return(ex.GetResponseJson());
     }
 }
Beispiel #6
0
 public static async Task <dynamic> DeleteATask(CustomTask task, HabiticaUser user)
 {
     try
     {
         return(await $"https://habitica.com/api/v3/tasks/{task.HabiticaTaskId}/tags/{task.TaskTag}"
                .WithHeaders(new
         {
             x_api_key = user.ApiToken,
             x_api_user = user.Uuid,
         })
                .GetJsonAsync());
     }
     catch (FlurlHttpException ex)
     {
         return(ex.GetResponseJson());
     }
 }
Beispiel #7
0
        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"));
        }
Beispiel #8
0
 public static async Task <dynamic> PostCreateTag(HabiticaUser user)
 {
     try
     {
         return(await "https://habitica.com/api/v3/tags"
                .WithHeaders(new
         {
             x_api_user = user.Uuid,
             x_api_key = user.ApiToken
         })
                .PostJsonAsync(new
         {
             name = "Habitica Abroad"
         })
                .ReceiveJson());
     }
     catch (FlurlHttpException ex)
     {
         return(ex.GetResponseJson());
     }
 }
Beispiel #9
0
        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"));
        }
Beispiel #10
0
        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"));
        }
Beispiel #11
0
        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"));
        }
Beispiel #12
0
 public static async Task <dynamic> PutUpdateChecklistItem(CustomTaskItem item, HabiticaUser user, CustomTask task)
 {
     try
     {
         return(await $"https://habitica.com/api/v3/tasks/{task.HabiticaTaskId}/checklist/{item.HabiticaItemId}"
                .WithHeaders(new
         {
             x_api_user = user.Uuid,
             x_api_key = user.ApiToken
         })
                .PostJsonAsync(new
         {
             text = item.ItemName,
         })
                .ReceiveJson());
     }
     catch (FlurlHttpException ex)
     {
         return(ex.GetResponseJson());
     }
 }
Beispiel #13
0
        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"));
            }
        }
Beispiel #14
0
        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"));
        }
Beispiel #15
0
        public ActionResult CloneTask(int TaskId)
        {
            habiticatravelEntities MyHabitica  = new habiticatravelEntities();
            DefaultTask            TaskToClone = MyHabitica.DefaultTasks.Find(TaskId);
            string       userId    = User.Identity.GetUserId();
            HabiticaUser MyHabUser = MyHabitica.HabiticaUsers.Single(u => u.UserId == userId);

            CustomTask ClonedTask = new CustomTask
            {
                TaskName          = TaskToClone.TaskName,
                TaskType          = TaskToClone.TaskType,
                TaskTag           = MyHabUser.TaskTagId,
                TaskNotes         = TaskToClone.TaskNotes,
                TaskDueDate       = TaskToClone.TaskDueDate,
                TaskDifficulty    = TaskToClone.TaskDifficulty,
                ReminderStartDate = TaskToClone.ReminderStartDate,
                ReminderTime      = TaskToClone.ReminderTime,
                UserId            = userId,
                CustomTaskItems   = new List <CustomTaskItem>()
            };

            MyHabitica.CustomTasks.Add(ClonedTask);
            try
            {
                MyHabitica.SaveChanges();
            }
            catch (System.Data.Entity.Validation.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;
            }

            List <DefaultTaskItem> CloneItemsList = new List <DefaultTaskItem>(MyHabitica.DefaultTasks.Find(TaskId).DefaultTaskItems.ToList());
            List <CustomTaskItem>  NewItemsList   = new List <CustomTaskItem>();

            for (int i = 0; i < CloneItemsList.Count; i++)
            {
                var        TempItem = new CustomTaskItem();
                int        TempId   = ClonedTask.TaskId;
                CustomTask TempTask = MyHabitica.CustomTasks.Find(TempId);
                TempItem.ItemName = CloneItemsList[i].ItemName;
                TempItem.TaskId   = TempTask.TaskId;

                MyHabitica.CustomTaskItems.Add(TempItem);
                ClonedTask.CustomTaskItems.Add(TempItem);
            }

            try
            {
                MyHabitica.SaveChanges();
            }
            catch (System.Data.Entity.Validation.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;
            }

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #16
0
        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"));
        }