public ActionResult Index(int? page) { var pageNumber = page ?? 1; const int pageSize = 20; var unitOfWork = new UnitOfWork(); int totalCustomerCount; var customers = unitOfWork.Repository<Customers>() .Query() .Include(i => i.CustomerDemographics) .OrderBy(q => q .OrderBy(c => c.ContactName) .ThenBy(c => c.CompanyName)) .Filter(q => q.ContactName != null) .GetPage(pageNumber, pageSize, out totalCustomerCount); ViewBag.Customers = new StaticPagedList<Customers>(customers, pageNumber, pageSize, totalCustomerCount); unitOfWork.Save(); return View(); }
public override void OnActionExecuting(HttpActionContext actionContext) { var authHeaderToken = actionContext.Request.Headers.Authorization; Guid token; if(Guid.TryParse(authHeaderToken.ToString(), out token)) { var unitOfWork = new UnitOfWork(); var user = unitOfWork.Users.Get(_ => _.UniqueToken == token); if (user != null) { if (user.Role != "admin" && user.SubscriptionEndDate <= DateTime.Now) { actionContext.Response = new HttpResponseMessage(HttpStatusCode.PaymentRequired); } base.OnActionExecuting(actionContext); return; } actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); }
public void AddNewCustomer() { using (IDbContext context = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(context)) using (ICustomerService customerService = new CustomerService(unitOfWork)) { var newCustomer = new Customer { Address = "2100 Ross Ave", City = "Dallas", CompanyName = "CBRE", ContactTitle = "Software Engineer", Country = "US", CustomerID = "CBRE", Fax = "2222222222", Phone = "1111111111", PostalCode = "75042", Region = "Dallas" }; customerService.Add(newCustomer); unitOfWork.Save(); var savedCustomer = customerService.GetCustomer("CBRE"); Assert.AreEqual(newCustomer.CustomerID, savedCustomer.CustomerID); } }
public void FindProductById() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { unitOfWork.Repository<Product>().Insert(new Product {ProductID = 1, Discontinued = false}); unitOfWork.Repository<Product>().Insert(new Product {ProductID = 2, Discontinued = true}); unitOfWork.Repository<Product>().Insert(new Product {ProductID = 3, Discontinued = true}); unitOfWork.Save(); var product = unitOfWork.Repository<Product>().Find(2); Assert.IsNotNull(product); Assert.AreEqual(2, product.ProductID); } }
public void DeleteProductById() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { unitOfWork.Repository<Product>().Insert(new Product {ProductID = 2, Discontinued = true, ObjectState = ObjectState.Added}); unitOfWork.Save(); unitOfWork.Repository<Product>().Delete(2); unitOfWork.Save(); var product = unitOfWork.Repository<Product>().Find(2); Assert.IsNull(product); } }
public static void Main(string[] args) { Console.WriteLine("Hello!"); string connectionStr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString; var unitOfWork = new UnitOfWork(new RepoDbContext(connectionStr)); var repo = unitOfWork.GetRepository<User>(); var user = repo.Find(1); user.Addresses.First().Name = "ChangedAddress"; user.Addresses.Add(new Address { Name = "Belarus, Minsk", UserId = 1 }); repo.Update(user); unitOfWork.Save(); /*while (true) { var users = repo.GetAll(); Console.WriteLine(); if (users.Any()) { Console.WriteLine("There are the following users in DB:"); Console.WriteLine("----------------------------------------------"); Console.WriteLine($"Id\t\tName"); Console.WriteLine("----------------------------------------------"); users.ForEach(x => { Console.WriteLine($"{x.Id}\t\t{x.Name}"); }); } else { Console.WriteLine("Currently the table User is empty"); } Console.WriteLine(); Console.WriteLine("The following operations are available:"); Console.WriteLine(" update existing users: -u"); Console.WriteLine(" insert 10 users: -i"); Console.WriteLine(" delete existing users: -d"); string data = Console.ReadLine(); if (data.Length < 2) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Error: Unknown command."); Console.ResetColor(); } else { string action = data.Substring(0, 2); switch (action) { case "-u": users.ForEach(x => { x.Name = $"Updated_{x.Name}"; }); break; case "-i": for (int i = 0; i < 10; i++) { repo.Insert(new User { Name = $"{i}{i}{i}{i}{i}{i}{i}" }); } break; case "-d": users.ForEach(x => { repo.Delete(x); }); break; default: Console.WriteLine("Error: Unknown command."); break; } unitOfWork.Save(); } }*/ }
public void GetProductsExecutesQuery() { using (IDbContext context = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(context)) { var products = unitOfWork.Repository<Product>().Query().Get().ToList(); Assert.IsInstanceOfType(products, typeof (List<Product>)); } }
public async void FindProductKeyAsync() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { unitOfWork.Repository<Product>().Insert(new Product { ProductID = 2, Discontinued = true }); unitOfWork.Save(); var product = await unitOfWork.Repository<Product>().FindAsync(2); Assert.AreEqual(product.ProductID, 2); } }
public void UpdateProduct() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { unitOfWork.Repository<Product>().Insert(new Product {ProductID = 2, Discontinued = true}); unitOfWork.Save(); var product = unitOfWork.Repository<Product>().Find(2); Assert.AreEqual(product.Discontinued, true, "Assert we are able to find the inserted Product."); product.Discontinued = false; product.ObjectState = ObjectState.Modified; unitOfWork.Repository<Product>().Update(product); unitOfWork.Save(); Assert.AreEqual(product.Discontinued, false, "Assert that our changes were saved."); } }
public void InsertRangeOfProducts() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { var newProducts = new[] { new Product {ProductID = 1, Discontinued = false, ObjectState = ObjectState.Added}, new Product {ProductID = 2, Discontinued = true, ObjectState = ObjectState.Added}, new Product {ProductID = 3, Discontinued = true, ObjectState = ObjectState.Added} }; unitOfWork.Repository<Product>().InsertRange(newProducts); var savedProducts = unitOfWork.Repository<Product>().Query().Get(); Assert.AreEqual(savedProducts.Count(), newProducts.Length); } }
public void GetProductsThatHaveBeenDiscontinued() { using (IDbContext northwindFakeContext = new NorthwindFakeContext()) using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext)) { unitOfWork.Repository<Product>().Insert(new Product {ProductID = 1, Discontinued = false, ObjectState = ObjectState.Added}); unitOfWork.Repository<Product>().Insert(new Product {ProductID = 2, Discontinued = true, ObjectState = ObjectState.Added}); unitOfWork.Repository<Product>().Insert(new Product {ProductID = 3, Discontinued = true, ObjectState = ObjectState.Added}); unitOfWork.Save(); var discontinuedProducts = unitOfWork.Repository<Product>().Query().Filter(t => t.Discontinued).Get(); Assert.AreEqual(2, discontinuedProducts.Count()); } }