Exemplo n.º 1
0
        public async Task <IActionResult> SetMainPohto(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 desiredMainPhoto = await _repo.GetPhoto(id);

            if (desiredMainPhoto.IsMain)
            {
                return(BadRequest("this is the main photo"));
            }
            var currentMainPhoto = await _repo.GetMainPhoto(userId);

            currentMainPhoto.IsMain = false;
            desiredMainPhoto.IsMain = true;
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            return(BadRequest("There is a problem "));
        }