public async Task <IActionResult> DeletePhoto(int schoolId, int photoId) { var school = await database.SchoolRepository.Get <School>(schoolId); int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != school.OwnerId && !await rolesService.IsPermitted(RolesPermitted, currentUserId)) { return(this.Forbidden("Nie jesteś właścicielem tej szkółki")); } if (school.SchoolPhotos.Count == 0) { return(BadRequest("Brak zdjęć do usunięcia")); } var photoToDelete = school.SchoolPhotos.FirstOrDefault(p => p.Id == photoId); if (photoToDelete == null) { throw new EntityNotFoundException("Zdjęcie"); } school.SchoolPhotos.Remove(photoToDelete); if (!await database.Complete()) { return(BadRequest("Zdjęcie nie zostało usunięte")); } filesUploader.Delete(photoToDelete.FilePath); return(NoContent()); }