public IArticle Add(Domain.Models.Article entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } //context.Sources.Attach((Source)entity.Source); var tagIds = entity.Tags != null ? entity.Tags.Select(x => x.Id) : new List <int>(); var sEntity = entity.ToPersistentEntity(); sEntity.Category = entity.Category != null ? context.Categories.Where(x => x.Id == entity.Category.Id).FirstOrDefault() : null; sEntity.Source = entity.Source != null ? context.Sources.Where(x => x.Id == entity.Source.Id).FirstOrDefault() : null; sEntity.SqlTags = context.Tags.Where(x => tagIds.Contains(x.Id)).Select(x => x).ToList(); var insertedRow = context.Articles.Add(sEntity); context.SaveChanges(); return(insertedRow.ToDomainEntity()); }
public IArticle Update(Domain.Models.Article entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } var sEntity = context.Articles .Include(x => x.SqlTags) .Where(x => x.Id == entity.Id) .FirstOrDefault(); if (sEntity == null) { return(null); } var tagsToInsert = entity.Tags .Select(x => x.Id) .ToList(); if (entity.Source != null) { sEntity.SourceId = entity.Source.Id; } if (entity.Category != null) { sEntity.CategoryId = entity.Category.Id; } sEntity.Author = entity.Author; sEntity.Title = entity.Title; sEntity.Summary = entity.Summary; sEntity.Url = entity.Url; sEntity.PublicationDate = entity.PublicationDate; sEntity.Content = entity.Content; sEntity.Synopsis = entity.Synopsis; sEntity.SqlTags = context.Tags .Where(x => tagsToInsert.Contains(x.Id)) .ToList(); sEntity.LastModifiedOn = DateTime.Now; context.SaveChanges(); return(GetById(sEntity.Id)); }