public SellOfferWrapper(sell_Offer s)
 {
     sellOffer    = s;
     Price        = (decimal?)s.price;
     Amount       = s.amount;
     LastSellDate = s.last_sell_date;
     Name         = s.name;
     BoughtCopies = s.buyed_copies;
 }
        protected virtual void NullProperties(sell_Offer sellOffer, buy_Offer buyOffer)
        {
            sellOffer.dic_Offer_status = null;
            sellOffer.users1           = null;
            sellOffer.users            = null;
            sellOffer.product          = null;

            buyOffer.dic_Offer_status = null;
            buyOffer.product          = null;
            buyOffer.users            = null;
            buyOffer.users1           = null;
        }
        public async Task <ErrorValue> AcceptBuyTransaction(sell_Offer sellOffer, buy_Offer buyOffer, int?rating)
        {
            using (var unitOfWork = _factory.CreateUnitOfWork())
            {
                try
                {
                    unitOfWork.StartTransaction();
                    NullProperties(sellOffer, buyOffer);
                    buyOffer = await unitOfWork.BuyOfferRepository.GetById(buyOffer.ID);

                    if (buyOffer.status_id == 3)
                    {
                        return(ErrorValue.TransactionAlreadyFinished);
                    }
                    var soldProduct = await GetSoldProduct(sellOffer, buyOffer, unitOfWork);

                    if (soldProduct == null)
                    {
                        return(ErrorValue.AmountGreaterThanStock);
                    }
                    sellOffer.product = soldProduct;
                    UpdateOffers(sellOffer, buyOffer, unitOfWork);
                    await _ratingUpdater.UpdateRating(buyOffer.buyer_id, unitOfWork, rating);

                    var transaction = new transactions()
                    {
                        buyer_id         = buyOffer.buyer_id,
                        seller_id        = sellOffer.seller_id,
                        sell_Offer       = sellOffer,
                        buy_offer_id     = buyOffer.ID,
                        status_id        = (int)TransactionState.Finished,
                        transaction_Date = DateTime.Now,
                        Update_Who       = sellOffer.seller_id,
                        Update_Date      = DateTime.Now,
                        Rating           = rating
                    };

                    unitOfWork.TransactionRepository.Add(transaction);
                    await unitOfWork.Save();

                    unitOfWork.Commit();
                }
                catch (Exception)
                {
                    unitOfWork.Rollback();
                    return(ErrorValue.ServerError);
                }
            }
            return(ErrorValue.NoError);
        }
        private void UpdateOffers(sell_Offer sellOffer, buy_Offer buyOffer, IUnitOfWork unitOfWork)
        {
            buyOffer.status_id  = (int)TransactionState.Finished;
            sellOffer.status_id = (int)TransactionState.Finished;

            if (buyOffer.ID != 0)
            {
                unitOfWork.BuyOfferRepository.Update(buyOffer);
            }
            else if (sellOffer.ID != 0)
            {
                unitOfWork.SellOfferRepository.Update(sellOffer);
            }
        }
        public async Task <ErrorValue> AcceptSellTransaction(sell_Offer sellOffer, buy_Offer buyOffer, int?rating)
        {
            using (var unitOfWork = _factory.CreateUnitOfWork())
            {
                try
                {
                    unitOfWork.StartTransaction();
                    NullProperties(sellOffer, buyOffer);
                    //get actual sellOffer from database
                    sellOffer = await unitOfWork.SellOfferRepository.GetById(sellOffer.ID);

                    if (sellOffer.status_id == 3)
                    {
                        return(ErrorValue.TransactionAlreadyFinished);
                    }
                    buyOffer.product_id = sellOffer.product_id;
                    //set offers to finished
                    UpdateOffers(sellOffer, buyOffer, unitOfWork);
                    //update product stocks
                    var boughtProduct = await GetBoughtProduct(sellOffer, buyOffer, unitOfWork);

                    //change user rating
                    await _ratingUpdater.UpdateRating(sellOffer.seller_id, unitOfWork, rating);

                    var transaction = new transactions()
                    {
                        buyer_id         = buyOffer.buyer_id,
                        seller_id        = sellOffer.seller_id,
                        buy_Offer        = buyOffer,
                        sell_offer_id    = sellOffer.ID,
                        status_id        = (int)TransactionState.Finished,
                        transaction_Date = DateTime.Now,
                        Update_Who       = buyOffer.buyer_id,
                        Update_Date      = DateTime.Now,
                        Rating           = rating
                    };
                    unitOfWork.TransactionRepository.Add(transaction);
                    await unitOfWork.Save();

                    unitOfWork.Commit();
                }
                catch (Exception)
                {
                    unitOfWork.Rollback();
                    return(ErrorValue.ServerError);
                }
            }
            return(ErrorValue.NoError);
        }
