public async Task <IActionResult> SetMainPhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } //var userFormRepo = await _repo.GetUser(userId); var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("This is already the main photo")); } var currentMainPhoto = await _repo.GetMainPhotoForUser(userId); currentMainPhoto.IsMain = false; photoFromRepo.IsMain = true; if (await _repo.SaveAll()) { return(NoContent()); } return(BadRequest("Could Not Set photo to main")); }
[HttpPost("{id}/setMain")] // serve per impostare una foto come principale public async Task <IActionResult> SetMainPhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) // se l'id che passiamo non corrisponde a nessun id delle foto in galleria { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) // serve per vedere se la foto è gia in main { return(BadRequest("This is already the main photo")); } var currentMainPhoto = await _repo.GetMainPhotoForUser(userId); currentMainPhoto.IsMain = false; photoFromRepo.IsMain = true; if (await _repo.SaveAll()) { return(NoContent()); } return(BadRequest("Could not set photo to main")); }