예제 #1
0
        public IActionResult Create(Pet pet)
        {
            _storeContext.Pets.Add(pet);
            _storeContext.SaveChanges();

            return(CreatedAtRoute("GetPet", new { id = pet.Id }, pet));
        }
예제 #2
0
        public IActionResult Create(Owner owner)
        {
            _storeContext.Owners.Add(owner);
            _storeContext.SaveChanges();

            return(CreatedAtRoute("GetOwner", new { id = owner.Id }, owner));
        }
예제 #3
0
        public ActionResult Create([Bind(Include = "PetID,PetName,PetDescription,create_dt,AnimalTypeCD")] Pet pet)
        {
            if (ModelState.IsValid)
            {
                db.Pets.Add(pet);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AnimalTypeCD = new SelectList(db.AnimalTypes, "AnimalTypeCD", "AnimalTypeName", pet.AnimalTypeCD);
            return(View(pet));
        }
        public int Add(Pet petEntity)
        {
            int result = -1;

            if (petEntity != null)
            {
                _context.Pets.Add(petEntity);
                _context.SaveChanges();
                result = petEntity.Id;
            }
            return(result);
        }
예제 #5
0
        public void DeleteAnimal(long id)
        {
            Animal toDelete = GetAnimal(id);

            if (toDelete != null)
            {
                _context.Animals.Remove(toDelete);
                _context.SaveChanges();
            }
            else
            {
                throw new Exception($"Could not find an animal with the ID '{id}'");
            }
        }
예제 #6
0
 public void AddPet(Pet pet)
 {
     using (var db = new PetStoreContext()) {
         db.Pets.Add(pet);
         db.SaveChanges();
     }
 }
예제 #7
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();
            }
        }
예제 #8
0
        public void SavePet(Pet pet)
        {
            Breed breed = _petContext.Breeds.Find(pet.Breed.Id);

            pet.Breed = breed;
            _petContext.Pets.Add(pet);
            _petContext.SaveChanges();
        }
예제 #9
0
        public IActionResult Put(int id)
        {
            using (var db = new PetStoreContext())  {
                db.Pets.Add(new Pet(2, "Amos", "Cat"));
                db.SaveChanges();
            }

            return(Ok());
        }
예제 #10
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();
     }
 }
예제 #11
0
 public PetsController(PetStoreContext storeContext)
 {
     _storeContext = storeContext;
     if (_storeContext.Pets.Count() == 0)
     {
         _storeContext.Pets.Add(new Pet {
             Name = "Cat", Family = "Cat"
         });
         _storeContext.SaveChanges();
     }
 }
예제 #12
0
 public OwnersController(PetStoreContext context)
 {
     _storeContext = context;
     if (_storeContext.Owners.Count() == 0)
     {
         _storeContext.Owners.Add(new Owner {
             Name = "Dileep"
         });
         _storeContext.SaveChanges();
     }
 }
예제 #13
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);
     }
 }
예제 #14
0
 public void SaveBreed(Breed breed)
 {
     if (!BreedExists(breed.Animal.Id, breed.Name))
     {
         Animal matchingAnimal = _context.Animals.Find(breed.Animal.Id);
         breed.Animal = matchingAnimal;
         _context.Breeds.Add(breed);
         _context.SaveChanges();
     }
     else
     {
         throw new Exception("Breed already exists");
     }
 }
예제 #15
0
        public IActionResult CriarPet([FromBody] PetRequestDTO petDto)
        {
            var categoria = petRepository.ObterCategoriaPorId(petDto.IdCategoria);

            if (categoria == null)
            {
                return(BadRequest("A categoria é obrigatoria"));
            }

            var pet = new Pet(petDto.Nome, petDto.Status, categoria,
                              petDto.Tags.Select(tag => new Tag(tag)).ToList());

            var petCadastrado = petRepository.Cadastrar(pet);

            contexto.SaveChanges();

            return(CreatedAtRoute("GetPet", new { id = petCadastrado.Id }, petCadastrado));
        }
예제 #16
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();
            }
        }
예제 #17
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();
            }
        }
예제 #18
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"));
        }
예제 #19
0
 public int Complete()
 {
     return(_context.SaveChanges());
 }