public async Task Execute(PangulDbContext db, DeleteTopic command) { command.Validate(); var topic = await _getTopic.Execute(db, new GetTopic() { TopicId = command.TopicId, RowVersion = command.RowVersion, UserContext = command.UserContext }); if (topic == null) { throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such topic ({command.DerivedProperties.TopicId})"); } // We cannot delete a topic if there are still attached questions var anyQuestions = await db.Question.AnyAsync(i => i.TopicId == command.DerivedProperties.TopicId); if (anyQuestions) { throw new PangulCommandFailedException(CommandFailureType.ConstraintFailed, $"Unable to delete topic {command.DerivedProperties.TopicId} while there are still attached questions"); } db.Topic.Remove(topic); }
public IActionResult Delete(int id, bool?saveChangesError = false) { if (saveChangesError.GetValueOrDefault()) { ViewBag.ErrorMessage = "Delete failed"; } var topic = _topicService.GetById(id); var model = new DeleteTopic { CategoryId = topic.Category.Id, TopicTitle = topic.Title }; if (topic.User.UserName != User.Identity.Name) { return(NotFound()); } if (topic == null) { return(NotFound()); } return(View(model)); }
public async Task <ApiResult> DeleteTopic(int topicId) { var requester = await GetRequestUserAsync(); var handler = new DeleteTopic(Db, _logger, _userManager); await handler.DeleteAsync(topicId, requester); return(new ApiResult()); }
public async Task DeleteAsync(DeleteTopic command) { var topic = await _dbContext.Posts .Include(x => x.CreatedByUser) .Include(x => x.Forum).ThenInclude(x => x.Category) .Include(x => x.Forum).ThenInclude(x => x.LastPost) .FirstOrDefaultAsync(x => x.Id == command.Id && x.TopicId == null && x.ForumId == command.ForumId && x.Forum.Category.SiteId == command.SiteId && x.Status != StatusType.Deleted); if (topic == null) { throw new DataException($"Topic with Id {command.Id} not found."); } topic.Delete(); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Deleted, typeof(Post), command.Id)); topic.Forum.DecreaseTopicsCount(); topic.Forum.Category.DecreaseTopicsCount(); topic.CreatedByUser.DecreaseTopicsCount(); if (topic.Forum.LastPost != null && (topic.Id == topic.Forum.LastPostId || topic.Id == topic.Forum.LastPost.TopicId)) { var newLastPost = await _dbContext.Posts .Where(x => x.ForumId == topic.ForumId && x.Status == StatusType.Published && (x.Topic == null || x.Topic.Status == StatusType.Published) && x.Id != topic.Id) .OrderByDescending(x => x.CreatedOn) .FirstOrDefaultAsync(); if (newLastPost != null) { topic.Forum.UpdateLastPost(newLastPost.Id); } else { topic.Forum.UpdateLastPost(null); } } await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Forum(topic.ForumId)); }
void Propagate(DeleteTopic request) { if (!string.IsNullOrEmpty(request.Cooperator)) { var client = new RestClient(request.Cooperator); var requestToSend = new RestRequest($"topics/{request.TopicName}", Method.DELETE); client.Execute(requestToSend); } }
public async Task <ActionResult> DeleteTopic(Guid forumId, Guid topicId) { var site = await _contextService.CurrentSiteAsync(); var user = await _contextService.CurrentUserAsync(); var command = new DeleteTopic { Id = topicId, ForumId = forumId, SiteId = site.Id, UserId = user.Id }; var topicUserId = await _dbContext.Posts .Where(x => x.Id == command.Id && x.TopicId == null && x.ForumId == command.ForumId && x.Forum.Category.SiteId == command.SiteId && x.Status != PostStatusType.Deleted) .Select(x => x.CreatedBy) .FirstOrDefaultAsync(); var permissions = await _permissionModelBuilder.BuildPermissionModelsByForumId(site.Id, forumId); var canDelete = _securityService.HasPermission(PermissionType.Delete, permissions); var canModerate = _securityService.HasPermission(PermissionType.Moderate, permissions); var authorized = (canDelete && topicUserId == user.Id || canModerate) && !user.IsSuspended; if (!authorized) { _logger.LogWarning("Unauthorized access to delete topic", new { SiteId = site.Id, ForumId = forumId, TopicId = topicId, User = User.Identity.Name }); return(Unauthorized()); } await _topicService.DeleteAsync(command); return(Ok()); }
public void Delete(DeleteTopic request) { var topicLock = Locks.TakeTopicLock(request.TopicName); lock (topicLock) { if (Locks.TopicsRecoveryLocks.ContainsKey(request.TopicName)) { throw new Exception($"Topic {request.TopicName} is inconsistent"); } Connections.RemoveTopic(request.TopicName); Monitor.PulseAll(topicLock); Propagators.ScheduleTopicOperation(request.TopicName, () => Propagate(request)); Locks.RemoveTopicLock(request.TopicName); } }
public static ResultState DeleteTopic(this DiscourseApi api, int topicId, string username = DefaultUsername) { var route = String.Format("/t/{0}", topicId); var data = new DeleteTopic(topicId); var result = api.ExecuteRequest <RestResponse>(route, Method.DELETE, true, username, null, data); switch (result.StatusCode) { case (HttpStatusCode)422: return(ResultState.Unchanged); case HttpStatusCode.Accepted: return(ResultState.Modified); default: return(ResultState.Error); } }