Skip to content

Commit c5e439b

Browse files
committed
finalize
1 parent dc833e8 commit c5e439b

File tree

10 files changed

+355
-19
lines changed

10 files changed

+355
-19
lines changed

Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM openjdk:17-jdk-slim
2+
WORKDIR /app
3+
COPY target/article-manager-0.0.1-SNAPSHOT.jar .
4+
EXPOSE 8080
5+
ENTRYPOINT ["java","-jar","article-manager-0.0.1-SNAPSHOT.jar"]

docker-compose.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.7"
2+
3+
services:
4+
article-manager:
5+
container_name: article-manager
6+
build:
7+
context: ../article-manager
8+
dockerfile: Dockerfile
9+
restart: always
10+
deploy:
11+
mode: replicated
12+
replicas: 1
13+
ports:
14+
- "8080:8080"

pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,31 @@
4242
<artifactId>lombok</artifactId>
4343
<optional>true</optional>
4444
</dependency>
45+
<!-- unit test dependency -->
4546
<dependency>
4647
<groupId>org.springframework.boot</groupId>
4748
<artifactId>spring-boot-starter-test</artifactId>
4849
<scope>test</scope>
50+
<exclusions>
51+
<exclusion>
52+
<groupId>org.junit.vintage</groupId>
53+
<artifactId>junit-vintage-engine</artifactId>
54+
</exclusion>
55+
</exclusions>
4956
</dependency>
57+
<dependency>
58+
<groupId>org.junit.platform</groupId>
59+
<artifactId>junit-platform-launcher</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<!-- https://mvnrepository.com/artifact/junit/junit -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.8.1</version>
67+
<scope>test</scope>
68+
</dependency>
69+
5070
</dependencies>
5171

5272
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.articlemanager.config;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Getter
10+
@Setter
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
@Configuration
14+
public class AppProperties {
15+
16+
private String secretKey = "M9NNje86aFRBWTK";
17+
private String auth = "a0s1SFRKbEtpbUwxZ3NQOg==";
18+
private String apiKey = "kK5HTJlKimL1gsP";
19+
20+
private boolean enableAuth = true;
21+
private boolean enableApiKey = false;
22+
23+
}

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

+100-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.example.articlemanager.delivery;
22

3+
import com.example.articlemanager.config.AppProperties;
34
import com.example.articlemanager.model.rqrs.ArticleRequest;
45
import com.example.articlemanager.model.rqrs.GenericResponse;
56
import com.example.articlemanager.usecase.ArticleUsecase;
67

8+
import org.apache.tomcat.util.codec.binary.Base64;
79
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.HttpStatus;
811
import org.springframework.http.ResponseEntity;
912
import org.springframework.web.bind.annotation.*;
1013

@@ -15,24 +18,61 @@ public class ArticleController {
1518
@Autowired
1619
private ArticleUsecase articleUsecase;
1720

21+
@Autowired
22+
private AppProperties appProperties;
23+
1824
@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+
2044
GenericResponse genericResponse = articleUsecase.addArticles(articleRq);
2145
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
2246
}
2347

2448
@GetMapping("/all")
2549
public ResponseEntity<?> getAllArticles(
50+
@RequestHeader(name = "Authorization", required = false) String authorization,
51+
@RequestParam(name = "apiKey", required = false) String apiKey,
2652
@RequestParam(name = "page", required = false) Integer page,
2753
@RequestParam(name="size", required = false) Integer size
2854
) {
2955

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+
3070
if(page == null && size == null){
3171
page = 0;
3272
size = 0;
33-
}else if (page == null && size != null) {
73+
}else if (page == null) {
3474
page = 1;
35-
}else if (page != null && size == null) {
75+
}else if (size == null) {
3676
size = 10;
3777
}
3878

@@ -41,19 +81,73 @@ public ResponseEntity<?> getAllArticles(
4181
}
4282

4383
@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+
45103
GenericResponse genericResponse = articleUsecase.getArticleById(id);
46104
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
47105
}
48106

49107
@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+
51127
GenericResponse genericResponse = articleUsecase.updateArticle(article, id);
52128
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
53129
}
54130

