示例#1
0
        public async Task<Auction> MakeBid(long AuctionId, decimal Price, long UserId, DateTime Time)
        {
            Auction auction = await _auctionRepository.GetAsync(AuctionId);

            if (auction.WinnerId == UserId)
            {
                throw new Exception("Bạn đang là người trả giá cao nhất !");
            }

            if (Price % 500 != 0)
            {
                throw new Exception("Giá chỉ có thể lẻ đến 500VND");
            }

            if (auction.EndDate <= DateTime.UtcNow)
            {
                throw new Exception("Thời gian đấu giá đã kết thúc");
            }
            if (auction.InitPrice + 1000 >= Price)
            {
                throw new Exception("Mức giá không hợp lệ, cần lớn hơn giá khởi  tối thiểu 1000");
            }

            if (auction.CurrentPrice + 1000 >= Price)
            {
                throw new Exception("Mức giá không hợp lệ, cần lớn hơn giá hiện tại tối thiểu 1000");
            }

            if (auction.LastBidTime > Time)
            {
                throw new Exception("Mức giá không được ghi nhận");
            }

            auction.CurrentPrice = Price;
            auction.WinnerId = UserId;
            auction.LastBidTime = Time;
            auction.NumberOfBid += 1;
            Bid.Bid bid = new Bid.Bid
            {
                AuctionId = auction.Id,
                UserId = UserId,
                BidPrice = Price,
                BidTime = Time
            };

            await _auctionRepository.UpdateAsync(auction);
            await _bidRepository.InsertAsync(bid);
            await CurrentUnitOfWork.SaveChangesAsync();

            return auction;
        }
示例#2
0
        public async Task Create(string ProductName, decimal SubTotal, long AuctionId, string serial)
        {
            Auction.Auction auction = await _auctionRepository
                                      .FirstOrDefaultAsync(s => s.Id == AuctionId);

            if (auction.HasMakeInvoice)
            {
                throw new UserFriendlyException("Hóa đơn đã được lập trước đó!");
            }

            Bid.Bid lastBid = _bidRepository.GetAll()
                              .Where(s => s.AuctionId == AuctionId)
                              .OrderByDescending(s => s.BidTime)
                              .FirstOrDefault();

            if (auction.EndDate > DateTime.UtcNow)
            {
                throw new UserFriendlyException("Phiên đấu giá chưa kết thúc");
            }

            auction.HasMakeInvoice = true;

            Invoice Invoice = new Invoice
            {
                AuctionId    = AuctionId,
                SubTotal     = SubTotal,
                BidId        = lastBid.Id,
                ProductName  = ProductName,
                SellerId     = auction.SellerId,
                UserId       = lastBid.UserId,
                SerialNumber = serial
            };

            await _auctionRepository.UpdateAsync(auction);

            await _invoiceRepository.InsertAsync(Invoice);
        }