예제 #1
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         _dogRepo.AddDog(dog);
         return(RedirectToAction(nameof(Index)));
     }
     catch (Exception ex)
     {
         return(View(dog));
     }
 }
예제 #2
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                // LOOK AT THIS
                //  Let's save a new dog
                //  This new dog may or may not have Notes and/or an ImageUrl
                _dogRepository.AddDog(dog);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                // LOOK AT THIS
                //  When something goes wrong we return to the view
                //  BUT our view expects a DogFormViewModel object...so we'd better give it one
                DogFormViewModel vm = new DogFormViewModel()
                {
                    Dog    = dog,
                    Owners = _ownerRepository.GetAll(),
                };

                return(View(vm));
            }
        }
예제 #3
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                //add a new dog to the database
                //this new dog may or may not have a value for Notes and/or ImageUrl
                //since we are not allowing user to chose the owner when adding a dog, we need to set the OwnerId to the id of the user/owner that is signed in;
                dog.OwnerId = GetCurrentUserId();
                _dogRepo.AddDog(dog);

                return(RedirectToAction("Index"));
            }
            catch
            {
                List <Owner> owners = _ownerRepo.GetAllOwners();
                //if something goes wrong we return to the view, which is the DogFormViewModel
                DogFormViewModel vm = new DogFormViewModel()
                {
                    Dog    = dog,
                    Owners = owners
                };

                return(View(vm));
            }
        }
예제 #4
0
        public ActionResult <DogDto> CreateDogForClient(int clientId, DogForCreationDto dog)
        {
            if (!_clientRepository.ClientExists(clientId))
            {
                return(NotFound());
            }

            var dogEntity = _mapper.Map <Entities.Dog>(dog);

            _dogRepository.AddDog(clientId, dogEntity);
            _dogRepository.Save();

            var dogToReturn = _mapper.Map <DogDto>(dogEntity);

            return(CreatedAtRoute("GetDogForClient",
                                  new { clientId = clientId, dogId = dogToReturn.DogId }, dogToReturn));
        }
예제 #5
0
        public ActionResult <DogDto> CreateDogForOwner(int ownerId, DogForCreationDto dog)
        {
            if (!_ownerRepository.OwnerExists(ownerId))
            {
                return(NotFound());
            }

            var dogEntity = _mapper.Map <Dog>(dog);

            _dogRepository.AddDog(ownerId, dogEntity);

            var dogToReturn            = _mapper.Map <DogDto>(dogEntity);
            var linkedResourceToReturn = GetLinkedResourceToReturn(dogToReturn, null);

            return(CreatedAtRoute("GetDogForOwner",
                                  new { ownerId = linkedResourceToReturn["OwnerId"], dogId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
예제 #6
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         dog.OwnerId = GetCurrentUserId();
         _dogRepo.AddDog(dog);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
예제 #7
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         dog.OwnerId = GetCurrentUserId();
         _dogRepo.AddDog(dog);
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         return(View(dog));
     }
 }
예제 #8
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         _dogRepo.AddDog(dog);
         return(RedirectToAction(nameof(Index)));
     }
     // Always have an exception so you can debug
     catch (Exception ex)
     {
         return(View(dog));
     }
 }
예제 #9
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                _dogRepo.AddDog(dog); //Does this have access to the field _ownerRepo because of line 14?

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View(dog));
            }
        }
예제 #10
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                _dogRepo.AddDog(dog);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(View(dog));
            }
        }
예제 #11
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         // update the dogs OwnerId to the current user's Id
         dog.OwnerId = GetCurrentUserId();
         _dogRepo.AddDog(dog);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View(dog));
     }
 }
예제 #12
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                // update the dogs OwnerId to the current user's Id
                dog.OwnerId = GetCurrentUserId();

                _dogRepository.AddDog(dog);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View(dog));
            }
        }
예제 #13
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                // update the dogs OwnerId to the current user's Id AFTER THE DOG WAS CREATION WAS STARTED ON THE FORM
                dog.OwnerId = GetCurrentUserId();

                //add dog to database (including OwnerId assigned above)
                _dogRepo.AddDog(dog);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View(dog));
            }
        }
예제 #14
0
 public ActionResult Create(Dog dog)
 {
     try
     {
         // update the dogs OwnerId to the current user's Id
         dog.OwnerId = GetCurrentUserId();
         _dogRepo.AddDog(dog);
         return(RedirectToAction("Index"));
     }
     catch
     {
         DogFormModel dfm = new DogFormModel()
         {
             Dog    = dog,
             Owners = _ownerRepo.GetAllOwners()
         };
         return(View(dfm));
     }
 }
예제 #15
0
        public ActionResult Create(Dog dog)
        {
            try
            {
                dog.OwnerId = GetCurrentUserId();
                _dogRepo.AddDog(dog);
                return(RedirectToAction("Index"));
            }
            catch
            {
                // LOOK AT THIS
                //  When something goes wrong we return to the view
                //  BUT our view expects a DogFormViewModel object...so we'd better give it one
                DogFormViewModel vm = new DogFormViewModel()
                {
                    Dog = dog,
                };

                return(View(vm));
            }
        }
예제 #16
0
        public async Task <IActionResult> AddDog(AddNewDogModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var files = HttpContext.Request.Form.Files;
                    foreach (var Image in files)
                    {
                        if (Image != null && Image.Length > 0)
                        {
                            var file    = Image;
                            var uploads = Path.Combine(_appEnvironment.WebRootPath, "images");
                            if (file.Length > 0)
                            {
                                var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                                using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                                {
                                    await file.CopyToAsync(fileStream);

                                    var newDog = new Dog(model.Name, model.Race, model.BirthDate, fileName);
                                    newDog.AddSize(model.Size);
                                    newDog.SetGender(model.Gender);
                                    newDog.AddHairLenght(model.Lenght);
                                    _repository.AddDog(newDog);
                                    return(RedirectToAction("Home"));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _repository.AddError(ex.Message);
                return(View("Error"));
            }
            return(View(model));
        }