public bool UpdatePayment(Payment newPayment) { CoreValidator.ThrowIfNull(newPayment, nameof(newPayment)); CoreValidator.ThrowIfNegativeOrZero(newPayment.Id, nameof(newPayment.Id)); CoreValidator.ThrowIfNullOrEmpty(newPayment.PaymentTypeCode, nameof(newPayment.PaymentTypeCode)); CoreValidator.ThrowIfNegativeOrZero(newPayment.UserId, nameof(newPayment.UserId)); using (var db = new AuctionContext()) { var dbPayment = GetPayment(newPayment.Id); CoreValidator.ThrowIfNull(dbPayment, nameof(dbPayment)); db.Payments.Attach(dbPayment); dbPayment.PaymentTypeCode = newPayment.PaymentTypeCode; dbPayment.Type = newPayment.Type; dbPayment.UserId = newPayment.UserId; db.Entry(dbPayment).State = EntityState.Modified; db.SaveChanges(); return(true); } }
public bool IsUserExistingById(int userId) { CoreValidator.ThrowIfNegativeOrZero(userId, nameof(userId)); using (var db = new AuctionContext()) { return(db.Users.Any(u => u.Id == userId)); } }
public Invoice GetInvoiceByProductId(int productId) { CoreValidator.ThrowIfNegativeOrZero(productId, nameof(productId)); using (var db = dbContext) { return(db.Invoices.FirstOrDefault(p => p.ProductId == productId)); } }
public Invoice GetInvoiceByUserId(int userId) { CoreValidator.ThrowIfNegativeOrZero(userId, nameof(userId)); using (var db = dbContext) { return(db.Invoices.FirstOrDefault(u => u.UserId == userId)); } }
public bool IsZipExisting(int zipId) { CoreValidator.ThrowIfNegativeOrZero(zipId, nameof(zipId)); using (var db = dbContext) { return(db.Zips.Any(z => z.Id == zipId)); } }
public Payment GetPayment(int paymentId) { CoreValidator.ThrowIfNegativeOrZero(paymentId, nameof(paymentId)); using (var db = dbContext) { return(db.Payments.FirstOrDefault(p => p.Id == paymentId)); } }
public IList <Invoice> GetAllInvoicesForUser(User user) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); using (var db = dbContext) { return(db.Invoices.Where(u => u.UserId == user.Id).ToList()); } }
public IList <User> GetProductUsers(Product product) { CoreValidator.ThrowIfNull(product, nameof(product)); CoreValidator.ThrowIfNegativeOrZero(product.Id, nameof(product.Id)); using (var db = dbContext) { var collection = db.Products.SingleOrDefault(p => p.Id == product.Id).Bids.Select(u => u.UserId); CoreValidator.ThrowIfNull(collection, nameof(collection)); return(collection.Select(userId => db.Users.FirstOrDefault(u => u.Id == userId)).ToList()); } }
public Product GetProductById(int id) { CoreValidator.ThrowIfNegativeOrZero(id, nameof(id)); using (var db = dbContext) { var product = db.Products.Include("Bids").SingleOrDefault(p => p.Id == id); CoreValidator.ThrowIfNull(product, nameof(product)); return(product); } }
public User GetUserById(int id) { CoreValidator.ThrowIfNegativeOrZero(id, nameof(id)); using (this.dbContext) { var currentUser = this.dbContext.Users.FirstOrDefault(u => u.Id == id); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser); } }
public Bid GetBidById(int bidId) { CoreValidator.ThrowIfNegativeOrZero(bidId, nameof(bidId)); using (var db = new AuctionContext()) { var currentBid = db.Bids.FirstOrDefault(b => b.Id == bidId); CoreValidator.ThrowIfNull(currentBid, nameof(currentBid)); return(currentBid); } }
public User GetUserById(int id) { CoreValidator.ThrowIfNegativeOrZero(id, nameof(id)); using (var db = new AuctionContext()) { var currentUser = db.Users.FirstOrDefault(u => u.Id == id); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser); } }
public bool IsBidWon(Bid bid) { CoreValidator.ThrowIfNegativeOrZero(bid.Id, nameof(bid.Id)); using (var db = new AuctionContext()) { var currentBid = GetBidById(bid.Id); CoreValidator.ThrowIfNull(currentBid, nameof(currentBid)); return(currentBid.IsWon); } }
public Payment GetPayment(int paymentId) { CoreValidator.ThrowIfNegativeOrZero(paymentId, nameof(paymentId)); using (var db = new AuctionContext()) { var payment = db.Payments .Include("User") .FirstOrDefault(p => p.Id == paymentId); CoreValidator.ThrowIfNull(payment, nameof(payment)); return(payment); } }
public Invoice GetInvoiceInlcudeCollectionByProductId(int productId) { CoreValidator.ThrowIfNegativeOrZero(productId, nameof(productId)); using (var db = new AuctionContext()) { var invoice = db.Invoices .Include("User") .Include("Product") .FirstOrDefault(i => i.ProductId == productId); CoreValidator.ThrowIfNull(invoice, nameof(invoice)); return(invoice); } }
public IList <Invoice> GetUserInvoices(User user) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); using (var db = new AuctionContext()) { var currentUser = GetUserById(user.Id); db.Users.Attach(currentUser); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser.Invoices.ToList()); } }
public Bid GetBidByIdWithAllObjects(int bidId) { CoreValidator.ThrowIfNegativeOrZero(bidId, nameof(bidId)); using (var db = new AuctionContext()) { var resultBid = db.Bids .Include("User") .Include("Product") .FirstOrDefault(b => b.Id == bidId); CoreValidator.ThrowIfNull(resultBid, nameof(resultBid)); return(resultBid); } }
public IList <Bid> GetAllBidsByProductId(int productId) { CoreValidator.ThrowIfNegativeOrZero(productId, nameof(productId)); var isProductExists = new ProductControllerMock(dbContext).IsProductExistingById(productId); if (!isProductExists) { throw new ArgumentException("Product does not exist in the system."); } using (dbContext) { return(dbContext.Bids.Where(b => b.ProductId == productId).ToList()); } }
public Product GetProductByIdWithBidsAndUser(int id) { CoreValidator.ThrowIfNegativeOrZero(id, nameof(id)); using (var db = new AuctionContext()) { var product = db.Products .Include("Bids") .Include("Bids.User") .FirstOrDefault(p => p.Id == id); CoreValidator.ThrowIfNull(product, nameof(product)); return(product); } }
public IList <Bid> GetAllBidsByUserId(int userId) { CoreValidator.ThrowIfNegativeOrZero(userId, nameof(userId)); var isUserExists = new UserControllerMock(dbContext).IsUserExistingById(userId); if (!isUserExists) { throw new ArgumentException("User does not exist in the system."); } using (dbContext) { return(dbContext.Bids.Where(b => b.UserId == userId).ToList()); } }
public IList <Bid> GetUserBids(User user) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); using (this.dbContext) { var currentUser = GetUserById(user.Id); this.dbContext.Users.Attach(currentUser); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser.Bids.ToList()); } }
public string AddCookie(int userId) { CoreValidator.ThrowIfNegativeOrZero(userId, nameof(userId)); using (var db = new AuctionContext()) { var dbUser = GetUserById(userId); db.Users.Attach(dbUser); string guid = Guid.NewGuid().ToString(); dbUser.RememberToken = guid; db.Entry(dbUser).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(guid); } }
public bool DeletePayment(int paymentId) { CoreValidator.ThrowIfNegativeOrZero(paymentId, nameof(paymentId)); using (var db = dbContext) { var paymentNew = GetPayment(paymentId); CoreValidator.ThrowIfNull(paymentNew, nameof(paymentNew)); db.Payments.Attach(paymentNew); db.Payments.Remove(paymentNew); //db.Entry(paymentNew).State = EntityState.Deleted; db.SaveChanges(); return(true); } }
public bool BidExpired(int productId) { CoreValidator.ThrowIfNegativeOrZero(productId, nameof(productId)); using (var db = new AuctionContext()) { using (var transaction = db.Database.BeginTransaction()) { try { var product = ProductController.Instance().GetProductById(productId); db.Products.Attach(product); if (!(DateTime.Now >= product.EndDate)) { throw new ArgumentException($"The product date for finish is not yet reached: {product.EndDate}"); } var bids = db.Bids.Where(b => b.ProductId == productId); if (bids.Count() == 0) { product.IsAvailable = false; db.Entry(product).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); } else { var bid = bids.OrderByDescending(b => b.Id).FirstOrDefault(); db.Bids.Attach(bid); bid.IsWon = true; db.Entry(bid).State = System.Data.Entity.EntityState.Modified; product.IsAvailable = false; db.Entry(product).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); } return(true); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void AddPayment(Payment payment, int userId) { CoreValidator.ThrowIfNull(payment, nameof(payment)); CoreValidator.ThrowIfNullOrEmpty(payment.PaymentTypeCode, nameof(payment.PaymentTypeCode)); CoreValidator.ThrowIfNegativeOrZero(userId, nameof(userId)); using (var db = new AuctionContext()) { var paymentNew = new Payment { Type = payment.Type, PaymentTypeCode = payment.PaymentTypeCode, UserId = userId }; db.Payments.Add(paymentNew); db.SaveChanges(); } }
public int GetAllUserSpentCoinsForGivenProduct(User user, string productName) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNullOrEmpty(productName, nameof(productName)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); using (var db = new AuctionContext()) { var currentUser = GetUserById(user.Id); db.Users.Attach(currentUser); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser.Bids .Where(b => b.Product.Name == productName) .Sum(b => b.Coins)); } }
public bool DeleteProduct(Product product) { CoreValidator.ThrowIfNegativeOrZero(product.Id, nameof(product.Id)); using (var db = dbContext) { var productNew = GetProductById(product.Id); CoreValidator.ThrowIfNull(productNew, nameof(productNew)); db.Products.Attach(productNew); db.Products.Remove(productNew); // db.Entry(productNew).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); return(true); } }
public User GetUserByIdWithAllCollections(int id) { CoreValidator.ThrowIfNegativeOrZero(id, nameof(id)); using (var db = new AuctionContext()) { var currentUser = db.Users .Include("Zip") .Include("Bids") .Include("Bids.Product") .Include("Payments") .Include("Invoices") .FirstOrDefault(u => u.Id == id); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser); } }
public IList <Product> GetUserProducts(User user) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); using (var db = new AuctionContext()) { var currentUser = GetUserById(user.Id); db.Users.Attach(currentUser); CoreValidator.ThrowIfNull(currentUser, nameof(currentUser)); return(currentUser.Bids .Where(b => b.UserId == user.Id) .Select(b => b.Product) .ToList()); } }
public void CreateInvoice(User user, Product product) { CoreValidator.ThrowIfNull(user, nameof(user)); CoreValidator.ThrowIfNull(product, nameof(product)); CoreValidator.ThrowIfNegativeOrZero(user.Id, nameof(user.Id)); CoreValidator.ThrowIfNegativeOrZero(product.Id, nameof(product.Id)); using (var db = dbContext) { var invoice = new Invoice { UserId = user.Id, ProductId = product.Id }; db.Invoices.Add(invoice); db.SaveChanges(); } }