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

            var dbUser = await _repository.GetUser(userId, true);

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

            var dbPhoto = await _repository.GetPhoto(id);

            if (dbPhoto.IsProfilePhoto)
            {
                return(BadRequest("This photo is already the profile picture"));
            }

            var currentProfilePhoto = await _repository.GetUserProfilePhoto(userId);

            currentProfilePhoto.IsProfilePhoto = false;

            dbPhoto.IsProfilePhoto = true;

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

            return(BadRequest("Could not set photo as the profile picture"));
        }