示例#1
0
        public async Task <IActionResult> UpdatePet(Guid petId, PetForUpdateDto pet)
        {
            var petFromRepo = await _petRepository.GetPetAsync(petId);

            if (petFromRepo == null)
            {
                return(NotFound());
            }

            var validationResults = new PetForUpdateDtoValidator().Validate(pet);

            validationResults.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
                //return ValidationProblem();
            }

            _mapper.Map(pet, petFromRepo);
            _petRepository.UpdatePet(petFromRepo);

            await _petRepository.SaveAsync();

            return(NoContent());
        }
        public async Task <IActionResult> UpdatePet(int id, PetForUpdateDto petForUpdadeDto)
        {
            var userId      = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var petFromRepo = await _repo.GetPet(id);

            if (petFromRepo.UserId != userId)
            {
                return(Unauthorized());
            }

            _mapper.Map(petForUpdadeDto, petFromRepo);

            if (!_repo.HasChanges())
            {
                throw new Exception($"No changes were made");
            }


            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating pet {id} failed on save");
        }
示例#3
0
        public IActionResult UpdatePet(Guid petId, PetForUpdateDto pet)
        {
            var petFromRepo = _unitOfWork.Pets.Get(petId);

            if (petFromRepo.CreatedById != Account.Id && Account.Role != Role.Admin)
            {
                return(Unauthorized(new { message = "Unauthorized" }));
            }

            _mapper.Map(pet, petFromRepo);
            _unitOfWork.Pets.Update(petFromRepo);
            _unitOfWork.Complete(Account.Id);

            return(NoContent());
        }