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

            var user = await _datingRepo.GetUser(userId);

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

            var photoForActive = await _datingRepo.GetPhoto(id);

            if (photoForActive.IsActive)
            {
                return(BadRequest("The photo is allready active"));
            }

            var currentActivePhoto = await _datingRepo.GetActivePhotoForUser(userId);

            currentActivePhoto.IsActive = false;

            photoForActive.IsActive = true;

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

            return(BadRequest("Could not set photo as active."));
        }