diff --git a/src/main/java/polish_community_group_11/polish_community/feed/controllers/FeedApisController.java b/src/main/java/polish_community_group_11/polish_community/feed/controllers/FeedApisController.java index 00fd654621db84c66122528f5187db6622392d66..b79a08e51000813cfd02974461e75038938d7023 100644 --- a/src/main/java/polish_community_group_11/polish_community/feed/controllers/FeedApisController.java +++ b/src/main/java/polish_community_group_11/polish_community/feed/controllers/FeedApisController.java @@ -107,15 +107,33 @@ public class FeedApisController { // updating the post @PatchMapping("/{postId}") - public ResponseEntity<Void> updatePost(@PathVariable int postId, @RequestBody FeedImpl feed) { + public ResponseEntity<?> updatePost( + @PathVariable int postId, + @RequestPart("post") FeedImpl feed, + @RequestPart(value = "image", required = false) MultipartFile image) { try { + + if (image != null && !image.isEmpty()) { + + String imageUrl = fileStorageService.storeFile(image); + feed.setPostImageUrl(imageUrl); + } else { + + FeedImpl existingPost = feedRepository.getPostById(postId); + if (existingPost != null) { + feed.setPostImageUrl(existingPost.getPostImageUrl()); + } + } + feedRepository.updatePost(postId, feed); - return ResponseEntity.ok().build(); + return ResponseEntity.ok().body("Post updated successfully"); } catch (Exception e) { - return ResponseEntity.notFound().build(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Error updating post: " + e.getMessage()); } } + @DeleteMapping("/{postId}") public ResponseEntity<Void> deletePost(@PathVariable int postId) { try { diff --git a/src/main/java/polish_community_group_11/polish_community/feed/repository/FeedRepositoryImpl.java b/src/main/java/polish_community_group_11/polish_community/feed/repository/FeedRepositoryImpl.java index 2686d3efd33bed04027571c19aff85a53ccebab0..a895914078f0b2f692b25f715277a2e08ef36430 100644 --- a/src/main/java/polish_community_group_11/polish_community/feed/repository/FeedRepositoryImpl.java +++ b/src/main/java/polish_community_group_11/polish_community/feed/repository/FeedRepositoryImpl.java @@ -77,11 +77,12 @@ public class FeedRepositoryImpl implements FeedRepository { // update a post that is editing @Override public void updatePost(int postId, FeedImpl feed) { - String sql = "UPDATE feed SET post_title = ?, post_description = ? WHERE post_id = ?"; + String sql = "UPDATE feed SET post_title = ?, post_description = ?, post_image_url = ? WHERE post_id = ?"; jdbcTemplate.update(sql, feed.getPostTitle(), feed.getPostDescription(), + feed.getPostImageUrl(), postId ); diff --git a/src/main/resources/static/js/feed/feed.js b/src/main/resources/static/js/feed/feed.js index 456e8ae0dbb475b4060d0c53a0fb0a6d438178b6..cf791a2697db1aa659090b16928ccb5ff1cf5610 100644 --- a/src/main/resources/static/js/feed/feed.js +++ b/src/main/resources/static/js/feed/feed.js @@ -18,6 +18,11 @@ function resetForm() { postForm.reset(); isEditing = false; editPostId = null; + + const preview = document.getElementById('imagePreview'); + if (preview) { + preview.innerHTML = ''; // Remove any existing preview content + } } addNewPost.addEventListener('click', () => { @@ -287,6 +292,14 @@ document.addEventListener('click', async (event) => { document.getElementById('postDescription').value = postData.postDescription; document.getElementById('postTags').value = postData.tags.join(', '); + // Show existing image in preview if available + const preview = document.getElementById('imagePreview'); + if (postData.postImageUrl) { + preview.innerHTML = `<img src="${postData.postImageUrl}" style="max-width: 200px; max-height: 200px;">`; + } else { + preview.innerHTML = ''; // Clear preview if no image exists + } + modal.style.display = 'flex'; } catch (error) { diff --git a/uploads/4c5aed17-7ad1-490d-bb5c-a98d7a1d6406_nicolas-jehly-0UU9-_1EMvM-unsplash.jpg b/uploads/4c5aed17-7ad1-490d-bb5c-a98d7a1d6406_nicolas-jehly-0UU9-_1EMvM-unsplash.jpg new file mode 100644 index 0000000000000000000000000000000000000000..648e6c93860d6984c454a484b2ef392bd8fbc642 Binary files /dev/null and b/uploads/4c5aed17-7ad1-490d-bb5c-a98d7a1d6406_nicolas-jehly-0UU9-_1EMvM-unsplash.jpg differ