コード例 #1
0
        public async Task <IActionResult> SetMainPhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await usersRepository.GetUser(userId, true);

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

            var photoFromRepo = await photosRepository.GetPhoto(id);

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

            var currentMainPhoto = await photosRepository.GetMainPhotoForUser(userId);

            if (currentMainPhoto != null)
            {
                currentMainPhoto.IsMain = false;
            }

            photoFromRepo.IsMain = true;

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

            return(BadRequest("Failed to set the photo to main"));
        }
コード例 #2
0
 public async Task <Photo> SetMainPhoto(int id)
 {
     return(await _photosRepo.GetMainPhotoForUser(id));
 }