public Animal Create(CreateAnimalRequest newAnimal, Enclosure enclosure) { if (!_context.AnimalClasses.Any(v => v.AnimalClassification == newAnimal.AnimalClass)) { AnimalClass animalClass = new AnimalClass() { AnimalClassification = newAnimal.AnimalClass }; _context.AnimalClasses.Add(animalClass); _context.SaveChanges(); } // var enclosureId = // enclosure = _context.Enclosures.GetByEnclosureName(newAnimal.EnclosureName); var newAnimalClass = GetAnimalClass(newAnimal.AnimalClass); var insertResponse = _context.Animals.Add(new Animal { AnimalName = newAnimal.AnimalName, Species = newAnimal.Species, Sex = newAnimal.Sex, DateOfBirth = newAnimal.DateOfBirth, DateAcquired = newAnimal.DateAquired, AnimalClassId = newAnimalClass.AnimalClassId, EnclosureId = enclosure.EnclosureId }); _context.SaveChanges(); return(insertResponse.Entity); }
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) }); }
public Animal Create(CreateAnimalRequest newAnimal) { if (!_context.Species.Any(x => x.SpeciesType == newAnimal.SpeciesType)) { _context.Species.Add(new Species { SpeciesType = newAnimal.SpeciesType, Classification = (Classification)Enum.Parse(typeof(Classification), newAnimal.Classification) }); _context.SaveChanges(); } var species = _context.Species.Single(x => x.SpeciesType == newAnimal.SpeciesType); var insertResponse = _context.Animals.Add(new Animal { SpeciesId = species.SpeciesId, Name = newAnimal.Name, Sex = (Sex)Enum.Parse(typeof(Sex), newAnimal.Sex), DOB = newAnimal.DOB, DateAcquired = newAnimal.DateAcquired }); _context.SaveChanges(); return(insertResponse.Entity); }
public IActionResult Create([FromBody] CreateAnimalRequest newAnimal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var animal = _animals.Create(newAnimal); var species = _species.GetBySpecies(newAnimal.SpeciesType); //var url = Url.Action("GetById", new { id = animal.AnimalId }); var responseViewModel = new AnimalResponse(animal, species); return(Created("test", responseViewModel)); }
public IActionResult Create([FromBody] CreateAnimalRequest newAnimal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Console.WriteLine(newAnimal.EnclosureName); var enclosure = _animals.GetByEnclosureName(newAnimal.EnclosureName); var animal = _animals.Create(newAnimal, enclosure); var url = Url.Action("GetById", new { id = animal.AnimalId }); var responseViewModel = new AnimalResponse(animal); return(Created(url, responseViewModel)); }
public async Task <IActionResult> CreateAnimal([FromBody] CreateAnimalRequest animalRequest) { try { var validator = new CreateAnimalValidator(animalRequest, _accommodationRepository); await validator.Validate(); if (validator.Validations.Any()) { return(BadRequest(validator.Validations)); } await _animalRepository.CreateAnimal(validator.Data); return(CreatedAtAction(nameof(GetAnimalById), validator.Data, new { id = validator.Data.Id })); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex)); } }
public CreateAnimalValidator(CreateAnimalRequest createAnimalRequest, IAccommodationRepository accommodationRepository) { _input = createAnimalRequest; _accommodationRepository = accommodationRepository; }
public async Task <IActionResult> Create([FromBody] CreateAnimalRequest request) { await _animalService.AddAsync(request, User); return(Ok()); }