public async Task Should_move_forum_down_and_add_events() { var options = Shared.CreateContextOptions(); var categoryId = Guid.NewGuid(); var siteId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum1 = new Forum(categoryId, "Forum 1", "my-forum-1", "Forum 1", 1); var forum2 = new Forum(categoryId, "Forum 2", "my-forum-2", "Forum 2", 2); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum1); dbContext.Forums.Add(forum2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = new MoveForum { Id = forum1.Id, Direction = Direction.Down, SiteId = siteId }; var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateForum> >(); var updateValidator = new Mock <IValidator <UpdateForum> >(); var sut = new ForumService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.MoveAsync(command); var updatedForum1 = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum1.Id); var updatedForum2 = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum2.Id); var event1 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum1.Id); var event2 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum2.Id); Assert.AreEqual(forum2.SortOrder, updatedForum1.SortOrder); Assert.AreEqual(forum1.SortOrder, updatedForum2.SortOrder); Assert.NotNull(event1); Assert.NotNull(event2); } }
public async Task <ActionResult> MoveDown([FromBody] Guid id) { var site = await _contextService.CurrentSiteAsync(); var user = await _contextService.CurrentUserAsync(); var command = new MoveForum { Id = id, SiteId = site.Id, UserId = user.Id, Direction = Direction.Down }; await _forumService.MoveAsync(command); return(Ok()); }
public async Task MoveAsync(MoveForum command) { var forum = await _dbContext.Forums .FirstOrDefaultAsync(x => x.Category.SiteId == command.SiteId && x.Id == command.Id && x.Status != ForumStatusType.Deleted); if (forum == null) { throw new DataException($"Forum with Id {command.Id} not found."); } if (command.Direction == Direction.Up) { forum.MoveUp(); } else if (command.Direction == Direction.Down) { forum.MoveDown(); } _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Reordered, typeof(Forum), forum.Id, new { forum.SortOrder })); var sortOrderToReplace = forum.SortOrder; var adjacentForum = await _dbContext.Forums .FirstOrDefaultAsync(x => x.CategoryId == forum.CategoryId && x.SortOrder == sortOrderToReplace && x.Status != ForumStatusType.Deleted); if (command.Direction == Direction.Up) { adjacentForum.MoveDown(); } else if (command.Direction == Direction.Down) { adjacentForum.MoveUp(); } _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Reordered, typeof(Forum), adjacentForum.Id, new { adjacentForum.SortOrder })); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Categories(command.SiteId)); }