Ejemplo n.º 1
0
        public async Task <string> AddPet([FromForm] PetInputModel model)
        {
            try
            {
                var message = await this.petsService.SharePet(model);

                return(message);
            }
            catch (ArgumentException e)
            {
                return(e.Message);
            }
        }
Ejemplo n.º 2
0
        public IActionResult Register(PetInputModel pet)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(pet));
            }

            string ownerId = this.userManager.GetUserId(this.User);

            var petToRegister = this.mapper.Map <PetRegisterServiceModel>(pet);

            petToRegister.OwnerId = ownerId;

            this.petService.Register(petToRegister);

            return(this.RedirectToAction(nameof(HomeController.Index), "Home"));
        }
Ejemplo n.º 3
0
        public async Task <string> SharePet(PetInputModel input)
        {
            var user = await this.db.Users
                       .Include(u => u.Pets)
                       .FirstOrDefaultAsync(u => u.Id == input.OwnerId);

            if (user == null)
            {
                throw new ArgumentException("Invalid user id!");
            }
            if (user.Pets.Count >= 5)
            {
                throw new ArgumentException("You have reached the maximum amount of pets!");
            }


            var fileName = ImageUploader.UploadFile(input.File, "petImages");

            if (fileName == null)
            {
                throw new ArgumentException("Invalid image!");
            }

            var pet = new Pet()
            {
                Name         = input.Name,
                Age          = input.Age,
                Owner        = user,
                OwnerId      = user.Id,
                ProfileImage = fileName,
            };

            await this.db.Pets.AddAsync(pet);

            user.Pets.Add(pet);

            await this.db.SaveChangesAsync();

            return("Ok");
        }