Exemplo n.º 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)
            });
        }
Exemplo n.º 2
0
        public async Task EditAsync(UpdateSpeciesRequest request)
        {
            var speciesFromDb = await _speciesRepository.GetByIdAsync(request.Id);

            if (request.Name == null)
            {
                throw new BadRequestException("Nie podano nazwy");
            }

            speciesFromDb.Name = request.Name;

            await _speciesRepository.SaveChangesAsync();
        }