Пример #1
0
        public IActionResult CreateDog(int clientId,
                                       [FromBody] DogForCreationDto dog)
        {
            if (dog.Name == dog.ShortName)
            {
                ModelState.AddModelError(
                    "ShortName",
                    "The Name and Short Name cannot be the same (please use less characters with short name field)");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_clientInfoRepository.ClientExists(clientId))
            {
                return(NotFound());
            }

            var finalDog = _mapper.Map <Entities.Dog>(dog);

            _clientInfoRepository.AddDogForClient(clientId, finalDog);
            _clientInfoRepository.Save();

            var createdDogToReturn = _mapper
                                     .Map <Models.DogDto>(finalDog);

            return(CreatedAtRoute(
                       "GetDog",
                       new { clientId = clientId, id = createdDogToReturn.Id },
                       createdDogToReturn));
        }
Пример #2
0
        public ActionResult <DogDto> CreateDogForClient(int clientId, DogForCreationDto dog)
        {
            if (!_clientRepository.ClientExists(clientId))
            {
                return(NotFound());
            }

            var dogEntity = _mapper.Map <Entities.Dog>(dog);

            _dogRepository.AddDog(clientId, dogEntity);
            _dogRepository.Save();

            var dogToReturn = _mapper.Map <DogDto>(dogEntity);

            return(CreatedAtRoute("GetDogForClient",
                                  new { clientId = clientId, dogId = dogToReturn.DogId }, dogToReturn));
        }
Пример #3
0
        public ActionResult <DogDto> CreateDogForOwner(int ownerId, DogForCreationDto dog)
        {
            if (!_ownerRepository.OwnerExists(ownerId))
            {
                return(NotFound());
            }

            var dogEntity = _mapper.Map <Dog>(dog);

            _dogRepository.AddDog(ownerId, dogEntity);

            var dogToReturn            = _mapper.Map <DogDto>(dogEntity);
            var linkedResourceToReturn = GetLinkedResourceToReturn(dogToReturn, null);

            return(CreatedAtRoute("GetDogForOwner",
                                  new { ownerId = linkedResourceToReturn["OwnerId"], dogId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }