Beispiel #1
0
        public async Task <IActionResult> Create(CreatePetInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                var breeds = this.breedsService.GetAll <BreedDropDownViewModel>();
                input.Breeds = breeds;

                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.petsService.CreateAsync(input, user.Id, $"{this.environment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                input.Breeds = this.breedsService.GetAll <BreedDropDownViewModel>();

                return(this.View(input));
            }

            this.TempData["Message"] = "Pet added successfully.";

            return(this.RedirectToAction("MyPets"));
        }
Beispiel #2
0
        public async Task CreateAsync(CreatePetInputModel input, string userId, string imagePath)
        {
            var breed = this.breedsRepo.All().FirstOrDefault(x => x.Id == input.BreedId);

            if (input.TypeOfPet != breed.TypeOfPet)
            {
                throw new Exception($"Invalid breed! Choose valid {input.TypeOfPet} breed!");
            }

            var pet = new Pet()
            {
                Name          = input.Name,
                Sex           = input.Sex,
                TypeOfPet     = input.TypeOfPet,
                BreedId       = input.BreedId,
                BirthDate     = input.BirthDate,
                StartOfPeriod = input.StartOfPeriod,
                EndOfPeriod   = input.EndOfPeriod,
                Description   = input.Description,
                OwnerId       = userId,
            };


            Directory.CreateDirectory($"{imagePath}/pets/");
            foreach (var image in input.Images)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');

                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new PetImage
                {
                    AddedByUserId = userId,
                    Extension     = extension,
                };

                pet.PetImages.Add(dbImage);

                var physicalPath = $"{imagePath}/pets/{dbImage.Id}.{extension}";

                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }


            await this.petsRepo.AddAsync(pet);

            await this.petsRepo.SaveChangesAsync();
        }
Beispiel #3
0
        public IActionResult Edit(CreatePetInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            var petModel = mapper.Map <PetEditListingModel>(model);

            this.pets.Edit(petModel);

            return(this.RedirectToAction("All", "Pet"));
        }
Beispiel #4
0
        public IActionResult Create(CreatePetInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            var petModel = mapper.Map <PetCreateServiceModel>(model);

            this.pets.Add(petModel);

            return(this.RedirectToAction("All", "Pet"));
        }
Beispiel #5
0
        public IActionResult Create()
        {
            var breeds = this.breedsService.GetAll <BreedDropDownViewModel>();

            var viewModel = new CreatePetInputModel
            {
                Name          = "Kapitan Salam",
                Sex           = Sex.Male,
                TypeOfPet     = TypeOfPet.Dog,
                Breeds        = breeds,
                BirthDate     = DateTime.Parse("01.01.2021", CultureInfo.InvariantCulture),
                Description   = "Very Aggressive",
                StartOfPeriod = DateTime.Parse("01.01.2021", CultureInfo.InvariantCulture),
                EndOfPeriod   = DateTime.Parse("02.01.2021", CultureInfo.InvariantCulture),
            };

            return(this.View(viewModel));
        }