Ejemplo n.º 1
0
        public async Task <IActionResult> SetMainPhoto(int petId, int photoId)
        {
            if (petId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var pet = await petsRepository.GetPet(petId);

            if (!pet.Photos.Any(p => p.Id == photoId))
            {
                return(BadRequest("The photo does not belong to the user"));
            }

            var photo = await petsRepository.GetPhoto(photoId);

            var currentMainPhoto = await petsRepository.GetMainPhoto(petId);

            currentMainPhoto.MainPhoto = false;
            photo.MainPhoto            = true;

            if (await petsRepository.Save())
            {
                return(NoContent());
            }

            return(BadRequest("Error with changing photo"));
        }