public async Task <ActionResult> Settings(SettingsViewModel _users)
        {
            for (int i = 0; i < _users.UserToRemove.Count; i++)
            {
                var entries = _context.Entries.Where(entry => entry.UserId == _users.UserToRemove[i]).ToList();

                for (int y = 0; y < entries.Count; y++)
                {
                    var comments = _context.Comments.Where(entry => entry.EntryId == entries[y].EntryId).ToList();

                    for (int z = 0; z < comments.Count; z++)
                    {
                        _context.Comments.Remove(comments[z]);
                    }

                    _context.Entries.Remove(entries[y]);
                }

                _context.SaveChanges();

                var user = await _userManager.FindByIdAsync(_users.UserToRemove[i]);

                var rolesForUser = await _userManager.GetRolesAsync(user);

                await _userManager.RemoveFromRoleAsync(user, "Admin");

                await _userManager.DeleteAsync(user);
            }

            return(RedirectToAction("Settings"));
        }
Exemple #2
0
        public IActionResult CreateEntry(EntryPublishViewModel _entry, string _newCategory)
        {
            if (ModelState.IsValid)
            {
                if ((_entry.Entry.Headline.Length > 0 && _entry.Entry.Headline.Length <= 50) && (_entry.Entry.Text.Length > 0 && _entry.Entry.Text.Length <= 2000))
                {
                    _entry.Entry.DateOfCreation = DateTime.Now;
                    _entry.Entry.UserId         = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

                    _entry.Entry.Text.Replace("\n", "<br />\n");


                    if (_newCategory != null)
                    {
                        if (_context.Categories.SingleOrDefault(category => category.CategoryName == _newCategory) == null)
                        {
                            Category category = new Category();
                            category.CategoryName = _newCategory;

                            _context.Categories.Add(category);
                            _context.SaveChanges();

                            _entry.Entry.CategoryId = category.CategoryId;
                        }
                    }

                    _context.Entries.Add(_entry.Entry);
                    _context.SaveChanges();

                    int _entryId = _context.Entries.OrderByDescending(p => p.EntryId).FirstOrDefault().EntryId;

                    return(RedirectToAction("EntryDetails", "Entry", new { _entryId = _entryId }));
                }
                else
                {
                    TempData["Error"] = "Titlen måste vara mellan 1-50 tecken och texten mellan 1-2000 tecken.";
                    return(RedirectToAction("CreateEntry", "PublishEntry"));
                }
            }
            else
            {
                return(View());
            }
        }
Exemple #3
0
        public IActionResult CreateComment(string _comment, int _entryId)
        {
            if (_comment != null && _comment.Length < 1000)
            {
                Comment comment = new Comment();
                comment.Text           = _comment;
                comment.DateOfCreation = DateTime.Now;
                comment.EntryId        = _entryId;
                comment.UserId         = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

                _context.Comments.Add(comment);
                _context.SaveChanges();
            }
            else
            {
                TempData["Error"] = "Kommentaren måste vara mellan 1 till 1000 tecken.";
            }

            return(RedirectToAction("EntryDetails", new { _entryId = _entryId }));
        }