Skip to content

Commit e07fab3

Browse files
authored
Refactor API Response and Other Classes (#21)
- Add creator class for `APIResponse`. - Rename package `aspect` to `log`.
1 parent 1fd07ce commit e07fab3

File tree

15 files changed

+147
-174
lines changed

15 files changed

+147
-174
lines changed

src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.ittovative.otpservice.config;
22

3-
import com.ittovative.otpservice.dto.OtpRequestDto;
4-
import com.ittovative.otpservice.dto.TokenDto;
3+
import com.ittovative.otpservice.dto.OTPRequestDTO;
4+
import com.ittovative.otpservice.dto.TokenDTO;
5+
import com.ittovative.otpservice.util.APIResponse;
56
import io.swagger.v3.oas.annotations.Operation;
67
import io.swagger.v3.oas.annotations.media.Content;
78
import io.swagger.v3.oas.annotations.media.ExampleObject;
89
import io.swagger.v3.oas.annotations.media.Schema;
910
import io.swagger.v3.oas.annotations.parameters.RequestBody;
1011
import io.swagger.v3.oas.annotations.responses.ApiResponse;
1112
import org.apache.coyote.BadRequestException;
12-
import org.springframework.http.ResponseEntity;
1313

14-
import static com.ittovative.otpservice.constant.HttpConstant.APPLICATION_JSON;
15-
import static com.ittovative.otpservice.constant.HttpConstant.BAD_REQUEST;
16-
import static com.ittovative.otpservice.constant.HttpConstant.CREATED;
17-
import static com.ittovative.otpservice.constant.HttpConstant.NOT_FOUND;
18-
import static com.ittovative.otpservice.constant.HttpConstant.OK;
14+
import static com.ittovative.otpservice.constant.HTTPConstant.APPLICATION_JSON;
15+
import static com.ittovative.otpservice.constant.HTTPConstant.BAD_REQUEST;
16+
import static com.ittovative.otpservice.constant.HTTPConstant.CREATED;
17+
import static com.ittovative.otpservice.constant.HTTPConstant.NOT_FOUND;
18+
import static com.ittovative.otpservice.constant.HTTPConstant.OK;
1919
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION;
2020
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE;
2121
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_TOKEN_RESPONSE_DESCRIPTION;
@@ -43,7 +43,7 @@ public interface SwaggerConfig {
4343
description = SEND_OTP_REQUEST_BODY_DESCRIPTION,
4444
content = @Content(
4545
mediaType = APPLICATION_JSON,
46-
schema = @Schema(implementation = OtpRequestDto.class),
46+
schema = @Schema(implementation = OTPRequestDTO.class),
4747
examples = @ExampleObject(value = SEND_OTP_REQUEST_BODY_EXAMPLE)
4848
)
4949
),
@@ -53,7 +53,7 @@ public interface SwaggerConfig {
5353
description = OTP_SENT_RESPONSE_DESCRIPTION,
5454
content = @Content(
5555
mediaType = APPLICATION_JSON,
56-
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
56+
schema = @Schema(implementation = APIResponse.class),
5757
examples = @ExampleObject(value = OTP_SENT_RESPONSE_EXAMPLE)
5858
)
5959
),
@@ -62,21 +62,21 @@ public interface SwaggerConfig {
6262
description = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION,
6363
content = @Content(
6464
mediaType = APPLICATION_JSON,
65-
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
65+
schema = @Schema(implementation = APIResponse.class),
6666
examples = @ExampleObject(value = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE)
6767
)
6868
)
6969
}
7070
)
71-
ResponseEntity<com.ittovative.otpservice.util.ApiResponse<String>> sendMessage(OtpRequestDto otpRequestDto);
71+
APIResponse<String> sendMessage(OTPRequestDTO otpRequestDto);
7272

7373
@Operation(summary = VERIFY_TOKEN_SUMMARY, description = VERIFY_TOKEN_DESCRIPTION,
7474
requestBody = @RequestBody(
7575
required = true,
7676
description = VERIFY_TOKEN_REQUEST_BODY_DESCRIPTION,
7777
content = @Content(
7878
mediaType = APPLICATION_JSON,
79-
schema = @Schema(implementation = OtpRequestDto.class),
79+
schema = @Schema(implementation = OTPRequestDTO.class),
8080
examples = @ExampleObject(value = VERIFY_TOKEN_REQUEST_BODY_EXAMPLE)
8181
)
8282
),
@@ -86,7 +86,7 @@ public interface SwaggerConfig {
8686
description = TOKEN_VERIFIED_RESPONSE_DESCRIPTION,
8787
content = @Content(
8888
mediaType = APPLICATION_JSON,
89-
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
89+
schema = @Schema(implementation = APIResponse.class),
9090
examples = @ExampleObject(value = TOKEN_VERIFIED_RESPONSE_EXAMPLE)
9191
)
9292
),
@@ -95,7 +95,7 @@ public interface SwaggerConfig {
9595
description = TOKEN_EXPIRED_RESPONSE_DESCRIPTION,
9696
content = @Content(
9797
mediaType = APPLICATION_JSON,
98-
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
98+
schema = @Schema(implementation = APIResponse.class),
9999
examples = @ExampleObject(value = TOKEN_EXPIRED_RESPONSE_EXAMPLE)
100100
)
101101
),
@@ -104,12 +104,11 @@ public interface SwaggerConfig {
104104
description = INVALID_TOKEN_RESPONSE_DESCRIPTION,
105105
content = @Content(
106106
mediaType = APPLICATION_JSON,
107-
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
107+
schema = @Schema(implementation = APIResponse.class),
108108
examples = @ExampleObject(value = INVALID_TOKEN_RESPONSE_EXAMPLE)
109109
)
110110
),
111111
}
112112
)
113-
ResponseEntity<com.ittovative.otpservice.util.ApiResponse<String>> verify(
114-
TokenDto tokenDto) throws BadRequestException;
113+
APIResponse<String> verify(TokenDTO tokenDto) throws BadRequestException;
115114
}

