コード例 #1
0
        public DogDto Add(CreateDogDto dog)
        {
            var d = _mapper.Map <CreateDogDto, DogEntity>(dog);

            var size = _dogRepository.GetSize(dog.SizeId);

            d.Size = size;

            var added = _dogRepository.Add(d);

            return(_mapper.Map <DogEntity, DogDto>(added));
        }
コード例 #2
0
        public async Task <IActionResult> RegisterDog(RegisterDogViewModel model, string id)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            ApplicationUser user = await _userManager.FindByIdAsync(id);

            //throw new Exception("Error");
            if (user == null)
            {
                _logger.LogError($"User not found with Id= {id} while trying to register a dog. 404 status code thrown.");
                Response.StatusCode = 404;
                return(View("~/Views/Error/UserNotFound.cshtml"));
            }

            Dog dog = new Dog
            {
                Id          = Guid.NewGuid(),
                IdParent    = id,
                FirstName   = model.FirstName,
                MiddleName  = model.MiddleName,
                LastName    = user.LastName,
                FullName    = model.FirstName + " " + model.MiddleName + " " + user.LastName,
                Description = model.Description,
                FavoriteToy = model.FavoriteToy,
                Breed       = model.Breed
            };

            string uniqueFileName = ProcessUploadedPhoto(model.Photo, dog.Id.ToString());

            dog.PhotoPath = uniqueFileName;

            _dogRepository.Add(dog);

            if (_dogRepository.GetAllDogs().Contains(dog))
            {
                if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                {
                    return(RedirectToAction("ListUsers", "Administration"));
                }
                await _signInManager.SignInAsync(user, isPersistent : false);

                return(RedirectToAction("ViewDog", "Account", new { dog.Id }));
            }

            return(View(model));
        }
コード例 #3
0
 public IActionResult Create(Dog newDog)
 {
     try
     {
         // TODO: Add insert logic here
         if (ModelState.IsValid)
         {
             _dogRepo.Add(newDog);
             return(RedirectToAction(nameof(Index)));
         }
         return(View(newDog));
     }
     catch
     {
         return(View(newDog));
     }
 }
コード例 #4
0
        public ActionResult Create(DogViewModel newDog, IFormFile pic, IFormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (!ModelState.IsValid)
                {
                    return(View(newDog));
                }

                // Remove deleted colors and set DogId
                foreach (Color clr in newDog.ThisDog.Colors.ToList())
                {
                    if (clr.Name == "" || clr.Name == null)
                    {
                        newDog.ThisDog.Colors.Remove(clr);
                    }
                }

                Status newStatus = new Status()
                {
                    DogStatus = "Arrival",
                    Date      = newDog.currentStatus.Date
                };

                newDog.ThisDog.Statuses = new List <Status>();
                newDog.ThisDog.Statuses.Add(newStatus);
                newDog.ThisDog.CurrentStatus = newStatus.DogStatus;

                newDog.ThisDog.Images = new List <Image>();
                if (pic != null)
                {
                    Image newImage = new Image();
                    var   filename = Path.Combine(_hostEnv.WebRootPath, "images", Path.GetFileName(pic.FileName));
                    //pic.CopyTo(new FileStream(filename, FileMode.Create));

                    using (var fileStream = new FileStream(filename, FileMode.Create))
                    {
                        pic.CopyTo(fileStream);
                    }
                    newImage.Name  = pic.FileName;
                    newImage.DogId = newDog.ThisDog.Id;
                    //Image noImage = newDog.ThisDog.Images.Find(p => p.Name == "noPhoto.jpg");
                    //if (noImage != null)
                    //{
                    //    newDog.ThisDog.Images.Remove(noImage);
                    //}

                    newDog.ThisDog.Images.Add(newImage);
                }


                _dogRepo.Add(newDog.ThisDog);



                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError("Error on Create: " + ex.Message);
                return(View(newDog));
            }
        }