public async Task <int> Handle(UpsertContentCommand request, CancellationToken cancellationToken) { if ((await _context.Topics.FindAsync(request.TopicId)) == null) { throw new BadRequestException($"{nameof(request.TopicId)} {request.TopicId} is invalid."); } Content content; if (request.Id.HasValue) { content = await _context.Contents.FindAsync(request.Id.Value); if (content == null) { throw new NotFoundException(nameof(Content), request.Id); } } else { content = new Content(); _context.Contents.Add(content); } content.TopicId = request.TopicId; content.Title = request.Title; content.Description = request.Description; content.HTMLBody = request.HTMLBody; content.Tags = request.Tags; await _context.SaveChangesAsync(cancellationToken); return(content.ContentId); }
public async Task <int> Handle(UpsertCategoryCommand request, CancellationToken cancellationToken) { Category category; if (request.Id.HasValue) { category = await _context.Categories.FindAsync(request.Id.Value); if (category == null) { throw new NotFoundException(nameof(Category), request.Id); } } else { category = new Category(); _context.Categories.Add(category); } category.Title = request.Title; category.Description = request.Description; await _context.SaveChangesAsync(cancellationToken); return(category.CategoryId); }
public async Task <int> Handle(UpsertTopicCommand request, CancellationToken cancellationToken) { if ((await _context.Categories.FindAsync(request.CategoryId)) == null) { throw new BadRequestException($"{nameof(request.CategoryId)} {request.CategoryId} is invalid."); } Topic topic; if (request.Id.HasValue) { topic = await _context.Topics.FindAsync(request.Id.Value); if (topic == null) { throw new NotFoundException(nameof(Topic), request.Id); } } else { topic = new Topic(); _context.Topics.Add(topic); } topic.Title = request.Title; topic.Description = request.Description; topic.CategoryId = request.CategoryId; await _context.SaveChangesAsync(cancellationToken); return(topic.TopicId); }
private async Task SeedCategoriesAsync(CancellationToken cancellationToken) { if (_context.Categories.Any()) { return; } _context.Categories.Add(new Category() { Title = "Uncategorised", Description = "Default category." }); _context.Categories.Add(new Category() { Title = "Sample Category", Description = "A sample category containing sample topics." }); await _context.SaveChangesAsync(cancellationToken); }
public async Task <Unit> Handle(DeleteContentCommand request, CancellationToken cancellationToken) { Content content = await _context.Contents.FindAsync(request.Id); if (content == null) { throw new NotFoundException(nameof(content), request.Id); } _context.Contents.Remove(content); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken) { Category category = await _context.Categories.FindAsync(request.Id); if (category == null) { throw new NotFoundException(nameof(Category), request.Id); } var hasTopics = _context.Topics.Any(t => t.CategoryId == category.CategoryId); if (hasTopics) { throw new DeleteFailureException(nameof(category), request.Id, "Category not empty."); } _context.Categories.Remove(category); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteTopicCommand request, CancellationToken cancellationToken) { Topic topic = await _context.Topics.FindAsync(request.Id); if (topic == null) { throw new NotFoundException(nameof(Topic), request.Id); } var hasContents = _context.Contents.Any(t => t.TopicId == topic.TopicId); if (hasContents) { throw new DeleteFailureException(nameof(topic), request.Id, "Topic not empty."); } _context.Topics.Remove(topic); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <IActionResult> SuccessfulLoginFrom(string provider, UserInformation userInfo, HttpContext httpContext) { var loginUser = _dbContext.Users .Include(u => u.AuthAccounts) .FindUserWithProvider(userInfo.Id, provider); // User is already logged in if (httpContext.User.Identity.IsAuthenticated) { var loggedInUserProvider = httpContext.User.Claims.FindStringClaimValue("LoginProvider"); if (loggedInUserProvider == provider) { // User has tried to log in twice with same provider return(new RedirectToActionResult("Index", "Home", null)); } // Modify logged in user with info from new provider login var loggedInUserId = httpContext.User.Claims.FindIntClaimValue("UserId"); if (loginUser != null && loggedInUserId == loginUser.Id) { // User has tried to log in twice with two of their registered providers return(new RedirectToActionResult("Index", "Home", null)); } var loggedInUser = _dbContext.Users .Include(u => u.AuthAccounts) .FirstOrDefault(u => u.Id == loggedInUserId); loggedInUser.LastLoggedIn = DateTime.Now; // Add this auth account to existing user if (loginUser == null) { // Add new auth account to logged in user loggedInUser.AuthAccounts.Add(new UserAuthAccount { Id = userInfo.Id, Provider = provider, UserName = userInfo.UserName }); } else { // Merge accounts (in this implementation the only useful stuff is the auth accounts) var mergeAuthAccounts = loginUser.AuthAccounts.Except(loggedInUser.AuthAccounts).ToList(); loginUser.AuthAccounts.Clear(); foreach (var authAccount in mergeAuthAccounts) { loggedInUser.AuthAccounts.Add(authAccount); } // Remove old user we've grabbed stuff from (one from latest login) _dbContext.Users.Remove(loginUser); } await _dbContext.SaveChangesAsync(); return(new RedirectToActionResult("Index", "Home", null)); } // New login else if (loginUser == null) { loginUser = new User { Name = userInfo.Name }; loginUser.AuthAccounts.Add(new UserAuthAccount { Id = userInfo.Id, Provider = provider, UserName = userInfo.UserName }); _dbContext.Users.Add(loginUser); } loginUser.LastLoggedIn = DateTime.Now; await _dbContext.SaveChangesAsync(); SignUserIn(loginUser, provider, httpContext); return(new RedirectToActionResult("Index", "Home", null)); }