Spaces:
Sleeping
Sleeping
File size: 4,025 Bytes
bb5169d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
package com.krishna.controller;
import com.krishna.dto.PostRequestDTO;
import com.krishna.dto.PostUpdateDTO;
import com.krishna.model.PostModel;
import com.krishna.model.UserModel;
import com.krishna.repository.LikeRepository;
import com.krishna.repository.PostRepository;
import com.krishna.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@RestController
@RequestMapping("/posts")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:8081", "http://127.0.0.1:5500", "http://localhost:5173/", "https://my-media-718.netlify.app/"})
public class PostController {
@Autowired
private PostRepository postRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private LikeRepository likeRepository;
@PostMapping("/add")
public ResponseEntity<PostModel> createPost(@RequestBody PostRequestDTO postRequestDTO) {
try {
UUID userId = postRequestDTO.getUserId();
if (userId == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
UserModel user = userRepository.findById(userId).orElse(null);
if (user == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
PostModel postModel = new PostModel();
postModel.setUserModel(user);
postModel.setContent(postRequestDTO.getContent());
postModel.setImageUrl(postRequestDTO.getImageUrl());
PostModel savedPost = postRepository.save(postModel);
return ResponseEntity.status(HttpStatus.CREATED).body(savedPost);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
// READ: All Posts
@GetMapping
public ResponseEntity<List<PostModel>> getAllPosts() {
try {
List<PostModel> posts = postRepository.findAll();
return ResponseEntity.ok(posts);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
// UPDATE
@PutMapping("/{id}")
public ResponseEntity<PostModel> updatePost(@PathVariable Long id, @RequestBody PostUpdateDTO postUpdateDTO) {
try {
Optional<PostModel> optionalPost = postRepository.findById(id);
if (optionalPost.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
PostModel post = optionalPost.get();
post.setContent(postUpdateDTO.getContent());
post.setImageUrl(postUpdateDTO.getImageUrl());
PostModel updatedPost = postRepository.save(post);
return ResponseEntity.ok(updatedPost);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
// DELETE
@DeleteMapping("/{id}")
public ResponseEntity<String> deletePost(@PathVariable Long id) {
try {
Optional<PostModel> optionalPost = postRepository.findById(id);
if (optionalPost.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Post not found");
}
// Delete associated likes first
likeRepository.deleteByPostId(id);
postRepository.deleteById(id);
return ResponseEntity.ok("Post deleted successfully");
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
}
}
}
|