public void OpenAuction(CreateAuctionViewModel model, Guid organizationId)
        {
            if (model == null)
            {
                throw new Exception($"{typeof(CreateAuctionViewModel).Name} is null");
            }

            int maximumAllowedActiveAuctions = 3;

            var auctionsCheck = _applicationDbContext.Auctions
                                .Where(p => p.OrganizationId == organizationId && p.Status == AuctionStatus.Active)
                                .Count() < maximumAllowedActiveAuctions;

            var categoryCheck = _applicationDbContext.AuctionTypes
                                .SingleOrDefault(p => p.Name == model.AuctionType);

            if (categoryCheck == null)
            {
                AuctionType auctionType = new AuctionType()
                {
                    Id   = Guid.NewGuid(),
                    Name = model.AuctionType
                };
                _applicationDbContext.AuctionTypes.Add(auctionType);
                _applicationDbContext.SaveChanges();
                categoryCheck = auctionType;
            }

            if (!auctionsCheck)
            {
                throw new Exception("Превышено максимальное количество активных аукционов!");
            }


            Auction auction = new Auction()
            {
                Id                      = Guid.NewGuid(),
                Description             = model.Description,
                ShippingAddress         = model.ShippingAddress,
                ShippingConditions      = model.ShippingConditions,
                MinRatingForParticipant = model.RatingForParticipant,
                StartPrice              = model.StartPrice,
                PriceStep               = model.PriceStep,
                MinPrice                = model.MinPrice,
                StartDate               = model.StartDate,
                FinishDate              = model.FinishDate,
                Status                  = AuctionStatus.Active,
                AuctionTypeId           = categoryCheck.Id,
                OrganizationId          = organizationId
            };

            _applicationDbContext.Auctions.Add(auction);
            _applicationDbContext.SaveChanges();
        }
        public void AuctionManagementService_MakeBidToAuction()
        {
            ApplicationDbContext applicationDb = new ApplicationDbContext();

            applicationDb.Database.CreateIfNotExists();

            OrganizationType organizationType = new OrganizationType()
            {
                Id   = Guid.NewGuid(),
                Name = "TstTypeA_02"
            };

            applicationDb.OrganizationTypes.Add(organizationType);
            applicationDb.SaveChanges();

            Organization auctionMaster = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_02",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionMaster);
            applicationDb.SaveChanges();

            Organization auctionParticipant = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_022",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionParticipant);
            applicationDb.SaveChanges();

            AuctionType auctionType = new AuctionType()
            {
                Id   = Guid.NewGuid(),
                Name = "TestAuctionType_02"
            };

            applicationDb.AuctionTypes.Add(auctionType);
            applicationDb.SaveChanges();

            Auction auction = new Auction()
            {
                Id                      = Guid.NewGuid(),
                Description             = "test_any description",
                MinRatingForParticipant = 10.5,
                StartPrice              = 500000M,
                MinPrice                = 100000M,
                PriceStep               = 50000M,
                StartDate               = new DateTime(2019, 4, 26),
                FinishDate              = new DateTime(2019, 5, 22),
                Status                  = AuctionStatus.Active,
                AuctionTypeId           = auctionType.Id,
                OrganizationId          = auctionMaster.Id
            };

            applicationDb.Auctions.Add(auction);
            applicationDb.SaveChanges();

            Transaction depositTrans = new Transaction()
            {
                Id = Guid.NewGuid(),
                TransactionType = TransactionType.Deposit,
                Sum             = 400000,
                TransactionDate = DateTime.Now,
                OrganizationId  = auctionParticipant.Id
            };

            applicationDb.Transactions.Add(depositTrans);
            applicationDb.SaveChanges();

            Transaction withdrawTrans = new Transaction()
            {
                Id = Guid.NewGuid(),
                TransactionType = TransactionType.Withdraw,
                Sum             = 100000,
                TransactionDate = DateTime.Now,
                OrganizationId  = auctionParticipant.Id
            };

            applicationDb.Transactions.Add(withdrawTrans);
            applicationDb.SaveChanges();

            MakeBidViewModel model = new MakeBidViewModel()
            {
                AuctionId      = auction.Id.ToString(),
                OrganizationId = auctionParticipant.Id.ToString(),
                Price          = 200000M
            };

            decimal bidCost = 50000M;
            AuctionManagementService sut = new AuctionManagementService();

            sut.MakeBidToAuction(model, bidCost);

            Bid bid = applicationDb.Bids
                      .SingleOrDefault(p => p.AuctionId == auction.Id && p.OrganizationId == auctionParticipant.Id);
            Transaction transaction = applicationDb.Transactions
                                      .SingleOrDefault(p => p.OrganizationId == auctionParticipant.Id && p.Sum == bidCost);

            Assert.IsNotNull(bid);
            Assert.IsNotNull(transaction);
        }
        public void AuctionManagementService_ElectWinnerInAuction()
        {
            ApplicationDbContext applicationDb = new ApplicationDbContext();

            applicationDb.Database.CreateIfNotExists();

            OrganizationType organizationType = new OrganizationType()
            {
                Id   = Guid.NewGuid(),
                Name = "TstTypeA_04"
            };

            applicationDb.OrganizationTypes.Add(organizationType);
            applicationDb.SaveChanges();

            Organization auctionMaster = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_04",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionMaster);
            applicationDb.SaveChanges();

            Organization auctionParticipant = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_042",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionParticipant);
            applicationDb.SaveChanges();

            AuctionType auctionType = new AuctionType()
            {
                Id   = Guid.NewGuid(),
                Name = "TestAuctionType_04"
            };

            applicationDb.AuctionTypes.Add(auctionType);
            applicationDb.SaveChanges();

            Auction auction = new Auction()
            {
                Id                      = Guid.NewGuid(),
                Description             = "test_any description_04",
                MinRatingForParticipant = 9.5,
                StartPrice              = 600000M,
                MinPrice                = 100000M,
                PriceStep               = 50000M,
                StartDate               = new DateTime(2019, 4, 26),
                FinishDate              = new DateTime(2019, 5, 22),
                Status                  = AuctionStatus.Active,
                AuctionTypeId           = auctionType.Id,
                OrganizationId          = auctionMaster.Id
            };

            applicationDb.Auctions.Add(auction);
            applicationDb.SaveChanges();

            Transaction depositTrans = new Transaction()
            {
                Id = Guid.NewGuid(),
                TransactionType = TransactionType.Deposit,
                Sum             = 400000,
                TransactionDate = DateTime.Now,
                OrganizationId  = auctionParticipant.Id
            };

            applicationDb.Transactions.Add(depositTrans);
            applicationDb.SaveChanges();

            OrganizationRating rating = new OrganizationRating()
            {
                Id             = Guid.NewGuid(),
                Score          = 10,
                OrganizationId = auctionParticipant.Id
            };

            applicationDb.OrganizationRatings.Add(rating);
            applicationDb.SaveChanges();

            var activeBidStatus = applicationDb.BidStatuses.SingleOrDefault(p => p.StatusName == "Active");

            if (activeBidStatus == null)
            {
                BidStatus status = new BidStatus()
                {
                    Id         = Guid.NewGuid(),
                    StatusName = "Active"
                };
                applicationDb.BidStatuses.Add(status);
                applicationDb.SaveChanges();
                activeBidStatus = status;
            }

            Bid bid = new Bid()
            {
                Id             = Guid.NewGuid(),
                Price          = 200000M,
                CreatedDate    = DateTime.Now,
                BidStatusId    = activeBidStatus.Id,
                AuctionId      = auction.Id,
                OrganizationId = auctionParticipant.Id
            };

            applicationDb.Bids.Add(bid);
            applicationDb.SaveChanges();

            BidInfoViewModel model = new BidInfoViewModel()
            {
                BidId          = bid.Id.ToString(),
                AuctionId      = auction.Id.ToString(),
                OrganizationId = auctionParticipant.Id.ToString(),
                CreatedDate    = DateTime.Now,
                Price          = bid.Price
            };

            AuctionManagementService sut = new AuctionManagementService();

            sut.ElectWinnerInAuction(model);

            Bid         newBid      = applicationDb.Bids.SingleOrDefault(p => p.Id.ToString() == model.BidId);
            Transaction transaction = applicationDb.Transactions
                                      .SingleOrDefault(p => p.Sum == model.Price && p.TransactionType == TransactionType.Withdraw &&
                                                       p.OrganizationId.ToString() == model.OrganizationId);

            Assert.IsNotNull(newBid);
            Assert.IsNotNull(transaction);
        }
        public void AuctionManagementService_RevokeBidFromAuction()
        {
            ApplicationDbContext applicationDb = new ApplicationDbContext();

            applicationDb.Database.CreateIfNotExists();

            OrganizationType organizationType = new OrganizationType()
            {
                Id   = Guid.NewGuid(),
                Name = "TstTypeA_03"
            };

            applicationDb.OrganizationTypes.Add(organizationType);
            applicationDb.SaveChanges();

            Organization auctionMaster = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_03",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionMaster);
            applicationDb.SaveChanges();

            Organization auctionParticipant = new Organization()
            {
                Id                 = Guid.NewGuid(),
                FullName           = "TestCompanyA_032",
                RegistrationDate   = DateTime.Now,
                OrganizationTypeId = organizationType.Id
            };

            applicationDb.Organizations.Add(auctionParticipant);
            applicationDb.SaveChanges();

            AuctionType auctionType = new AuctionType()
            {
                Id   = Guid.NewGuid(),
                Name = "TestAuctionType_03"
            };

            applicationDb.AuctionTypes.Add(auctionType);
            applicationDb.SaveChanges();

            Auction auction = new Auction()
            {
                Id                      = Guid.NewGuid(),
                Description             = "test_any description_03",
                MinRatingForParticipant = 9.5,
                StartPrice              = 600000M,
                MinPrice                = 100000M,
                PriceStep               = 50000M,
                StartDate               = new DateTime(2019, 4, 26),
                FinishDate              = new DateTime(2019, 5, 22),
                Status                  = AuctionStatus.Active,
                AuctionTypeId           = auctionType.Id,
                OrganizationId          = auctionMaster.Id
            };

            applicationDb.Auctions.Add(auction);
            applicationDb.SaveChanges();

            var activeBidStatus = applicationDb.BidStatuses.SingleOrDefault(p => p.StatusName == "Active");

            if (activeBidStatus == null)
            {
                BidStatus status = new BidStatus()
                {
                    Id         = Guid.NewGuid(),
                    StatusName = "Active"
                };
                applicationDb.BidStatuses.Add(status);
                applicationDb.SaveChanges();
                activeBidStatus = status;
            }

            Bid bid = new Bid()
            {
                Id             = Guid.NewGuid(),
                Price          = 200000M,
                CreatedDate    = DateTime.Now,
                BidStatusId    = activeBidStatus.Id,
                AuctionId      = auction.Id,
                OrganizationId = auctionParticipant.Id
            };

            applicationDb.Bids.Add(bid);
            applicationDb.SaveChanges();

            AuctionManagementService sut = new AuctionManagementService();

            sut.RevokeBidFromAuction(bid.Id);

            Bid checkBid = applicationDb.Bids.Find(bid.Id);

            Assert.IsNotNull(checkBid);
            //Assert.IsTrue(bid.BidStatus.StatusName== "Revoke");
        }
        public void OpenAuction(CreateAuctionViewModel model, Guid organizationId)
        {
            if (model == null)
            {
                throw new Exception($"{typeof(CreateAuctionViewModel).Name} is null");
            }

            int maximumAllowedActiveAuctions = 3;

            var auctionsCheck = _applicationDbContext.Auctions
                                .Where(p => p.OrganizationId == organizationId && p.Status == AuctionStatus.Active)
                                .Count() < maximumAllowedActiveAuctions;

            var categoryCheck = _applicationDbContext.AuctionTypes
                                .SingleOrDefault(p => p.Name == model.AuctionType);

            if (categoryCheck == null)
            {
                AuctionType auctionType = new AuctionType()
                {
                    Id   = Guid.NewGuid(),
                    Name = model.AuctionType
                };
                _applicationDbContext.AuctionTypes.Add(auctionType);
                _applicationDbContext.SaveChanges();
                categoryCheck = auctionType;
            }

            if (!auctionsCheck)
            {
                throw new Exception("Превышено максимальное количество активных аукционов!");
            }


            Auction auction = new Auction()
            {
                Id                      = Guid.NewGuid(),
                Description             = model.Description,
                ShippingAddress         = model.ShippingAddress,
                ShippingConditions      = model.ShippingConditions,
                MinRatingForParticipant = model.RatingForParticipant,
                StartPrice              = model.StartPrice,
                PriceStep               = model.PriceStep,
                MinPrice                = model.MinPrice,
                StartDate               = model.StartDate,
                FinishDate              = model.FinishDate,
                Status                  = AuctionStatus.Active,
                AuctionTypeId           = categoryCheck.Id,
                OrganizationId          = organizationId
            };

            _applicationDbContext.Auctions.Add(auction);
            _applicationDbContext.SaveChanges();


            //загружаем файлы аукциона в бд
            List <AuctionFile> auctionFiles = new List <AuctionFile>();

            if (model.UploadFiles.Count != 0)
            {
                foreach (HttpPostedFileBase i in model.UploadFiles)
                {
                    AuctionFile file     = new AuctionFile();
                    byte[]      fileData = null;

                    // считываем файл в массив байтов
                    using (var binaryReader = new BinaryReader(i.InputStream))
                    {
                        fileData = binaryReader.ReadBytes(i.ContentLength);
                    }

                    // установка массива байтов
                    file.AuctionId = auction.Id;
                    file.FileName  = i.FileName;
                    file.Extension = i.ContentType;
                    file.Content   = fileData;
                    file.CreatedAt = DateTime.Now;
                    _applicationDbContext.AuctionFiles.Add(file);
                }
                _applicationDbContext.SaveChanges();
            }
        }
 public AuctionQueryRequestMessage(AuctionType type, int?offset = default(int?), byte limit = DefaultLimit)
 {
     Type   = type;
     Offset = offset;
     Limit  = limit;
 }
Beispiel #7
0
 public static IQueryable <Auction> FilterByType(this IQueryable <Auction> queryable, AuctionType value)
 {
     return(queryable.Where(a => a.Type == value));
 }