55131
@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+
57151
GenericResponse genericResponse = articleUsecase.deleteArticleById(id);
58152
return ResponseEntity.status(genericResponse.getHttpStatusCode()).body(genericResponse);
59153
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.articlemanager.model.rqrs;
2+
3+
import lombok.Data;
4+
import lombok.experimental.Accessors;
5+
6+
@Data
7+
@Accessors(chain = true)
8+
public class AddArticleRs {
9+
10+
private int id;
11+
private String title;
12+
private String body;
13+
private String author;
14+
15+
}

src/main/java/com/example/articlemanager/model/rqrs/GenericResponse.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.articlemanager.model.rqrs;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
34
import org.springframework.http.HttpStatus;
45
import org.springframework.http.HttpStatusCode;
56

@@ -10,6 +11,7 @@
1011

1112
@Data
1213
@Accessors(chain = true)
14+
@JsonInclude(JsonInclude.Include.NON_NULL)
1315
public class GenericResponse {
1416

1517
@JsonIgnore

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

+20-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import com.example.articlemanager.model.rqrs.AddArticleRs;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.stereotype.Repository;
@@ -32,10 +33,20 @@ public GenericResponse addArticles(ArticleRequest articleRq) {
3233
return genericResponse;
3334
}
3435
}
35-
36+
// insert with query
3637
articleInsertRepository.insertWithQuery(articleRq);
3738

38-
genericResponse.setSuccessMsg("Article Inserted Successfully");
39+
// get all article
40+
articles = articleRepository.findAll();
41+
42+
// construct response
43+
AddArticleRs addArticleRs = new AddArticleRs();
44+
addArticleRs.setAuthor(articleRq.getAuthor());
45+
addArticleRs.setBody(articleRq.getBody());
46+
addArticleRs.setTitle(articleRq.getTitle());
47+
addArticleRs.setId(Math.toIntExact(articles.get(articles.size() - 1).getId()));
48+
49+
genericResponse.setSuccess(addArticleRs);
3950
}catch(Exception e){
4051
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
4152

@@ -82,8 +93,7 @@ public GenericResponse getAllArticle(Integer page, Integer size){
8293
}
8394
genericResponse.setSuccess(articles);
8495
}catch(Exception e){
85-
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
86-
96+
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
8797
}
8898

8999
return genericResponse;
@@ -92,13 +102,12 @@ public GenericResponse getAllArticle(Integer page, Integer size){
92102
public GenericResponse getArticleById(Long id){
93103
GenericResponse genericResponse = new GenericResponse();
94104
try{
95-
Article article = articleRepository.findById(id).get();
96-
if(article == null){
105+
List<Article> articles = articleRepository.findById(id).stream().toList();
106+
if(articles.isEmpty()){
97107
genericResponse.setFailed(HttpStatus.NOT_FOUND, "Article not found");
98108
return genericResponse;
99109
}
100-
101-
genericResponse.setSuccess(article);
110+
genericResponse.setSuccess(articles);
102111
}catch(Exception e){
103112
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
104113
}
@@ -109,12 +118,11 @@ public GenericResponse getArticleById(Long id){
109118
public GenericResponse deleteArticleById(Long id){
110119
GenericResponse genericResponse = new GenericResponse();
111120
try{
112-
Article article = articleRepository.findById(id).get();
113-
if(article == null){
114-
genericResponse.setFailed(HttpStatus.INTERNAL_SERVER_ERROR, "Article not found");
121+
List<Article> articles = articleRepository.findById(id).stream().toList();
122+
if(articles.isEmpty()){
123+
genericResponse.setFailed(HttpStatus.NOT_FOUND, "Article not found");
115124
return genericResponse;
116125
}
117-
118126
articleRepository.deleteById(id);
119127

120128
genericResponse.setSuccessMsg("Article Deleted Successfully");

src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ spring.datasource.name=testdb
66
spring.datasource.username=sa
77
spring.datasource.password=
88

9-
spring.jpa.show-sql=true
9+
# spring.jpa.show-sql=true
1010
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
1111
spring.jpa.hibernate.ddl-auto=update
1212
spring.h2.console.enabled=true

0 commit comments

Comments
 (0)