Exemplo n.º 1
0
        // GET: Pet
        public ActionResult Index()
        {
            PetStoreContext context = new PetStoreContext();
            List <Pet>      pets    = context.Pets.ToList();

            return(View(pets));
        }
Exemplo n.º 2
0
 public void AddPet(Pet pet)
 {
     using (var db = new PetStoreContext()) {
         db.Pets.Add(pet);
         db.SaveChanges();
     }
 }
Exemplo n.º 3
0
        //IPetRepository _petRepository;
        //public TestController(IPetRepository petRepository)
        //{
        //    _petRepository = petRepository;
        //}
        // GET: Test
        public ActionResult Index()
        {
            var context = new PetStoreContext();
            var model   = context.Pet.ToList();

            return(View(model));
        }
Exemplo n.º 4
0
        static void AddProductsToDatabase()
        {
            using PetStoreContext context = new PetStoreContext();
            {
                Product squeakyBone = new Product()
                {
                    Name  = "Squeaky Dog Bone",
                    Price = 4.99M
                };

                //We have the option of explicitly adding the object to its associated table.  This is not necessary (see below)
                context.Products.Add(squeakyBone);

                Product tennisBalls = new Product()
                {
                    Name  = "Tennis Ball 3-Pack",
                    Price = 9.99M
                };

                //Here, we add the object only to the context.  Entity will recognize the object and assign it to the correct table.
                context.Add(tennisBalls);

                //Commit the changes to the context.
                context.SaveChanges();
            }
        }
Exemplo n.º 5
0
        static void ReadInformationFromDatabase()
        {
            //Query the database using Fluent API; this means using extensions to chain methods together.  Here, we
            //also use lambda expressions to consolidate the verbiage.
            using PetStoreContext context = new PetStoreContext();
            {
                //The lines below can also be written as a LINQ query:
                //var products = from product in context.Products
                //               where product.Price > 5.00m
                //               orderby product.Name
                //               select product;

                var products = context.Products
                               .Where(p => p.Price >= 5.00m)
                               .OrderBy(p => p.Name);


                foreach (Product p in products)
                {
                    Console.WriteLine($"Id:     {p.Id}");
                    Console.WriteLine($"Name:   {p.Name}");
                    Console.WriteLine($"Price:  {p.Price}");
                    Console.WriteLine(new string('-', 20));
                }
            }
        }
Exemplo n.º 6
0
        public IActionResult Put(int id)
        {
            using (var db = new PetStoreContext())  {
                db.Pets.Add(new Pet(2, "Amos", "Cat"));
                db.SaveChanges();
            }

            return(Ok());
        }
Exemplo n.º 7
0
 protected override void Up(MigrationBuilder migrationBuilder)
 {
     using (var db = new PetStoreContext())
     {
         db.Pets.Add(new Pet(1, "Zoey", "Cat"));
         db.Pets.Add(new Pet(2, "Norman", "Dog"));
         db.SaveChanges();
     }
 }
Exemplo n.º 8
0
 public PetsController(PetStoreContext storeContext)
 {
     _storeContext = storeContext;
     if (_storeContext.Pets.Count() == 0)
     {
         _storeContext.Pets.Add(new Pet {
             Name = "Cat", Family = "Cat"
         });
         _storeContext.SaveChanges();
     }
 }
Exemplo n.º 9
0
 public OwnersController(PetStoreContext context)
 {
     _storeContext = context;
     if (_storeContext.Owners.Count() == 0)
     {
         _storeContext.Owners.Add(new Owner {
             Name = "Dileep"
         });
         _storeContext.SaveChanges();
     }
 }