Пример #6
0
        private async Task <product> GetSoldProduct(sell_Offer sellOffer, buy_Offer buyOffer, IUnitOfWork unitOfWork)
        {
            var boughtProduct = await unitOfWork.ProductRepository.GetById(buyOffer.product_id);

            boughtProduct.stock += buyOffer.amount;
            unitOfWork.ProductRepository.Update(boughtProduct);
            var soldProduct = await unitOfWork.ProductRepository.GetData(p => p.product_owner == sellOffer.seller_id &&
                                                                         p.Name == boughtProduct.Name);

            if (soldProduct.Count() != 0)
            {
                var p = soldProduct.First();
                if (p.stock < buyOffer.amount)
                {
                    return(null);
                }
                p.stock -= buyOffer.amount;
                unitOfWork.ProductRepository.Update(p);
                return(p);
            }
            return(null);
        }
        private async Task <product> GetBoughtProduct(sell_Offer sellOffer, buy_Offer buyOffer, IUnitOfWork unitOfWork)
        {
            var soldProduct = await unitOfWork.ProductRepository.GetById(sellOffer.product_id);

            soldProduct.stock       -= sellOffer.amount;
            soldProduct.sold_copies += sellOffer.amount;
            unitOfWork.ProductRepository.Update(soldProduct);
            var boughtProduct = await unitOfWork.ProductRepository.GetData(p => p.product_owner == buyOffer.buyer_id &&
                                                                           p.Name == soldProduct.Name);

            //if there is an existing product with such name and owner then update
            if (boughtProduct.Count() != 0)
            {
                var p = boughtProduct.First();
                p.stock += sellOffer.amount;
                unitOfWork.ProductRepository.Update(p);
                return(p);
            }
            //otherwise update and add
            var product = new product()
            {
                ID              = 0,
                condition_id    = soldProduct.condition_id,
                genre_id        = soldProduct.genre_id,
                product_type_id = soldProduct.product_type_id,
                Name            = soldProduct.Name,
                Update_Date     = DateTime.Now,
                Update_Who      = buyOffer.buyer_id,
                stock           = sellOffer.amount,
                sold_copies     = 0,
                product_owner   = buyOffer.buyer_id
            };

            unitOfWork.ProductRepository.Add(product);

            return(product);
        }
        public static SellOfferWrapper CreateSellOffer(sell_Offer s)
        {
            var wrapper = new SellOfferWrapper(s);

            wrapper.Seller  = new UserWrapper(s.users);
            wrapper.Product = new ProductWrapper(s.product);
            if (s.product.dic_condition != null)
            {
                wrapper.Product.Condition = new ConditionWrapper(s.product.dic_condition);
            }
            if (s.product.dic_Genre != null)
            {
                wrapper.Product.Genre = new GenreWrapper(s.product.dic_Genre);
            }
            if (s.product.dic_Product_type != null)
            {
                wrapper.Product.ProductType = new ProductTypeWrapper(s.product.dic_Product_type);
            }
            wrapper.UpdateDate  = DateTime.Now;
            wrapper.UpdateWho   = s.seller_id;
            wrapper.OfferStatus = new OfferStatusWrapper(s.dic_Offer_status);

            return(wrapper);
        }