Ejemplo n.º 1
0
        public IActionResult Post(Task task)
        {
            //Check if the task have another id then 0
            if (task.Id != 0)
            {
                //If it is id is something else tell them something is wrong.
                //This is added for error check from the front-end.
                return(BadRequest());
            }
            //Check if all the information is added.
            if (ModelState.IsValid)
            {
                //Makes a connection between task and user
                task.User   = _userManager.GetUserAsync(User).Result;
                task.UserId = _userManager.GetUserAsync(User).Result.Id;

                //Add task type
                task.Type = Task.TaskType.NORMAL;

                //Add task to database and saves it
                _db.Add(task);
                _db.SaveChanges();
            }

            //Return 200 ok with the new task
            return(Ok(task));
        }
Ejemplo n.º 2
0
        public IActionResult Post(Task task)
        {
            if (task.Id != 0)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                //Makes a connection between task and user
                task.User   = _userManager.GetUserAsync(User).Result;
                task.UserId = _userManager.GetUserAsync(User).Result.Id;

                //Add task to database and saves it
                _db.Add(task);
                _db.SaveChanges();
            }


            //Return 200 ok with the new task
            return(Ok(task));

            //return 201 Created with the location of the new task
            //return CreatedAtAction(nameof(Get), new {id = task.Id}, task);
        }