Exemplo n.º 10
0
 public bool DeletePet(int petId)
 {
     using (var db = new PetStoreContext()) {
         var toRemove = db.Pets.FirstOrDefault(p => p.Id == petId);
         if (toRemove != null)
         {
             db.Pets.Remove(toRemove);
             db.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
Exemplo n.º 11
0
        public UnitOfWork(PetStoreContext context)
        {
            _context    = context;
            UserAddress = new UserAddressRepository(_context);
            Products    = new ProductRepository(_context);

            Orders     = new OrderRepository(_context);
            OrderItems = new OrderItemRepository(_context);

            ShoppingCarts     = new ShoppingCartRepository(_context);
            ShoppingCartItems = new ShoppingCartItemRepository(_context);

            Pets     = new PetRepository(_context);
            PetTypes = new PetTypeRepository(_context);
        }
Exemplo n.º 12
0
        static void EditExistingRecord()
        {
            //To edit a record, we first need to get a reference to it; this requires querying the table.
            using PetStoreContext context = new PetStoreContext();
            {
                var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone")
                                  .FirstOrDefault();

                //check that a non null object was returned.
                if (squeakyBone is Product)
                {
                    //Modify the object's data.
                    squeakyBone.Price = 7.99m;
                }

                //Commit the changes.
                context.SaveChanges();
            }
        }
Exemplo n.º 13
0
        static void DeleteExistingRecord()
        {
            //To edit a record, we first need to get a reference to it; this requires querying the table.
            using PetStoreContext context = new PetStoreContext();
            {
                var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone")
                                  .FirstOrDefault();

                //check that a non null object was returned.
                if (squeakyBone is Product)
                {
                    //Remove the object
                    context.Remove(squeakyBone);
                    Console.WriteLine("removed object.");
                }

                //Commit the changes.
                context.SaveChanges();
            }
        }
Exemplo n.º 14
0
        public ActionResult Create(Pet pet)
        {
            if (!ModelState.IsValid)
            {
                return(View(pet));
            }

            try
            {
                PetStoreContext context = new PetStoreContext();
                context.Pets.Add(pet);
                context.SaveChanges();
            } catch (Exception ex)
            {
                ModelState.AddModelError("Error", ex.Message);
                return(View(pet));
            }


            TempData["Message"] = "Created " + pet.Name;

            return(RedirectToAction("Index"));
        }
Exemplo n.º 15
0
 public PetService(PetStoreContext petContext)
 {
     this._petContext = petContext;
 }
Exemplo n.º 16
0
 public BaseRepository(PetStoreContext context, IAsyncPolicy retryPolicy)
 {
     _context     = context ?? throw new System.ArgumentNullException(nameof(context));
     _retryPolicy = retryPolicy ?? throw new System.ArgumentNullException(nameof(retryPolicy));
 }
Exemplo n.º 17
0
 public ProductRepository(PetStoreContext context)
     : base(context)
 {
 }
Exemplo n.º 18
0
 public BreedService(PetStoreContext context)
 {
     this._context = context;
 }
Exemplo n.º 19
0
 public PetsController(PetStoreContext contexto, IPetRepository petRepository)
 {
     this.contexto      = contexto;
     this.petRepository = petRepository;
 }
Exemplo n.º 20
0
 public ProviderItemRepository(PetStoreContext context)
     : base(context)
 {
 }
Exemplo n.º 21
0
 public PetRepository(PetStoreContext contexto)
 {
     this.contexto = contexto;
 }
Exemplo n.º 22
0
 public IEnumerable <Pet> GetAllPets()
 {
     using (var db = new PetStoreContext()) {
         return(db.Pets.ToList());
     }
 }
Exemplo n.º 23
0
 public AnimalService(PetStoreContext context)
 {
     _context = context;
 }
Exemplo n.º 24
0
 public Pet GetPet(int petId)
 {
     using (var db = new PetStoreContext()) {
         return(db.Pets.FirstOrDefault(p => p.Id == petId));
     }
 }
Exemplo n.º 25
0
 public PetsController(PetStoreContext contexto)
 {
     this.contexto = contexto;
 }
Exemplo n.º 26
0
 public OwnersController(PetStoreContext context)
 {
     _context = context;
 }
Exemplo n.º 27
0
 public PetRepository(PetStoreContext context)
 {
     _context = context;
 }
Exemplo n.º 28
0
 public PetsController(PetStoreContext context)
 {
     _context = context;
 }
Exemplo n.º 29
0
 public ShoppingCartRepository(PetStoreContext context)
     : base(context)
 {
 }
Exemplo n.º 30
0
 public OrderRepository(PetStoreContext context)
     : base(context)
 {
 }