public IActionResult Permissions(PermissionsViewModel permissionsViewModel)
        {
            using (var db = new CrossRefContext())
            {
                var user = db.Users.Include("Permission").First(u =>
                                                                u.Email.Equals(
                                                                    User.FindFirst(ClaimTypes.Email).Value
                                                                    ));

                user.Permission.ShowAffiliation = permissionsViewModel.ShowAffiliation;
                user.Permission.ShowArticles    = permissionsViewModel.ShowArticles;
                user.Permission.ShowBiography   = permissionsViewModel.ShowBiography;
                user.Permission.ShowDateOfBirth = permissionsViewModel.ShowDateOfBirth;

                db.Update(user);
                db.SaveChanges();

                return(RedirectToAction("MyOverview", "Profile"));
            }
        }
        public IActionResult Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (var db = new CrossRefContext())
            {
                if (db.Users.Any(u => u.Email.Equals(model.Email)))
                {
                    ModelState.AddModelError("EmailAlreadyInUse", "Email address is already in use");
                }

                var user = new User
                {
                    Email     = model.Email,
                    Password  = EncryptPassword(model.Password),
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };

                var permission = new Permission
                {
                    Author          = user,
                    ShowArticles    = false,
                    ShowBiography   = false,
                    ShowAffiliation = false,
                    ShowDateOfBirth = false
                };

                db.Users.Add(user);
                db.Permissions.Add(permission);
                db.SaveChanges();

                return(RedirectToAction("Login", "Authentication"));
            }
        }
        public IActionResult Settings(SettingsViewModel settingsViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(settingsViewModel));
            }

            using (var db = new CrossRefContext())
            {
                var user = db.Users.First(u => u.Email.Equals(
                                              User.FindFirst(ClaimTypes.Email).Value));

                user.FirstName   = settingsViewModel.FirstName;
                user.LastName    = settingsViewModel.LastName;
                user.Affiliation = settingsViewModel.Affiliation;
                user.Biography   = settingsViewModel.Biography;
                user.DateOfBirth = settingsViewModel.DateOfBirth;

                db.Update(user);
                db.SaveChanges();

                return(View("Overview"));
            }
        }
        public async Task <IActionResult> FetchArticles()
        {
            using (var db = new CrossRefContext())
            {
                var user = db.Users.First(u => u.Email.Equals(
                                              User.FindFirst(ClaimTypes.Email).Value));

                var articles = await CrossRefService.FetchArticles(
                    user.FirstName + " " + user.LastName,
                    user.Affiliation);

                foreach (var article in articles)
                {
                    var existingArticle = db.Articles
                                          .Include("ArticleAuthors")
                                          .FirstOrDefault(a => a.DOI.Equals(article.DOI));

                    if (existingArticle != null)
                    {
                        if (existingArticle.ArticleAuthors.Any(aa => aa.UserId == user.Id))
                        {
                            continue;
                        }

                        var manyToManyBullshit = existingArticle.ArticleAuthors;
                        manyToManyBullshit.Add(new ArticleAuthors
                        {
                            User    = user,
                            Article = existingArticle
                        });

                        existingArticle.ArticleAuthors = manyToManyBullshit;
                        db.Articles.Update(existingArticle);
                        db.SaveChanges();
                    }
                    else
                    {
                        var newArticle = new Article
                        {
                            DOI               = article.DOI,
                            Title             = article.Title,
                            YearOfPublication = article.YearOfPublication
                        };

                        var aa = new ArticleAuthors
                        {
                            Article = newArticle,
                            User    = user
                        };
                        newArticle.ArticleAuthors = new List <ArticleAuthors> {
                            aa
                        };

                        db.Articles.Add(newArticle);
                        db.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("MyOverview", "Profile"));
        }