Beispiel #1
0
        public ProductCode AddProduct(ProductCreateDto Product)
        {
            if (DateTime.UtcNow > Product.ExpiryDate)
            {
                return(ProductCode.InvalidDate);
            }
            Product.UploadDate = DateTime.UtcNow;
            var product = _mapper.Map <Product>(Product);

            product.ProductId = Guid.NewGuid();
            var imgName = GetImgName(Product.ImgFile, product.ProductId);

            product.ImgUrl = GetImgUrl(imgName);
            _S3UploadeImage.UploadFileAsync(Product.ImgFile, imgName).Wait();
            _context.Product.Add(product);
            _context.SaveChanges();
            return(ProductCode.Success);
        }
Beispiel #2
0
        public BidCode AddBid(BidCreateDto Bid)
        {
            var bid = _mapper.Map <Bid>(Bid);

            bid.BidDate = DateTime.UtcNow;
            bid.BidId   = Guid.NewGuid();
            var latestBid = _context.Bid.OrderByDescending(x => x.BidAmount).FirstOrDefault(x => x.ProductId == bid.ProductId);

            if (latestBid != null)
            {
                var product = _context.Product.Include("Bids.User").FirstOrDefault(x => x.ProductId == bid.ProductId);
                if (product == null)
                {
                    return(BidCode.Null);
                }
                if (bid.BidAmount <= latestBid.BidAmount)
                {
                    return(BidCode.PriceTooLow);
                }
                if (bid.UserId == latestBid.UserId)
                {
                    return(BidCode.HighestBid);
                }
                var bidders = product.Bids.Select(x => x.User).Distinct();
                foreach (var bidder in bidders)
                {
                    if (bidder.UserId != Bid.UserId)
                    {
                        var message = $"<h3>A new bid has been made for {product.ProductName}!</h3><p>Current highest bid amount:${bid.BidAmount}</p><p>Submit a higher bid in order to win the auction!</p>";
                        var mail    = _emailService.NewMail(bidder.Username, $"New Bid for {product.ProductName}", message);
                        Task.Factory.StartNew(() => _emailService.SendEmail(mail));
                    }
                    else
                    {
                        var message = $"<h3>Your bid for {product.ProductName} is a success!</h3><p>Your bid amount:${bid.BidAmount}</p><p>Submit a higher bid in order to win the auction!</p>";
                        var mail    = _emailService.NewMail(bidder.Username, $"Bid success for {product.ProductName}", message);
                        Task.Factory.StartNew(() => _emailService.SendEmail(mail));
                    }
                }
            }
            _context.Bid.Add(bid);
            _context.SaveChanges();
            _bidHubContext.Clients.All.SendAsync("ReceiveBid", _mapper.Map <BidReadDto>(bid));
            return(Codes.BidCode.Success);
        }
Beispiel #3
0
        public User Register(string Username, string Password)
        {
            var existingUser = _context.Users.FirstOrDefault(x => x.Username == Username);

            if (existingUser != null)
            {
                return(null);
            }
            var user = new User()
            {
                UserId   = Guid.NewGuid(),
                Username = Username,
                Password = Password,
                Role     = Role.User
            };

            _context.Users.Add(user);
            _context.SaveChanges();
            return(user);
        }