Skip to content

Commit 247e162

Browse files
committed
enhance async
1 parent e3dd4cb commit 247e162

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/main/java/com/example/articlemanager/delivery/ArticleController.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.springframework.http.ResponseEntity;
1212
import org.springframework.web.bind.annotation.*;
1313

14+
import java.util.concurrent.CompletableFuture;
15+
1416
@RestController
1517
@RequestMapping("/articles")
1618
public class ArticleController {
@@ -124,7 +126,9 @@ public ResponseEntity<?> updateArticle(
124126
}
125127
}
126128

127-
GenericResponse genericResponse = articleUsecase.updateArticle(article, id);
129+
CompletableFuture.runAsync(()->articleUsecase.updateArticle(article, id));
130+
GenericResponse genericResponse = new GenericResponse();
131+
genericResponse.setSuccessMsg("Article Updated Successfully");
128132
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
129133
}
130134

@@ -148,7 +152,10 @@ public ResponseEntity<?> deleteArticle(
148152
}
149153
}
150154

151-
GenericResponse genericResponse = articleUsecase.deleteArticleById(id);
155+
156+
CompletableFuture.runAsync(()->articleUsecase.deleteArticleById(id));
157+
GenericResponse genericResponse = new GenericResponse();
158+
genericResponse.setSuccessMsg("Article Deleted Successfully");
152159
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
153160
}
154161
}

src/main/java/com/example/articlemanager/usecase/ArticleUsecase.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import com.example.articlemanager.model.rqrs.AddArticleRs;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.stereotype.Repository;
@@ -14,6 +15,7 @@
1415
import com.example.articlemanager.repository.ArticleRepository;
1516

1617
@Repository
18+
@Slf4j
1719
public class ArticleUsecase {
1820

1921
@Autowired
@@ -67,8 +69,10 @@ public GenericResponse updateArticle(ArticleRequest article, Long id){
6769
articleRepository.save(newArticle);
6870

6971
genericResponse.setSuccessMsg("Article Updated Successfully");
72+
log.info("{}", genericResponse.getMessage());
7073
}catch(Exception e){
7174
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
75+
log.info("{}", genericResponse.getError());
7276
}
7377

7478
return genericResponse;
@@ -115,22 +119,24 @@ public GenericResponse getArticleById(Long id){
115119
return genericResponse;
116120
}
117121

118-
public GenericResponse deleteArticleById(Long id){
122+
public void deleteArticleById(Long id){
119123
GenericResponse genericResponse = new GenericResponse();
120124
try{
121125
List<Article> articles = articleRepository.findById(id).stream().toList();
122126
if(articles.isEmpty()){
123127
genericResponse.setFailed(HttpStatus.NOT_FOUND, "Article not found");
124-
return genericResponse;
128+
125129
}
126130
articleRepository.deleteById(id);
127131

128132
genericResponse.setSuccessMsg("Article Deleted Successfully");
133+
log.info("{}", genericResponse.getMessage());
129134
}catch(Exception e){
130-
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
135+
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
136+
log.info("{}", genericResponse.getError());
131137
}
132138

133-
return genericResponse;
139+
134140
}
135141

136142

0 commit comments

Comments
 (0)