public ArticleGroup Read(int id) { return(ExecuteWithTry(() => { using (Container = new ArticleContext()) { var groupDb = Container.Groups.FirstOrNothing(c => c.GroupId == id); TransactionHelper.AddTransaction(Container, ActionType.Read, groupDb, _currentUser); return groupDb.Bind(Mapper.Map <Group, ArticleGroup>).GetOrDefault(null); } })); }
public int Save(IPlan entity, bool now = true) { try { if (entity == null) { return(-1); } using (Container = new ArticleContext()) { var current = entity as CurrentPlan; var planDb = Mapper.Map <CurrentPlan, Plan>(current); if (Container.Plans.FirstOrDefault(c => c.Name == entity.Name) != null) { throw new Exception("Найден план с таким же названием"); } Container.Plans.Add(planDb); if (now) { Container.SaveChanges(); } entity.Id = planDb.Id; TransactionHelper.AddTransaction(Container, ActionType.Adding, planDb, _currentUser); if (now) { Container.SaveChanges(); } return(planDb.Id); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public int Save(ITag entity, bool now = true) { try { if (entity == null) { return(-1); } using (Container = new ArticleContext()) { var tagDb = Mapper.Map <Tag, Entities.Tag>(entity as Tag); if (Container.Tags.FirstOrDefault(c => c.Name == tagDb.Name) != null) { throw new Exception("Найден тэг с таким же именем.Тэг не будет сохранен."); } Container.Tags.Add(tagDb); if (now) { Container.SaveChanges(); } entity.Id = tagDb.Id; TransactionHelper.AddTransaction(Container, ActionType.Adding, tagDb, _currentUser); if (now) { Container.SaveChanges(); } return(tagDb.Id); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public int Update(ArticleGroup entity) { if (entity == null || entity.GroupId == 0) { return(-1); } return(ExecuteWithTry(() => { using (Container = new ArticleContext()) { var fromDb = Container.Groups.FirstOrDefault(c => c.GroupId == entity.GroupId); fromDb.GroupName = entity.GroupName; TransactionHelper.AddTransaction(Container, ActionType.Updating, fromDb, _currentUser); Container.SaveChanges(); return fromDb.GroupId; } })); }
public IEnumerable <ArticleGroup> SaveMany(IEnumerable <ArticleGroup> entities) { return(ExecuteWithTry(() => { using (Container = new ArticleContext()) { var enumerable = entities as ArticleGroup[] ?? entities.ToArray(); foreach (var group in enumerable) { var groupDb = Mapper.Map <ArticleGroup, Group>(group); TransactionHelper.AddTransaction(Container, ActionType.Adding, groupDb, _currentUser); Container.Groups.Add(groupDb); } Container.SaveChanges(); return enumerable; } })); }
public void Delete(Article entity, bool now = true) { try { if (entity == null) { return; } using (Container = new ArticleContext()) { var artDb = Container.Articles.FirstOrNothing(c => c.ArticleId == entity.ArticleId); if (artDb != Maybe.Nothing) { var deletingList = artDb.Value.EmbdedImages.Where(embdedImage => embdedImage != null).ToList(); foreach (var embdedImage in deletingList) { TransactionHelper.AddTransaction(Container, ActionType.Deleting, embdedImage, _currentUser); Container.EmbdedImages.Remove(embdedImage); } TransactionHelper.AddTransaction(Container, ActionType.Deleting, artDb.Value, _currentUser); Container.Articles.Remove(artDb.Value); if (now) { Container.SaveChanges(); } } } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw ex; } catch (Exception e) { throw e; } }
public int Update(ITag entity) { try { if (entity == null) { return(-1); } using (Container = new ArticleContext()) { var tagDb = Container.Tags.FirstOrDefault(c => c.Id == entity.Id); tagDb.Name = entity.Name; tagDb.TagColor = entity.TagColor; tagDb.Priority = entity.Priority; tagDb.TagType = entity.TagType; Container.SaveChanges(); TransactionHelper.AddTransaction(Container, ActionType.Updating, tagDb, _currentUser); Container.SaveChanges(); return(tagDb.Id); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public List <string> Import(bool isDb, byte[] data) { if (!isDb) { throw new Exception("Выбран другой источник данных"); } using (Container = new ArticleContext()) { TransactionHelper.AddTransaction(Container, ActionType.Import, null, _currentUser); Container.SaveChanges(); } using (var stream = new MemoryStream(data)) { var document = XDocument.Load(stream); return(MergeAndImport(document)); } }
public ITag Read(int id) { try { if (id <= 0) { return(null); } using (Container = new ArticleContext()) { var artDb = Container.Tags.FirstOrNothing(c => c.Id == id); if (artDb.HasValue) { TransactionHelper.AddTransaction(Container, ActionType.Read, artDb.Value, _currentUser); //foreach (var embdedImage in artDb.Value.EmbdedImages) //{ // TransactionHelper.AddTransaction(Container, ActionType.Read, embdedImage); //} Container.SaveChanges(); } return(artDb.Bind(Mapper.Map <Entities.Tag, FWD.BusinessObjects.Domain.Tag>).GetOrDefault(null)); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public int Save(ArticleGroup entity, bool now = true) { return(ExecuteWithTry(() => { using (Container = new ArticleContext()) { var existings = Container.Groups.FirstOrDefault(c => c.GroupName == entity.GroupName); if (existings != null) { throw new Exception("Группа с именем '{0}' уже существует".Fmt(existings.GroupName)); } var result = Mapper.Map <Group>(entity); TransactionHelper.AddTransaction(Container, ActionType.Adding, result, _currentUser); Container.Groups.Add(result); if (now) { Container.SaveChanges(); } return result.GroupId; } })); }
public void Delete(Comment entity, bool now = true) { try { if (entity == null) { return; } using (Container = new ArticleContext()) { var commentDb = Container.Comments.FirstOrNothing(c => c.CommentId == entity.CommentId); if (commentDb != Maybe.Nothing) { var user = Container.Users.First(c => c.UserId == commentDb.Value.UserId); TransactionHelper.AddTransaction(Container, ActionType.Deleting, commentDb.Value, user); Container.Comments.Remove(commentDb.Value); if (now) { Container.SaveChanges(); } } } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception) { throw; } }
public int Update(IPlan entity) { try { if (entity == null || entity.Id == 0) { return(-1); } using (Container = new ArticleContext()) { var fromDb = Container.Plans.FirstOrDefault(c => c.Id == entity.Id); fromDb.Name = entity.Name; fromDb.Description = entity.Description; fromDb.PossibleChangeDate = DateTime.Now; fromDb.IsDone = entity.IsDone; TransactionHelper.AddTransaction(Container, ActionType.Updating, fromDb, _currentUser); Container.SaveChanges(); return(fromDb.Id); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw ex; } catch (Exception e) { throw e; } }
public int Save(Comment entity, bool now = true) { try { if (entity == null) { return(-1); } using (Container = new ArticleContext()) { var commDb = Mapper.Map <Comment, Entities.Comment>(entity); var user = Container.Users.First(c => c.UserId == commDb.UserId); TransactionHelper.AddTransaction(Container, ActionType.Adding, commDb, user); Container.Comments.Add(commDb); if (now) { Container.SaveChanges(); } return(0); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public int Save(Article entity, bool now = true) { try { if (entity == null) { return(-1); } using (Container = new ArticleContext()) { entity.Tags = entity.Tags .GroupBy(c => c.Name) .Select(c => c.First()) .ToList(); var artDb = Mapper.Map <Article, Entities.Article>(entity); if (Container.Articles.FirstOrDefault(c => c.Link == artDb.Link) != null) { throw new Exception("Найдена статья с таким же источником. Статья не сохранена."); } if (artDb.Group == null && entity.GroupId == -1) { artDb.Group = Container.Groups.First(c => c.GroupId == 1); } else { var groupName = entity.ArticleGroup.MaybeAs <BusinessObjects.Domain.ArticleGroup>() .Bind(c => c.GroupName) .GetOrDefault(string.Empty); var existingGroup = Container.Groups .FirstOrDefault(c => c.GroupName == groupName || c.GroupId == entity.GroupId); artDb.Group = existingGroup ?? artDb.Group; } if (entity.Tags != null) { var currentTagsIDs = entity.Tags .Select(x => x.Id) .ToList(); artDb.Tags = Container.Tags .Where(c => currentTagsIDs.Contains(c.Id)) .ToList(); } foreach (var embdedImage in artDb.EmbdedImages) { TransactionHelper.AddTransaction(Container, ActionType.Adding, embdedImage, _currentUser); } artDb.CreationDate = DateTime.Now; Container.Articles.Add(artDb); if (now) { Container.SaveChanges(); } entity.ArticleId = artDb.ArticleId; if (entity.Images != null && entity.Images.Any()) { var ids = artDb.EmbdedImages .Select(c => new { Name = c.Name, Id = c.ImageId }); entity.Images.Zip(ids, (a, b) => { a.ImageId = b.Id; return(a); }); } TransactionHelper.AddTransaction(Container, ActionType.Adding, artDb, _currentUser); if (now) { Container.SaveChanges(); } return(artDb.ArticleId); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw; } catch (Exception e) { throw; } }
public int Update(Article entity) { try { if (entity == null || entity.ArticleId == 0) { return(-1); } using (Container = new ArticleContext()) { var fromDb = Container.Articles.FirstOrDefault(c => c.ArticleId == entity.ArticleId); var groupName = entity.ArticleGroup.MaybeAs <BusinessObjects.Domain.ArticleGroup>().Bind(c => c.GroupName).GetOrDefault(string.Empty); var existingGroup = Container.Groups.FirstOrDefault(c => c.GroupName == groupName); fromDb.ArticleName = entity.ArticleName; fromDb.InitialText = entity.InitialText; fromDb.Link = entity.Link; fromDb.AuthorName = entity.AuthorName; fromDb.Group = existingGroup ?? Mapper.Map <Entities.ArticleGroup>(entity.ArticleGroup); fromDb.Rate = entity.Rate; if (entity.Tags != null) { var currentTags = Mapper.Map <IEnumerable <FWD.BusinessObjects.Domain.Tag>, IEnumerable <Entities.Tag> >(entity.Tags).ToList(); var matched = (currentTags.Select( tag => new { tag, matched = fromDb.Tags.FirstOrDefault(c => c.Name == tag.Name) }) .Where(@t => @t.matched != null) .Select(@t => @t.tag)).ToList(); if (fromDb.Tags.Count > currentTags.Count) { var removeList = fromDb.Tags .Select(tag => new { tag, res = currentTags.FirstOrDefault(c => c.Id == tag.Id) }) .Where(@t => @t.res == null) .Select(@t => @t.tag) .ToList(); foreach (var tag in removeList) { fromDb.Tags.Remove(tag); } } foreach (var tag in matched) { currentTags.Remove(tag); } foreach (var tag in currentTags) { var fromDbTag = Container.Tags.FirstOrDefault(c => c.Id == tag.Id); fromDb.Tags.Add(fromDbTag); } } TransactionHelper.AddTransaction(Container, ActionType.Updating, fromDb, _currentUser); var embdedImage = Mapper.Map <IEnumerable <Image>, IEnumerable <EmbdedImage> >(entity.Images).ToList(); var lst = (embdedImage.Select( image => new { image, matched = fromDb.EmbdedImages.FirstOrDefault(c => c.Name == image.Name) }) .Where(@t => @t.matched != null) .Select(@t => @t.image)).ToList(); if (embdedImage.Count < fromDb.EmbdedImages.Count) { var removeList = fromDb.EmbdedImages .Select(image => new { image, res = embdedImage.FirstOrDefault(c => c.ImageId == image.ImageId) }) .Where(@t => @t.res == null) .Select(@t => @t.image) .ToList(); foreach (var image in removeList) { TransactionHelper.AddTransaction(Container, ActionType.Deleting, image, _currentUser); fromDb.EmbdedImages.Remove(image); Container.EmbdedImages.Remove(image); } } foreach (var image in lst) { embdedImage.Remove(image); } foreach (var image in embdedImage) { TransactionHelper.AddTransaction(Container, ActionType.Adding, image, _currentUser); fromDb.EmbdedImages.Add(image); } Container.SaveChanges(); return(fromDb.ArticleId); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw ex; } catch (Exception e) { throw e; } }
public IEnumerable <Article> SaveMany(IEnumerable <Article> entities) { try { var builder = new StringBuilder(); using (Container = new ArticleContext()) { var list = new List <Entities.Article>(); foreach (var article in entities) { var duplicated = Container.Articles.FirstOrDefault(c => c.Link == article.Link); if (duplicated == null) { var artDb = Mapper.Map <Article, Entities.Article>(article); var group = artDb.Group ?? Container.Groups. FirstOrDefault(c => c.GroupId == article.GroupId); artDb.Group = group ?? Container.Groups.First(); if (article.Tags != null && article.Tags.Count > 0) { var currentTagsIDs = article.Tags .Select(x => x.Name) .ToList(); artDb.Tags = Container.Tags .Where(c => currentTagsIDs.Contains(c.Name)) .ToList(); } artDb.CreationDate = DateTime.Now; list.Add(artDb); TransactionHelper.AddTransaction(Container, ActionType.Adding, artDb, _currentUser); foreach (var embdedImage in artDb.EmbdedImages) { TransactionHelper.AddTransaction(Container, ActionType.Adding, embdedImage, _currentUser); } Container.Articles.Add(artDb); } else { builder.Append("Найдена повторяющиеся статья для \"{0}\". Статья не сохранена.".Fmt(duplicated.ArticleName)); } } Container.SaveChanges(); if (!string.IsNullOrEmpty(builder.ToString())) { throw new Exception(builder.ToString()); } return(list.Zip(entities, (db, bo) => new Article { ArticleId = db.ArticleId, InitialText = db.InitialText, Link = db.Link, ArticleName = db.ArticleName, AuthorName = db.AuthorName, Images = Mapper.Map <IEnumerable <EmbdedImage>, List <Image> >(db.EmbdedImages), Rate = db.Rate, CreationDate = db.CreationDate, GroupId = db.Group.GroupId, //ArticleGroup = Mapper.Map<Entities.ArticleGroup, ArticleGroup>(db.Group), //Tags = Mapper.Map<IEnumerable<Entities.Tag>, List<Tag>>(db.Tags), })); } } catch (DbEntityValidationException ex) { foreach (var dbEntityValidationResult in ex.EntityValidationErrors) { foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { Console.WriteLine(dbValidationError.ErrorMessage); } } throw ex; } catch (Exception e) { throw e; } }