1
1
package com .example .articlemanager .delivery ;
2
2
3
+ import com .example .articlemanager .config .AppProperties ;
3
4
import com .example .articlemanager .model .rqrs .ArticleRequest ;
4
5
import com .example .articlemanager .model .rqrs .GenericResponse ;
5
6
import com .example .articlemanager .usecase .ArticleUsecase ;
6
7
8
+ import org .apache .tomcat .util .codec .binary .Base64 ;
7
9
import org .springframework .beans .factory .annotation .Autowired ;
10
+ import org .springframework .http .HttpStatus ;
8
11
import org .springframework .http .ResponseEntity ;
9
12
import org .springframework .web .bind .annotation .*;
10
13
@@ -15,24 +18,61 @@ public class ArticleController {
15
18
@ Autowired
16
19
private ArticleUsecase articleUsecase ;
17
20
21
+ @ Autowired
22
+ private AppProperties appProperties ;
23
+
18
24
@ PostMapping ("/add" )
19
- public ResponseEntity <?> addArticles (@ RequestBody ArticleRequest articleRq ) {
25
+ public ResponseEntity <?> addArticles (
26
+ @ RequestHeader (name = "Authorization" , required = false ) String authorization ,
27
+ @ RequestParam (name = "apiKey" , required = false ) String apiKey ,
28
+ @ RequestBody ArticleRequest articleRq ) {
29
+
30
+ if (appProperties .isEnableAuth ()){
31
+ if (authorization == null || !authorization .equals (appProperties .getSecretKey ())){
32
+ GenericResponse genericResponse = new GenericResponse ();
33
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
34
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
35
+ }
36
+ }else if (appProperties .isEnableApiKey ()){
37
+ if (apiKey == null || !apiKey .equals (appProperties .getApiKey ())){
38
+ GenericResponse genericResponse = new GenericResponse ();
39
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
40
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
41
+ }
42
+ }
43
+
20
44
GenericResponse genericResponse = articleUsecase .addArticles (articleRq );
21
45
return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
22
46
}
23
47
24
48
@ GetMapping ("/all" )
25
49
public ResponseEntity <?> getAllArticles (
50
+ @ RequestHeader (name = "Authorization" , required = false ) String authorization ,
51
+ @ RequestParam (name = "apiKey" , required = false ) String apiKey ,
26
52
@ RequestParam (name = "page" , required = false ) Integer page ,
27
53
@ RequestParam (name ="size" , required = false ) Integer size
28
54
) {
29
55
56
+ if (appProperties .isEnableAuth ()){
57
+ if (authorization == null || !authorization .equals (appProperties .getSecretKey ())){
58
+ GenericResponse genericResponse = new GenericResponse ();
59
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
60
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
61
+ }
62
+ }else if (appProperties .isEnableApiKey ()){
63
+ if (apiKey == null || !apiKey .equals (appProperties .getApiKey ())){
64
+ GenericResponse genericResponse = new GenericResponse ();
65
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
66
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
67
+ }
68
+ }
69
+
30
70
if (page == null && size == null ){
31
71
page = 0 ;
32
72
size = 0 ;
33
- }else if (page == null && size != null ) {
73
+ }else if (page == null ) {
34
74
page = 1 ;
35
- }else if (page != null && size == null ) {
75
+ }else if (size == null ) {
36
76
size = 10 ;
37
77
}
38
78
@@ -41,19 +81,73 @@ public ResponseEntity<?> getAllArticles(
41
81
}
42
82
43
83
@ GetMapping ("/{id}" )
44
- public ResponseEntity <?> getArticleById (@ PathVariable Long id ) {
84
+ public ResponseEntity <?> getArticleById (
85
+ @ RequestHeader (name = "Authorization" , required = false ) String authorization ,
86
+ @ RequestParam (name = "apiKey" , required = false ) String apiKey ,
87
+ @ PathVariable Long id ) {
88
+
89
+ if (appProperties .isEnableAuth ()){
90
+ if (authorization == null || !authorization .equals (appProperties .getSecretKey ())){
91
+ GenericResponse genericResponse = new GenericResponse ();
92
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
93
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
94
+ }
95
+ }else if (appProperties .isEnableApiKey ()){
96
+ if (apiKey == null || !apiKey .equals (appProperties .getApiKey ())){
97
+ GenericResponse genericResponse = new GenericResponse ();
98
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
99
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
100
+ }
101
+ }
102
+
45
103
GenericResponse genericResponse = articleUsecase .getArticleById (id );
46
104
return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
47
105
}
48
106
49
107
@ PostMapping ("/update/{id}" )
50
- public ResponseEntity <?> updateArticle (@ PathVariable Long id , @ RequestBody ArticleRequest article ) {
108
+ public ResponseEntity <?> updateArticle (
109
+ @ RequestHeader (name = "Authorization" , required = false ) String authorization ,
110
+ @ RequestParam (name = "apiKey" , required = false ) String apiKey ,
111
+ @ PathVariable Long id , @ RequestBody ArticleRequest article ) {
112
+
113
+ if (appProperties .isEnableAuth ()){
114
+ if (authorization == null || !authorization .equals (appProperties .getSecretKey ())){
115
+ GenericResponse genericResponse = new GenericResponse ();
116
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
117
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
118
+ }
119
+ }else if (appProperties .isEnableApiKey ()){
120
+ if (apiKey == null || !apiKey .equals (appProperties .getApiKey ())){
121
+ GenericResponse genericResponse = new GenericResponse ();
122
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
123
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
124
+ }
125
+ }
126
+
51
127
GenericResponse genericResponse = articleUsecase .updateArticle (article , id );
52
128
return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
53
129
}
54
130
55
131
@ DeleteMapping ("/delete/{id}" )
56
- public ResponseEntity <?> deleteArticle (@ PathVariable Long id ) {
132
+ public ResponseEntity <?> deleteArticle (
133
+ @ RequestHeader (name = "Authorization" , required = false ) String authorization ,
134
+ @ RequestParam (name = "apiKey" , required = false ) String apiKey ,
135
+ @ PathVariable Long id ) {
136
+
137
+ if (appProperties .isEnableAuth ()){
138
+ if (authorization == null || !authorization .equals (appProperties .getSecretKey ())){
139
+ GenericResponse genericResponse = new GenericResponse ();
140
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
141
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
142
+ }
143
+ }else if (appProperties .isEnableApiKey ()){
144
+ if (apiKey == null || !apiKey .equals (appProperties .getApiKey ())){
145
+ GenericResponse genericResponse = new GenericResponse ();
146
+ genericResponse .setFailed (HttpStatus .UNAUTHORIZED , "Unauthorized" );
147
+ return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
148
+ }
149
+ }
150
+
57
151
GenericResponse genericResponse = articleUsecase .deleteArticleById (id );
58
152
return ResponseEntity .status (genericResponse .getHttpStatusCode ()).body (genericResponse );
59
153
}
0 commit comments