Skip to content

Commit

Permalink
Refactor API Response and Other Classes (#21)
Browse files Browse the repository at this point in the history
- Add creator class for `APIResponse`.
- Rename package `aspect` to `log`.
  • Loading branch information
MohanadKh03 authored Sep 29, 2024
1 parent 1fd07ce commit e07fab3
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 174 deletions.
35 changes: 17 additions & 18 deletions src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.ittovative.otpservice.config;

import com.ittovative.otpservice.dto.OtpRequestDto;
import com.ittovative.otpservice.dto.TokenDto;
import com.ittovative.otpservice.dto.OTPRequestDTO;
import com.ittovative.otpservice.dto.TokenDTO;
import com.ittovative.otpservice.util.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.apache.coyote.BadRequestException;
import org.springframework.http.ResponseEntity;

import static com.ittovative.otpservice.constant.HttpConstant.APPLICATION_JSON;
import static com.ittovative.otpservice.constant.HttpConstant.BAD_REQUEST;
import static com.ittovative.otpservice.constant.HttpConstant.CREATED;
import static com.ittovative.otpservice.constant.HttpConstant.NOT_FOUND;
import static com.ittovative.otpservice.constant.HttpConstant.OK;
import static com.ittovative.otpservice.constant.HTTPConstant.APPLICATION_JSON;
import static com.ittovative.otpservice.constant.HTTPConstant.BAD_REQUEST;
import static com.ittovative.otpservice.constant.HTTPConstant.CREATED;
import static com.ittovative.otpservice.constant.HTTPConstant.NOT_FOUND;
import static com.ittovative.otpservice.constant.HTTPConstant.OK;
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION;
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE;
import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_TOKEN_RESPONSE_DESCRIPTION;
Expand Down Expand Up @@ -43,7 +43,7 @@ public interface SwaggerConfig {
description = SEND_OTP_REQUEST_BODY_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = OtpRequestDto.class),
schema = @Schema(implementation = OTPRequestDTO.class),
examples = @ExampleObject(value = SEND_OTP_REQUEST_BODY_EXAMPLE)
)
),
Expand All @@ -53,7 +53,7 @@ public interface SwaggerConfig {
description = OTP_SENT_RESPONSE_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
schema = @Schema(implementation = APIResponse.class),
examples = @ExampleObject(value = OTP_SENT_RESPONSE_EXAMPLE)
)
),
Expand All @@ -62,21 +62,21 @@ public interface SwaggerConfig {
description = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
schema = @Schema(implementation = APIResponse.class),
examples = @ExampleObject(value = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE)
)
)
}
)
ResponseEntity<com.ittovative.otpservice.util.ApiResponse<String>> sendMessage(OtpRequestDto otpRequestDto);
APIResponse<String> sendMessage(OTPRequestDTO otpRequestDto);

@Operation(summary = VERIFY_TOKEN_SUMMARY, description = VERIFY_TOKEN_DESCRIPTION,
requestBody = @RequestBody(
required = true,
description = VERIFY_TOKEN_REQUEST_BODY_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = OtpRequestDto.class),
schema = @Schema(implementation = OTPRequestDTO.class),
examples = @ExampleObject(value = VERIFY_TOKEN_REQUEST_BODY_EXAMPLE)
)
),
Expand All @@ -86,7 +86,7 @@ public interface SwaggerConfig {
description = TOKEN_VERIFIED_RESPONSE_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
schema = @Schema(implementation = APIResponse.class),
examples = @ExampleObject(value = TOKEN_VERIFIED_RESPONSE_EXAMPLE)
)
),
Expand All @@ -95,7 +95,7 @@ public interface SwaggerConfig {
description = TOKEN_EXPIRED_RESPONSE_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
schema = @Schema(implementation = APIResponse.class),
examples = @ExampleObject(value = TOKEN_EXPIRED_RESPONSE_EXAMPLE)
)
),
Expand All @@ -104,12 +104,11 @@ public interface SwaggerConfig {
description = INVALID_TOKEN_RESPONSE_DESCRIPTION,
content = @Content(
mediaType = APPLICATION_JSON,
schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class),
schema = @Schema(implementation = APIResponse.class),
examples = @ExampleObject(value = INVALID_TOKEN_RESPONSE_EXAMPLE)
)
),
}
)
ResponseEntity<com.ittovative.otpservice.util.ApiResponse<String>> verify(
TokenDto tokenDto) throws BadRequestException;
APIResponse<String> verify(TokenDTO tokenDto) throws BadRequestException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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

public final class ApiResponseConstant {
public final class APIResponseConstant {
public static final String OTP_SENT = "OTP sent successfully!";
public static final String TOKEN_VERIFIED = "Token verified successfully!";

private ApiResponseConstant() {
private APIResponseConstant() {
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

public final class HttpConstant {
public final class HTTPConstant {
public static final String APPLICATION_JSON = "application/json";

public static final String OK = "200";
Expand All @@ -18,7 +18,7 @@ public final class HttpConstant {
public static final String METHOD_NOT_ALLOWED = "405";
public static final String TOO_MANY_REQUESTS = "429";

private HttpConstant() {
private HTTPConstant() {
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ittovative.otpservice.controller;

import com.ittovative.otpservice.config.SwaggerConfig;
import com.ittovative.otpservice.constant.APIResponseConstant;
import com.ittovative.otpservice.constant.SwaggerConstant;
import com.ittovative.otpservice.dto.OTPRequestDTO;
import com.ittovative.otpservice.dto.TokenDTO;
import com.ittovative.otpservice.service.OTPService;
import com.ittovative.otpservice.util.APIResponse;
import com.ittovative.otpservice.util.ResponseUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.coyote.BadRequestException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/sms")
@Tag(name = SwaggerConstant.CONTROLLER_NAME, description = SwaggerConstant.CONTROLLER_DESCRIPTION)
public class OTPController implements SwaggerConfig {
private final OTPService otpService;

public OTPController(OTPService otpService) {
this.otpService = otpService;
}

/**
* Send message response entity.
*
* @param otpRequestDto the otp request dto
* @return the response entity
*/
@Override
@PostMapping
public APIResponse<String> sendMessage(@RequestBody @Valid OTPRequestDTO otpRequestDto) {
otpService.send(otpRequestDto);
return ResponseUtil.createUnifiedResponse(HttpStatus.CREATED.value(), APIResponseConstant.OTP_SENT, null);
}

/**
* Verify response entity.
*
* @param tokenDto the verify otp request dto
* @return the response entity
* @throws BadRequestException the bad request exception
*/
@Override
@PostMapping("/verify")
public APIResponse<String> verify(@RequestBody @Valid TokenDTO tokenDto)
throws BadRequestException {
otpService.verifyToken(tokenDto);
return ResponseUtil.createUnifiedResponse(HttpStatus.OK.value(), APIResponseConstant.TOKEN_VERIFIED, null);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

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

public record OtpRequestDto(
public record OTPRequestDTO(
@NotBlank @Pattern(regexp = "^[+0-9-]+$", message = INVALID_PHONE_NUMBER_FORMAT) String toPhoneNumber) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_PHONE_NUMBER_FORMAT;
import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_TOKEN_FORMAT;

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

0 comments on commit e07fab3

Please sign in to comment.