예제 #1
0
        public void AddBuying(BuyingAddDto buyingInfo)
        {
            #region Sql-Запрос.
            /*declare @CommentId uniqueidentifier
            if @Comment is not null and rtrim(@Comment) <> ''
            begin
              set @CommentId = newid()
            end

            insert into Buying (Id, Goods, Priority, InputDate, Comment)
            values (newid(), @Goods, @Priority, getdate(), @CommentId)

            if @CommentId is not null
            begin
              insert into Comments(Id, Description)
              values (@CommentId, @Comment)
            end*/
            #endregion

            using (EFUnitOfWork uow = new EFUnitOfWork("GoodsBuyingConnectionString"))
            using (var transaction = uow.BeginTransaction())
            {
                // Если комментарий есть, добавим его сначала.
                Comments comment = null;
                if (!string.IsNullOrWhiteSpace(buyingInfo.Comment))
                {
                    comment = new Comments { Description = buyingInfo.Comment };
                    uow.Comments.Create(comment);
                    uow.Save();
                }

                Dal.Entities.Buying buying = new Dal.Entities.Buying
                {
                    Goods = buyingInfo.Goods,
                    Priority = buyingInfo.Priority,
                    Comment = comment != null ? comment.Id : (Guid?)null
                };
                uow.Buyings.Create(buying);
                // Обновим дату синхронизации.
                UpdateSyncDate(uow);
                uow.Save();
                transaction.Commit();
            }
        }
예제 #2
0
        public void AddGoods(GoodsAddDto goodsInfo)
        {
            #region Sql-запрос.
            /*if not exists(select 1 from Goods where Name = @Name)
            begin
              insert into Goods (Id, Name) values (newid(), @Name)
            end*/
            #endregion

            using (EFUnitOfWork uow = new EFUnitOfWork("GoodsBuyingConnectionString"))
            using (var transaction = uow.BeginTransaction())
            {
                if (uow.Goods.GetAll().Where(v => v.Name == goodsInfo.Name).Count() == 0)
                {
                    Goods goods = new Goods { Name = goodsInfo.Name };
                    uow.Goods.Create(goods);
                    uow.Save();
                }
                transaction.Commit();
            }
        }
예제 #3
0
        public void ChangeGoods(GoodsChangeDto goodsInfo)
        {
            #region Sql-запрос.
            /*if exists(select 1 from Goods where Id = @Id)
            begin
              update Goods
              set
                Name = @Name
              where Id = @Id
            end*/
            #endregion

            using (EFUnitOfWork uow = new EFUnitOfWork("GoodsBuyingConnectionString"))
            using (var transaction = uow.BeginTransaction())
            {
                Goods goods = uow.Goods.Get(goodsInfo.Id);

                if (goods != null)
                {
                    goods.Name = goodsInfo.NewName;
                    uow.Save();
                }

                transaction.Commit();
            }
        }
예제 #4
0
        public void DeleteGoods(Guid id)
        {
            #region Sql-Запрос.
            /*if exists(select 1 from Goods where Id = @Id)
            and not exists(select 1 from Buying where Goods = @Id)
            begin
              delete from Goods
              where Id = @Id
            end*/
            #endregion

            using (EFUnitOfWork uow = new EFUnitOfWork("GoodsBuyingConnectionString"))
            using (var transaction = uow.BeginTransaction())
            {
                Goods goods = uow.Goods.Get(id);
                var checkBuying = uow.Buyings.GetAll().FirstOrDefault(v => v.Goods == id);

                if (goods != null && checkBuying == null)
                {
                    uow.Goods.Delete(goods.Id);
                    uow.Save();
                }

                transaction.Commit();
            }
        }
예제 #5
0
        public void DeleteBuying(Guid id)
        {
            #region Sql-Запрос.
            /*delete from Buying where Id = cast(@Id as uniqueidentifier)
            delete Comments
            from Comments
              left join Buying on Comments.Id = Buying.Comment
            where Buying.Id is null*/
            #endregion

            using (EFUnitOfWork uow = new EFUnitOfWork("GoodsBuyingConnectionString"))
            using (var transaction = uow.BeginTransaction())
            {
                // Удаление из таблицы покупок.
                uow.Buyings.Delete(id);
                // Обновим дату синхронизации.
                UpdateSyncDate(uow);
                uow.Save();

                // Удаление из таблицы комментариев.
                // Используем неотложенную операцию.
                var deleteComments = (from c in uow.Comments.GetAll()
                                     join b in uow.Buyings.GetAll() on c.Id equals b.Comment into leftBuyings
                                     from subBuying in leftBuyings.DefaultIfEmpty()
                                     where subBuying == null
                                     select c.Id).ToArray();
                if (deleteComments.Count() > 0)
                {
                    foreach (var c in deleteComments)
                    {
                        uow.Comments.Delete(c);
                    }
                    uow.Save();
                }

                transaction.Commit();
            }
        }