Skip to content

Commit

Permalink
add get post file url
Browse files Browse the repository at this point in the history
  • Loading branch information
luuh-oliveira committed May 31, 2024
1 parent b74e301 commit beab1a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/main/java/api/educai/controllers/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public ResponseEntity<PostDTO> getPostById(@PathVariable String id){
return ResponseEntity.status(200).body(mapper.map(postService.getPostById(id), PostDTO.class));
}

@Operation(summary = "Retorna a URL para download de um post específico passando seu ID")
@GetMapping("/{id}/download")
public ResponseEntity<String> getPostUrlById(@PathVariable String id){
return ResponseEntity.status(200).body(postService.getPostUrlById(id));
}

@Operation(summary = "Atualiza o título de um post")
@Secured("ROLE_TEACHER")
@PatchMapping("/{id}")
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/api/educai/dto/PostDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PostDTO {
@NotBlank
@Size(max = 100)
private String description;
private String url;
private String file;
private String originalFileName;
private LocalDate datePosting;
}
1 change: 1 addition & 0 deletions src/main/java/api/educai/entities/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Post {
@Size(max = 100)
private String description;
private String file;
private String originalFileName;
@NotNull
private LocalDate datePosting;
}
18 changes: 14 additions & 4 deletions src/main/java/api/educai/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Post createPost(NewPostDTO newPost, MultipartFile file) {
if (file != null) {
try {
String path = azureBlobService.upload(file);
post.setOriginalFileName(file.getOriginalFilename());
post.setFile(path);
} catch (IOException ex) {
throw new ResponseStatusException(HttpStatusCode.valueOf(500), "Error while trying to upload file!");
Expand All @@ -57,16 +58,16 @@ public List<Post> getPosts() {
return posts;
}

public Post getPostById(String id){
public Post getPostById(String id) {
ObjectId idPost = new ObjectId(id);
Post post = postRepository.findById(idPost);
if(post == null){
if (post == null) {
throw new ResponseStatusException(HttpStatusCode.valueOf(204), "No posts found with that id");
}
return post;
}

public Post updatePost(ObjectId id, PatchPost updatedPost){
public Post updatePost(ObjectId id, PatchPost updatedPost) {

Post post = postRepository.findById(id);
post.setTitle(updatedPost.getTitle());
Expand All @@ -76,8 +77,17 @@ public Post updatePost(ObjectId id, PatchPost updatedPost){
return post;
}

public Post deletePost(String id){
public Post deletePost(String id) {
ObjectId idPost = new ObjectId(id);
return postRepository.deleteById(idPost);
}

public String getPostUrlById(String id) {
Post post = getPostById(id);
String path = post.getFile();
String[] parts = path.split("/");
String fileName = parts[parts.length - 1];
return azureBlobService.getBlobUrl(fileName);

}
}

0 comments on commit beab1a2

Please sign in to comment.