public async Task <ActionResult <PetDetails> > ProcessCreationForm(int ownerId, [FromBody] PetRequest petRequest, CancellationToken cancellationToken)
        {
            var owner = await _ownersRepo.FindById(ownerId, cancellationToken);

            _logger.LogInformation($"Saving pet {petRequest}");

            var newPet = new Pet(petRequest.Name, petRequest.BirthDate, int.Parse(petRequest.TypeId), ownerId)
            {
                PetType = await _petsRepo.FindPetTypeById(int.Parse(petRequest.TypeId), cancellationToken),
                Owner   = owner ?? throw new ResourceNotFoundException("Owner " + ownerId + " not found")
            };

            await _petsRepo.Save(newPet, cancellationToken);

            return(Created($"owners/pets/{newPet.Id}", new PetDetails(newPet.Id, newPet.Name, newPet.Owner.FirstName + " " + newPet.Owner.LastName, newPet.BirthDate, DTOs.PetType.ToDTO(newPet.PetType))));
        }
        public async Task <ActionResult <Pet> > ProcessCreationForm(int ownerId, [FromBody] PetRequest petRequest, CancellationToken cancellationToken)
        {
            var owner = await _ownersRepo.FindById(ownerId, cancellationToken);

            if (owner == null)
            {
                throw new ResourceNotFoundException("Owner " + ownerId + " not found");
            }

            //var pet = new Pet();
            //owner.AddPet(pet);

            _logger.LogInformation($"Saving pet {petRequest}");
            var newPet = await _petsRepo.Save(ownerId, petRequest, cancellationToken);

            return(Created($"owners/pets/{newPet.Id}", newPet));
        }
Esempio n. 3
0
        public async Task <ActionResult <OwnerDetails> > FindOwner(int ownerId, CancellationToken cancellationToken)
        {
            var owner = await _ownersRepo.FindById(ownerId, cancellationToken);

            return(Ok(OwnerDetails.FromOwner(owner)));
        }