protected override void OnModelCreating(ModelBuilder modelBuilder) { var homePage = new Article { Id = 1, Topic = "HomePage", Slug = "home-page", Content = "This is the default home page. Please change me!", Published = Instant.FromDateTimeUtc(new DateTime(2018, 6, 19, 14, 31, 2, 265, DateTimeKind.Utc)), AuthorId = Guid.Empty }; var homePageHistory = ArticleHistory.FromArticle(homePage); homePageHistory.Id = 1; homePageHistory.Article = null; modelBuilder.Entity <Article>(entity => { entity.HasIndex(a => a.Slug).IsUnique(); entity.HasData(homePage); }); modelBuilder.Entity <ArticleHistory>(entity => { entity.HasData(homePageHistory); }); modelBuilder.Entity <SlugHistory>(entity => { entity.HasIndex(a => new { a.OldSlug, a.AddedDateTime }); }); }
protected void AddNotification(String text, ArticleHistory history, Boolean includeAdmins, params Int32[] recipientIds) { if (includeAdmins) { recipientIds = dbContext.User.Where(u => u.Status == UserStatus.ADMINISTRATOR) .Select(u => u.Id).Union(recipientIds).ToArray(); } var curUserId = GetCurrentUserId(); List <UserNotification> notifications = new List <UserNotification>(); foreach (var recipientId in recipientIds.Distinct()) { if (recipientId != curUserId) { var notification = new UserNotification(); notification.Text = text; notification.RecipientId = recipientId; notification.InsertDate = DateTime.Now; notification.ArticleHistory = history; notifications.Add(notification); } } dbContext.UserNotification.AddRange(notifications); senderHub.AddNotifications(notifications); }
protected void AddNotification(String text, ArticleHistory history, params Int32[] recipientIds) { if (recipientIds.Any()) { AddNotification(text, history, false, recipientIds); } }
public async Task <Article> CreateArticleAndHistory(Article article) { Context.Articles.Add(article); Context.ArticleHistories.Add(ArticleHistory.FromArticle(article)); await Context.SaveChangesAsync(); return(article); }
public override async Task AddAsync(Article article) { await base.AddAsync(article); this.UnitOfWork .Context .Set <ArticleHistory>() .Add(ArticleHistory.FromArticle(article)); }
private ArticleHistory SetHistory(int versionNum, Article article, string userId) { ArticleHistory articleHistory = new ArticleHistory() { VersionNum = versionNum, TagsOld = article.TagsId, ContentOld = article.Content, StateOld = article.State, ModifiedBy = userId, Timestamp = DateTime.Now.ToString("HH:mm:ss dd.MM.yyyy") }; return(articleHistory); }
public async Task UpdateAsync(Article article) { this.UnitOfWork .Context .Attach(article) .State = EntityState.Modified; var existingArticle = this.UnitOfWork.Context.Set <Article>() .AsNoTracking() .First(a => a.Id == article.Id); article.ViewCount = existingArticle.ViewCount; article.Version = existingArticle.Version + 1; article.Published = this.clock.GetCurrentInstant(); article.Slug = UrlHelpers.UrlFriendly(article.Topic.ToLower()); article.AuthorId = this.httpContextAccessor .HttpContext .User .FindFirstValue(ClaimTypes.NameIdentifier); this.UnitOfWork .Context .Set <ArticleHistory>() .Add(ArticleHistory.FromArticle(article)); try { await this.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { if (!this.IsArticleExistByTopic(article.Topic)) { throw new ArticleNotFoundException(ex.Message); } else { throw; } } }
protected ArticleHistory AddHistory(Int32 artId, Int32 authorId, String obj, String newVal, String oldVal, DateTime?insDate = null, Int32?objId = null) { var history = new ArticleHistory(); history.ArticleId = artId; history.AuthorId = authorId; history.Object = obj; history.NewValue = newVal; history.OldValue = oldVal; history.ObjectId = objId; var date = (insDate ?? DateTime.Now); history.InsertDate = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second); dbContext.ArticleHistory.Add(history); return(history); }
public async Task Update(Article article) { Context.Attach(article); //.State = EntityState.Modified; Context.ArticleHistories.Add(ArticleHistory.FromArticle(article)); try { await Context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!await Exists(article.Id)) { throw new ArticleNotFoundException(); } else { throw; } } }
public async void DeleteArticle(string articleId, string accountId) { ArticleDocumentDBRepository ArticleRepository = new ArticleDocumentDBRepository(); Article OldVersion = await ArticleRepository.Details(articleId); OldVersion.State = Article.Deleted; ArticleRepository.Update(articleId, OldVersion); ArticleHistoryDocumentDBRepository ArticleHistoryRepository = new ArticleHistoryDocumentDBRepository(); ArticleHistory ArticleHistory = ArticleHistoryRepository.GetAllListWhere(f => f.ArticleId == articleId).Result.FirstOrDefault(); ArticleHistory newHistory; if (ArticleHistory != null) { newHistory = SetHistory(ArticleHistory.VersionNum + 1, OldVersion, accountId); } else { newHistory = SetHistory(1, OldVersion, accountId); } ArticleHistoryRepository.Create(newHistory); }
public async void UpdateArticle(Article article, string accountId) { ArticleDocumentDBRepository ArticleRepository = new ArticleDocumentDBRepository(); Article OldVersion = await ArticleRepository.Details(article.Id); article.DateUpdated = DateTime.Now.ToString("HH:mm:ss dd.MM.yyyy"); ArticleRepository.Update(article.Id, article); ArticleHistoryDocumentDBRepository ArticleHistoryRepository = new ArticleHistoryDocumentDBRepository(); ArticleHistory ArticleHistory = ArticleHistoryRepository.GetAllListWhere(f => f.ArticleId == article.Id).Result.FirstOrDefault(); ArticleHistory newHistory; if (ArticleHistory != null) { newHistory = SetHistory(ArticleHistory.VersionNum + 1, OldVersion, accountId); } else { newHistory = SetHistory(1, OldVersion, accountId); } ArticleHistoryRepository.Create(newHistory); }
public ActionResult RemoveAmendment([FromQuery] Int32[] ids) { var amendments = dbContext.Amendment.Where(a => ids.Contains(a.Id)); DateTime now = DateTime.Now; ArticleHistory history = null; Article art = null; var curUser = GetUserInfo(); foreach (var amendment in amendments) { if (art == null) { art = amendment.Article; } if (curUser.status == UserStatus.ADMINISTRATOR && amendment.AuthorId != curUser.id) { return(BadRequest("Only authors can delete their own amendments")); } history = AddHistory(amendment.ArticleId, curUser.id, "Amendment Removed", null, null, now, amendment.Id); dbContext.Amendment.Remove(amendment); } if (history != null) { AddNotification($"Some amendments for an article '{art.Name}' have been removed by {curUser.name}", history, art.AuthorId); } dbContext.SaveChanges(); return(Ok()); }
public async Task <ActionResult> UpdateArticle([FromRoute] string articleId, [FromBody] CreateArticleDto dto) { var article = await _appDbContext.Articles.FirstOrDefaultAsync(i => i.Id == Guid.Parse(articleId)); var currentUser = await GetCurrentUser(); if (!article.Users.Contains(currentUser)) { article.Users.Add(currentUser); } var newHistory = new ArticleHistory() { Version = article.Histories.Count + 1, TimeStamp = DateTime.Now, Title = article.Title, Description = article.Description, HeroImage = article.HeroImage, Parts = article.Parts, Issues = article.Issues, Ratings = article.Ratings, Contributors = article.Users }; article.Histories.Add(newHistory); article.Title = dto.Title; article.Description = dto.Description; article.HeroImage = _mapper.Map <Image>(dto.HeroImage); article.Parts = _mapper.Map <List <ArticlePart> >(dto.Parts); await _appDbContext.SaveChangesAsync(); return(Ok()); }
public ActionResult UpdateAmendment(Amendment[] amendments) { if (!amendments.Any()) { throw new Exception("The request is empty"); } var articleId = amendments.First().ArticleId; var article = dbContext.Article.FirstOrDefault(a => a.Id == articleId); if (article == null) { return(BadRequest("The requested article does not exist in the data base")); } var curUser = GetUserInfo(); // If the user has the rights to get amendments if (article.AuthorId != curUser.id && article.AssignedToId != curUser.id) { return(BadRequest("The user cannot deal with these amendments")); } var ids = amendments.Select(am => am.Id).ToArray(); DateTime now = DateTime.Now; ArticleHistory history = null; foreach (var entity in dbContext.Amendment.Where(am => ids.Contains(am.Id))) { var amendment = amendments.First(am => am.Id == entity.Id); if (entity.Content != amendment.Content) { history = AddHistory(amendment.ArticleId, curUser.id, "Amendment.Content", amendment.Content, entity.Content, now, amendment.Id); } entity.Content = amendment.Content; if (entity.Resolved != amendment.Resolved) { history = AddHistory(amendment.ArticleId, curUser.id, "Amendment.Resolved", null, null, now, amendment.Id); } entity.Resolved = amendment.Resolved; } if (history != null) { var recipient = curUser.id == article.AuthorId ? (article.AssignedToId.HasValue ? article.AssignedToId : null) : article.AuthorId; if (recipient.HasValue) { AddNotification($"There have been changes to the amendments of an article '{article.Name}' by {curUser.name}", history, recipient.Value); } } dbContext.SaveChanges(); return(Ok()); }
public ActionResult UpdateArticle(Article article) { Article curArt; var curUser = GetUserInfo(); Int32 curUserId = curUser.id; DateTime now = DateTime.Now; Boolean newArticle = article.Id == 0; if (newArticle) { // Creating a new article with a current user as its author curArt = new Article(); curArt.AuthorId = curUserId; curArt.InsertDate = now; dbContext.Article.Add(curArt); } else { curArt = dbContext.Article.FirstOrDefault(a => a.Id == article.Id); if (curArt == null) { return(BadRequest("The requested article does not exist in the data base")); } } ArticleHistory history = null; if (!newArticle && curArt.Name != article.Name) { history = AddHistory(curArt.Id, curUserId, "Name", article.Name, curArt.Name, now); } curArt.Name = article.Name; if (!newArticle && curArt.Tags != article.Tags) { history = AddHistory(curArt.Id, curUserId, "Tags", article.Tags, curArt.Tags, now); } curArt.Tags = article.Tags; if (curArt.Status != ArticleStatus.ON_REVIEW && curArt.Status != ArticleStatus.ON_AMENDING) { if (!newArticle && !Enumerable.SequenceEqual(article.Content, curArt.Content)) { history = AddHistory(curArt.Id, curUserId, "Content", null, null, now); } curArt.Content = article.Content; } if (article.ReviewedContent != null) { curArt.ReviewedContent = article.ReviewedContent; } if (!newArticle && curArt.Description != article.Description) { history = AddHistory(curArt.Id, curUserId, "Description", article.Description, curArt.Description, now); } curArt.Description = article.Description; Int32?assignedToId = null; if (!newArticle && curArt.Status != article.Status) { history = AddHistory(curArt.Id, curUserId, "Status", article.Status.ToString(), curArt.Status.ToString(), now); curArt.Status = article.Status; if (article.Status == ArticleStatus.APPROVED || article.Status == ArticleStatus.DRAFT) { if (curArt.Amendments.Any()) { foreach (var am in curArt.Amendments) { am.Archived = true; } AddHistory(curArt.Id, curUserId, "Amendment.Archived", null, null, now); } curArt.ReviewedContent = null; if (curArt.AssignedToId.HasValue) { AddHistory(curArt.Id, curUserId, "AssignedTo", null, curArt.AssignedTo.Login, now); assignedToId = curArt.AssignedToId; curArt.AssignedToId = null; } if (article.Status == ArticleStatus.APPROVED) { AddNotification($"An article '{curArt.Name}' has been approved by {curUser.name} and published", history, true, curArt.AuthorId); } else { var recipients = new List <Int32>(); recipients.Add(curArt.AuthorId); if (assignedToId.HasValue) { recipients.Add(assignedToId.Value); } AddNotification($"All ammendments related to an article '{curArt.Name}' have been archived as it has been returned to the draft state", history, recipients.ToArray()); } } else if (article.Status == ArticleStatus.ON_EDIT && curArt.AssignedToId == curUserId) { AddNotification($"An article '{curArt.Name}' has been sent on edit by {curUser.name}", history, curArt.AuthorId); } else if (article.Status == ArticleStatus.ON_REVIEW && curArt.AuthorId == curUserId) { var text = $"An article '{curArt.Name}' has been sent on review by its author {curUser.name}"; if (curArt.AssignedToId.HasValue) { AddNotification(text, history, curArt.AssignedToId.Value); } else { AddNotification(text, history, true); } } // It's considered to be a new article if it has just been sent - needs a notification else if (article.Status == ArticleStatus.CREATED) { AddNotification($"{curUser.name} has added a new article '{curArt.Name}'", history, true); } } if (history != null && curArt.AssignedToId != null) { AddNotification($"An article '{curArt.Name}' has been changed by its author {curUser.name}", history, curArt.AssignedToId.Value); } dbContext.SaveChanges(); if (newArticle && article.Status != ArticleStatus.DRAFT) { history = AddHistory(curArt.Id, curUserId, "Created", null, null, null); curArt.Status = article.Status; AddNotification($"{curUser.name} has added a new article '{curArt.Name}'", history, true); dbContext.SaveChanges(); } return(Ok()); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { var homePage = new Article { Id = 1, Topic = "HomePage", Slug = "home-page", Content = "This is the default home page. Please change me!", Published = NodaTime.SystemClock.Instance.GetCurrentInstant(), AuthorId = Guid.NewGuid() }; var homePageHistory = ArticleHistory.FromArticle(homePage); homePageHistory.Id = 1; homePageHistory.Article = null; modelBuilder.Entity <Article>(entity => { entity.HasIndex(a => a.Slug).IsUnique(); entity.HasData(homePage); }); modelBuilder.Entity <ArticleHistory>(entity => { entity.HasData(homePageHistory); }); modelBuilder.Entity <SlugHistory>(entity => { entity.HasIndex(a => new { a.OldSlug, a.AddedDateTime }); }); modelBuilder.Entity <Customer>().HasData(new[] { new Customer { CustomerID = 1, CustomerName = "Skf", CreatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant(), UpdatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant() }, new Customer { CustomerID = 2, CustomerName = "ABB", CreatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant(), UpdatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant() }, new Customer { CustomerID = 3, CustomerName = "Yara", CreatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant(), UpdatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant() }, new Customer { CustomerID = 4, CustomerName = "Ericsson", CreatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant(), UpdatedAt = NodaTime.SystemClock.Instance.GetCurrentInstant() } }); }