コード例 #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 _repo.GetUser(userId);

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

            var currentPhoto = await _repo.GetCurrentPhoto(userId);

            var photo = await _repo.GetPhoto(id);

            if (photo.IsMain)
            {
                BadRequest("Photo is already the main");
            }

            currentPhoto.IsMain = false;
            photo.IsMain        = true;

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

            return(BadRequest("Cannot set the photo to main"));
        }