Ejemplo n.º 3
0
        private void GenerateTasks(Dictionary <ApplicationUser, List <Course> > courseList, DateTime from, DateTime lastDayBeforeExam)
        {
            //TODO Add settings values from: startTime, endTime
            //TODO Need to take in tasks from timeEdit
            //TODO Make it so task on primary day and rest get on func since close to doup code.
            //TODO need to make all math roundings down for safty on auto gen.
            //TODO Lastly on REST check, make it if not fit on weekdays put on weekends.
            //TODO Make it so if it cant make hours need to 0, then tell user to change start and end time.
            //TODO Make it so when adding days from rest it go after each other and not spaces.
            //TODO Clean up code more

            //start time
            var startTime = new DateTime(2019, 1, 1, 8, 0, 0);
            //end time
            var endTime = new DateTime(2019, 1, 1, 20, 0, 0);
            //inbetween time  only for hours at the moment
            var betweenTime = endTime.Hour - startTime.Hour;
            //grad C for the moment
            var grad = 300;

            //Get a list of weekdays forward to Exam Periode
            //From is set to a date instead from current date for testing.
            var dayList  = GenerateMapOfWeekdaysToExamPeriod(from, lastDayBeforeExam);
            var dayList2 = GenerateListOfWeekdaysToExamPeriod(from, lastDayBeforeExam);

            //Users that we are going to do it on.
            var users = courseList.Keys;

            //For each on every user
            foreach (var user in users)
            {
                //Get how many courses the user have
                var numCourses = courseList[user].Count;

                //Courses the user have
                var courses = courseList[user];

                //TODO get TimeEdit times to check against if not cached
                //TODO Cache them so we need less request auto for it.

                //Get all task for the current user
                var currentUserTasks = _db.Tasks.Where(w => w.UserId == user.Id).ToList();

                //Hours need to reach the goal on the different courses
                Dictionary <Course, float> hoursNeedToReachTheGoal = new Dictionary <Course, float>();

                //max task on a day 2 task types on a day
                int maxHoursTaskOnDay;
                if (numCourses <= 5)
                {
                    maxHoursTaskOnDay = betweenTime;
                }
                else
                {
                    maxHoursTaskOnDay = betweenTime / 2;
                }

                //List for new auto tasks
                List <Task> newTasks = new List <Task>();

                //Add to primary day
                var courseIndex = 0;
                foreach (var course in courses)
                {
                    //Hours to reach the goal
                    hoursNeedToReachTheGoal[course] = grad - course.HoursDone;

                    //Add tasks on Priority days all the way to exam periode
                    foreach (var day in dayList[StaticDataModel.Weekdays[courseIndex]])
                    {
                        var taskCounter = 0;
                        //For each hour down
                        for (int i = 0; i < betweenTime; i++)
                        {
                            if (hoursNeedToReachTheGoal[course] < 0 || maxHoursTaskOnDay == taskCounter)
                            {
                                break;
                            }

                            //TODO Later add task so they can stick together instead of many small ones

                            //First check if date is the same
                            //Check if my task start is between the start and end of a task
                            // && it is connected to the right user
                            //If it does not exist the i can add a task
                            if (!currentUserTasks.Exists(
                                    e => e.Start.Day == day.Day &&
                                    e.Start.Month == day.Month &&
                                    e.Start.Hour <= (i + startTime.Hour) &&
                                    e.End.Hour >= (i + startTime.Hour) &&
                                    e.UserId == user.Id))
                            {
                                //Make a new task
                                Task newTask = MakeNewAutoGenerateTask(day, startTime.Hour + i, course, user);

                                //Add task to new Task list
                                newTasks.Add(newTask);

                                //Remove 1 hour because we new task is 1 hour
                                hoursNeedToReachTheGoal[course] -= 1;

                                //Add 1 to the task counter on this day
                                taskCounter++;
                            }
                        }
                    }

                    courseIndex++;
                    //Reset course index to start, aka friday +1 day gives monday
                    if (courseIndex == StaticDataModel.Weekdays.Count)
                    {
                        courseIndex = 0;
                    }
                    //course.
                }

                //Rest add here
                foreach (var course in courses)
                {
                    //Equal placement for every course on rest days
                    maxHoursTaskOnDay = betweenTime / courses.Count;

                    for (int k = 0; k < dayList2.Count; k++)
                    {
                        var taskCounter = 0;

                        //For each hour down
                        for (int i = 0; i < betweenTime; i++)
                        {
                            if (hoursNeedToReachTheGoal[course] < 0 || maxHoursTaskOnDay == taskCounter)
                            {
                                break;
                            }

                            //TODO Later add task so they can stick together instead of many small ones

                            //First check if date is the same
                            //Check if my task start is between the start and end of a task
                            // && it is connected to the right user
                            //If it does not exist the i can add a task
                            if (!currentUserTasks.Exists(
                                    e => e.Start.Day == dayList2[k].Day &&
                                    e.Start.Month == dayList2[k].Month &&
                                    e.Start.Hour <= (i + startTime.Hour) &&
                                    e.End.Hour >= (i + startTime.Hour) &&
                                    e.UserId == user.Id) &&
                                (!newTasks.Exists(
                                     e => e.Start.Day == dayList2[k].Day &&
                                     e.Start.Month == dayList2[k].Month &&
                                     e.Start.Hour <= (i + startTime.Hour) &&
                                     e.End.Hour >= (i + startTime.Hour) &&
                                     e.UserId == user.Id))
                                )
                            {
                                //Make a new task
                                Task newTask = MakeNewAutoGenerateTask(dayList2[k], startTime.Hour + i, course, user);

                                //Add task to new Task list
                                newTasks.Add(newTask);

                                //Remove 1 hour because we new task is 1 hour
                                hoursNeedToReachTheGoal[course] -= 1;

                                //Add 1 to the task counter on this day
                                taskCounter++;
                            }
                        }
                    }
                }

                //Check for rest on all is cleared;
                foreach (var course in courses)
                {
                    if (hoursNeedToReachTheGoal[course] <= 0)
                    {
                        //TODO Add on weekends then.
                        Console.Write("Course: " + course.Code +
                                      ", Hours still needed for goal: " + hoursNeedToReachTheGoal[course]);
                    }
                }

                //Add new tasks to database
                foreach (var task in newTasks)
                {
                    _db.Add(task);
                }

                //Save changes to database
                _db.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        public IActionResult GetUserInfo()
        {
            ApplicationUserController userController = new ApplicationUserController(_db, _userManager);

            userController.ConnectUserAndTokens(_db.Users.Find(_userManager.GetUserId(User)));

            var trueToken = new Token();

            var accessToken = _db.Tokens.Where(w => w.UserId == _userManager.GetUserId(User)).ToList();

            foreach (var token in accessToken)
            {
                if (token.UserId == _userManager.GetUserId(User))
                {
                    trueToken = token;
                }
            }

            HttpClient http = new HttpClient();

            //Adding HTTP header to our Get request, the token parameter should be trueToken.AccessToken
            //However the access tokens we receive from the FEIDE login do not work
            http.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", "1f4d6f53-1594-459f-a718-bbc251861e5a");
            var data = http.GetAsync("https://groups-api.dataporten.no/groups/me/groups").Result.Content
                       .ReadAsStringAsync().Result;
            var tempString = data.Split(",");

            foreach (var temp in tempString)
            {
                if (temp.Contains("id"))
                {
                    var courseCode = temp.Split(":");
                    foreach (var course in courseCode)
                    {
                        if (course.Any(char.IsUpper) && course.Any(char.IsDigit))
                        {
                            Course newCourse = new Course();
                            newCourse.Code   = course;
                            newCourse.UserId = _userManager.GetUserId(User);
                            //newCourse.User;
                            var courseList = _db.Courses.Where(w =>
                                                               w.UserId == _userManager.GetUserId(User) &&
                                                               w.Code == newCourse.Code).ToList();

                            if (courseList.Count == 0)
                            {
                                _db.Add(newCourse);
                            }
                        }
                    }
                }
            }



            _db.SaveChanges();


            return(Ok());
        }
Ejemplo n.º 5
0
        public IActionResult GetUserInfo(string userId)
        {
            ApplicationUserController userController = new ApplicationUserController(_db, _userManager);

            userController.ConnectUserAndTokens(_db.Users.Find(userId));

            var trueToken = new Token();

            var accessToken = _db.Tokens.Where(w => w.UserId == userId).ToList();

            foreach (var token in accessToken)
            {
                if (token.UserId == userId)
                {
                    trueToken = token;
                }
            }

            HttpClient http = new HttpClient();

            //Adding HTTP header to our Get request, the token parameter should be trueToken.AccessToken
            //However the access tokens we receive from the FEIDE login do not work
            //We use a token generated by postman instead, as it uses OAUTH 2.0 instead of OIDC to receive the token
            http.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", "33d21b72-d49d-4ccd-ac84-0f5e4e28178f");
            var data = http.GetAsync("https://groups-api.dataporten.no/groups/me/groups").Result.Content
                       .ReadAsStringAsync().Result;
            var tempString = data.Split(",");

            //set a length that the Feide reply has to be greater than to ensure only valid responses
            if (data.Length < 100)
            {
                return(BadRequest());
            }
            foreach (var temp in tempString)
            {
                if (temp.Contains("id"))
                {
                    var courseCode = temp.Split(":");
                    foreach (var course in courseCode)
                    {
                        if (course.Any(char.IsUpper) && course.Any(char.IsDigit))
                        {
                            Course newCourse = new Course();
                            newCourse.Code   = course;
                            newCourse.UserId = userId;
                            //newCourse.User;
                            var courseList = _db.Courses.Where(w =>
                                                               w.UserId == userId &&
                                                               w.Code == newCourse.Code).ToList();

                            if (courseList.Count == 0)
                            {
                                _db.Add(newCourse);
                            }
                        }
                    }
                }
            }
            _db.SaveChanges();

            return(Ok());
        }