예제 #1
0
        public async Task <IActionResult> SetMainPhoto(int bookId, int id)
        {
            var book = await _repo.GetBook(bookId);

            if (!book.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("This is already the main photo"));
            }

            var currentMainPhoto = await _repo.GetMainPhotoForBook(bookId);

            currentMainPhoto.IsMain = false;

            photoFromRepo.IsMain = true;

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

            return(BadRequest("Could not set photo to main"));
        }