Пример #1
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);
        }
Пример #2
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));
        }
Пример #3
0
        private void RemoveAllGeneratedTasks(DateTime from, DateTime to)
        {
            //Get all Auto generated tasks from date to date
            var tasksToDelete = _db.Tasks.Where(w => w.Type == Task.TaskType.AUTO && w.Start >= from && w.Start <= to);

            //Remove all tasks that was found
            _db.Tasks.RemoveRange(tasksToDelete);

            //Save changes to database.
            _db.SaveChanges();
        }
Пример #4
0
        public void ConnectUserAndTokens(ApplicationUser user)
        {
            var count = _db.Tokens.Count();
            var token = _db.Tokens.Find(count);

            token.UserId = user.Id;
            _db.SaveChanges();
        }
Пример #5
0
        //URL Call: https://localhost:5001/api/settings/put/1
        public IActionResult EditSettings(Setting settings)
        {
            //Check if task exists
            if (!_db.Settings.Any(w => w.Id == settings.Id))
            {
                return(NotFound());
            }

            //Update the setting
            _db.Settings.Update(settings);

            //Save changes
            _db.SaveChanges();

            // Return setting
            return(Ok(settings));
        }
        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());
        }
Пример #7
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());
        }