src/main/java/com/ittovative/otpservice/constant/ApiResponseConstant.java renamed to src/main/java/com/ittovative/otpservice/constant/APIResponseConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
44

5-
public final class ApiResponseConstant {
5+
public final class APIResponseConstant {
66
public static final String OTP_SENT = "OTP sent successfully!";
77
public static final String TOKEN_VERIFIED = "Token verified successfully!";
88

9-
private ApiResponseConstant() {
9+
private APIResponseConstant() {
1010
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
1111
}
1212
}

src/main/java/com/ittovative/otpservice/constant/HttpConstant.java renamed to src/main/java/com/ittovative/otpservice/constant/HTTPConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
44

5-
public final class HttpConstant {
5+
public final class HTTPConstant {
66
public static final String APPLICATION_JSON = "application/json";
77

88
public static final String OK = "200";
@@ -18,7 +18,7 @@ public final class HttpConstant {
1818
public static final String METHOD_NOT_ALLOWED = "405";
1919
public static final String TOO_MANY_REQUESTS = "429";
2020

21-
private HttpConstant() {
21+
private HTTPConstant() {
2222
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
2323
}
2424
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.ittovative.otpservice.controller;
2+
3+
import com.ittovative.otpservice.config.SwaggerConfig;
4+
import com.ittovative.otpservice.constant.APIResponseConstant;
5+
import com.ittovative.otpservice.constant.SwaggerConstant;
6+
import com.ittovative.otpservice.dto.OTPRequestDTO;
7+
import com.ittovative.otpservice.dto.TokenDTO;
8+
import com.ittovative.otpservice.service.OTPService;
9+
import com.ittovative.otpservice.util.APIResponse;
10+
import com.ittovative.otpservice.util.ResponseUtil;
11+
import io.swagger.v3.oas.annotations.tags.Tag;
12+
import jakarta.validation.Valid;
13+
import org.apache.coyote.BadRequestException;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
@RestController
21+
@RequestMapping("/api/v1/sms")
22+
@Tag(name = SwaggerConstant.CONTROLLER_NAME, description = SwaggerConstant.CONTROLLER_DESCRIPTION)
23+
public class OTPController implements SwaggerConfig {
24+
private final OTPService otpService;
25+
26+
public OTPController(OTPService otpService) {
27+
this.otpService = otpService;
28+
}
29+
30+
/**
31+
* Send message response entity.
32+
*
33+
* @param otpRequestDto the otp request dto
34+
* @return the response entity
35+
*/
36+
@Override
37+
@PostMapping
38+
public APIResponse<String> sendMessage(@RequestBody @Valid OTPRequestDTO otpRequestDto) {
39+
otpService.send(otpRequestDto);
40+
return ResponseUtil.createUnifiedResponse(HttpStatus.CREATED.value(), APIResponseConstant.OTP_SENT, null);
41+
}
42+
43+
/**
44+
* Verify response entity.
45+
*
46+
* @param tokenDto the verify otp request dto
47+
* @return the response entity
48+
* @throws BadRequestException the bad request exception
49+
*/
50+
@Override
51+
@PostMapping("/verify")
52+
public APIResponse<String> verify(@RequestBody @Valid TokenDTO tokenDto)
53+
throws BadRequestException {
54+
otpService.verifyToken(tokenDto);
55+
return ResponseUtil.createUnifiedResponse(HttpStatus.OK.value(), APIResponseConstant.TOKEN_VERIFIED, null);
56+
}
57+
}

src/main/java/com/ittovative/otpservice/controller/OtpController.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/main/java/com/ittovative/otpservice/dto/OtpRequestDto.java renamed to src/main/java/com/ittovative/otpservice/dto/OTPRequestDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_PHONE_NUMBER_FORMAT;
77

8-
public record OtpRequestDto(
8+
public record OTPRequestDTO(
99
@NotBlank @Pattern(regexp = "^[+0-9-]+$", message = INVALID_PHONE_NUMBER_FORMAT) String toPhoneNumber) {
1010
}

src/main/java/com/ittovative/otpservice/dto/TokenDto.java renamed to src/main/java/com/ittovative/otpservice/dto/TokenDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_PHONE_NUMBER_FORMAT;
77
import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_TOKEN_FORMAT;
88

9-
public record TokenDto(
9+
public record TokenDTO(
1010
@NotBlank @Pattern(regexp = "^[+0-9-]+$", message = INVALID_PHONE_NUMBER_FORMAT) String phoneNumber,
1111
@NotBlank @Pattern(regexp = "^\\d{6}$", message = INVALID_TOKEN_FORMAT) String token) {
1212
}

0 commit comments

Comments
 (0)