Skip to content
Snippets Groups Projects
Commit f0ac71d0 authored by Richard Githuba's avatar Richard Githuba
Browse files

now can edit pictures in posts

parent 502e236b
No related branches found
No related tags found
No related merge requests found
......@@ -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 {
......
......@@ -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
);
......
......@@ -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) {
......
uploads/4c5aed17-7ad1-490d-bb5c-a98d7a1d6406_nicolas-jehly-0UU9-_1EMvM-unsplash.jpg

3.17 MiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment