示例#1
0
        public async Task AddAsync(CreateAnimalRequest request, ClaimsPrincipal claimsPrincipal)
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                throw new BadRequestException("Imię nie może być puste.");
            }

            if (request.Age < 0)
            {
                throw new BadRequestException("Wiek zwierzęcia nie może być ujemny.");
            }

            if (await _speciesRepository.GetByIdAsync(request.SpeciesId) == null)
            {
                throw new BadRequestException("Dany gatunek nie istnieje");
            }

            if (await _userRepository.GetByIdAsync(int.Parse(claimsPrincipal.Identity.Name)) == null)
            {
                throw new BadRequestException("Dany właściciel nie istnieje");
            }

            await _animalRepository.AddAsync(new Animal()
            {
                Name      = request.Name,
                Age       = request.Age,
                SpeciesId = request.SpeciesId,
                OwnerId   = int.Parse(claimsPrincipal.Identity.Name)
            });
        }
示例#2
0
        public async Task AddAsync(string email, string name, int yearOfBirth)
        {
            var user = await _userRepository.GetOrFailAsync(email);

            var animal = await _animalRepository.GetAsync(user.Id, name);

            if (animal != null)
            {
                throw new ServiceException(ErrorCodes.AnimalAlreadyExist, $"Animal with name {name} already exists.");
            }

            var newAnimal = new Animal(user.Id, name);

            newAnimal.SetYearOfBirth(yearOfBirth);

            await _animalRepository.AddAsync(newAnimal);
        }