Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/get post file url #52

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}
}