Skip to content

Commit

Permalink
Merge pull request #8 from hardikSinghBehl/main
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikSinghBehl authored Mar 6, 2024
2 parents d6cf630 + 95add9e commit 7955aea
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY pom.xml .
COPY lombok.config .
RUN mvn dependency:go-offline -B
COPY src ./src
RUN mvn clean install
RUN mvn clean install -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

FROM openjdk:21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CacheManager cacheManager(final RedisProperties redisProperties) {
final var cacheManager = Caching.getCachingProvider().getCacheManager();
final var isCacheCreated = Optional.ofNullable(cacheManager.getCache(CACHE_NAME)).isPresent();

if (Boolean.FALSE.equals(isCacheCreated)) {
if (Boolean.FALSE.equals(isCacheCreated)) {
final var connectionUrl = String.format("redis://%s:%d", redisProperties.getHost(), redisProperties.getPort());
final var configuration = new Config();
configuration.useSingleServer().setPassword(redisProperties.getPassword()).setAddress(connectionUrl);
Expand Down
47 changes: 23 additions & 24 deletions src/main/java/com/behl/overseer/service/PlanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ public class PlanService {
private final UserPlanMappingRepository userPlanMappingRepository;
private final AuthenticatedUserIdProvider authenticatedUserIdProvider;

/**
* Updates the subscription plan for a user and deactivates their
* current plan in the system. The rate-limit corresponding to the
* previous plan is cleared on successful plan updation.
*
* If the provided plan-id to update matches the user's current plan-id,
* then no changes in the datasource is performed and method execution
* is halted.
*
* @param planUpdationRequest containing user's new plan details.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws InvalidPlanException if no plan exists with provided-id.
*/
/**
* Updates the subscription plan for a user and deactivates their current plan
* in the system. The rate-limit corresponding to the previous plan is cleared
* on successful plan updation.
*
* If the provided plan-id to update matches the user's current plan-id, then no
* changes in the datasource is performed and method execution is halted.
*
* @param planUpdationRequest containing user's new plan details.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws InvalidPlanException if no plan exists with provided-id.
*/
public void update(@NonNull final PlanUpdationRequestDto planUpdationRequest) {
final var planId = planUpdationRequest.getPlanId();
final var isPlanIdValid = planRepository.existsById(planId);
Expand All @@ -59,21 +58,21 @@ public void update(@NonNull final PlanUpdationRequestDto planUpdationRequest) {

rateLimitingService.reset(userId);
}

/**
* Retrieves all available subscription plans.
*
* @return List of PlanResponseDto containing details of each available plan.
*/
public List<PlanResponseDto> retrieve() {
return planRepository.findAll()
.stream()
.map(plan -> PlanResponseDto.builder()
.id(plan.getId())
.name(plan.getName())
.limitPerHour(plan.getLimitPerHour())
.build())
.toList();
}
public List<PlanResponseDto> retrieve() {
return planRepository.findAll()
.stream()
.map(plan -> PlanResponseDto.builder()
.id(plan.getId())
.name(plan.getName())
.limitPerHour(plan.getLimitPerHour())
.build())
.toList();
}

}
29 changes: 14 additions & 15 deletions src/main/java/com/behl/overseer/service/RateLimitingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,24 @@ public Bucket getBucket(@NonNull final UUID userId) {
return proxyManager.builder().build(userId, () -> createBucketConfiguration(userId));
}

/**
* Resets the rate limiting for the specified user-id.
*
* @param userId unique identifier of the user.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
*/
/**
* Resets the rate limiting for the specified user-id.
*
* @param userId unique identifier of the user.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
*/
public void reset(@NonNull final UUID userId) {
proxyManager.removeProxy(userId);
}

/**
* Constructs an instance of {@link BucketConfiguration} corresponding to
* the user's active plan which enforce the allowed rate-limit of API
* invocation.
*
* @param userId The unique identifier of the user.
* @return The bucket configuration for rate limiting based on the user's active plan.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
*/
/**
* Constructs an instance of {@link BucketConfiguration} corresponding to the
* user's active plan which enforce the allowed rate-limit of API invocation.
*
* @param userId The unique identifier of the user.
* @return The bucket configuration for rate limiting based on the user's active plan.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
*/
private BucketConfiguration createBucketConfiguration(@NonNull final UUID userId) {
final var userPlanMapping = userPlanMappingRepository.getActivePlan(userId);
final var limitPerHour = userPlanMapping.getPlan().getLimitPerHour();
Expand Down
50 changes: 24 additions & 26 deletions src/main/java/com/behl/overseer/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class UserService {
private final PasswordEncoder passwordEncoder;
private final UserPlanMappingRepository userPlanMappingRepository;

/**
/**
* Creates a new user account in the system corresponding to provided
* subscription plan in the request.
*
* @param userCreationRequest containing user account details.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws AccountAlreadyExistsException If an account with the provided email-id already exists.
* @throws InvalidPlanException If the provided plan ID is invalid.
*/
@Transactional
public void create(@NonNull final UserCreationRequestDto userCreationRequest) {
*
* @param userCreationRequest containing user account details.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws AccountAlreadyExistsException If an account with the provided email-id already exists.
* @throws InvalidPlanException If the provided plan ID is invalid.
*/
@Transactional
public void create(@NonNull final UserCreationRequestDto userCreationRequest) {
final var emailId = userCreationRequest.getEmailId();
final var userAccountExistsWithEmailId = userRepository.existsByEmailId(emailId);
if (Boolean.TRUE.equals(userAccountExistsWithEmailId)) {
Expand All @@ -63,18 +63,18 @@ public void create(@NonNull final UserCreationRequestDto userCreationRequest) {
userPlanMapping.setUserId(savedUser.getId());
userPlanMapping.setPlanId(planId);
userPlanMappingRepository.save(userPlanMapping);
}
/**
* Validates user login credentials and generates an access token on
* successful authentication.
*
* @param userLoginRequest The request object containing user login credentials.
* @return The access token response containing the generated access token.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws InvalidLoginCredentialsException If the provided login credentials are invalid.
*/
public TokenSuccessResponseDto login(@NonNull final UserLoginRequestDto userLoginRequest) {
}

/**
* Validates user login credentials and generates an access token on successful
* authentication.
*
* @param userLoginRequest The request object containing user login credentials.
* @return The access token response containing the generated access token.
* @throws IllegalArgumentException if provided argument is <code>null</code>.
* @throws InvalidLoginCredentialsException If the provided login credentials are invalid.
*/
public TokenSuccessResponseDto login(@NonNull final UserLoginRequestDto userLoginRequest) {
final var user = userRepository.findByEmailId(userLoginRequest.getEmailId())
.orElseThrow(InvalidLoginCredentialsException::new);

Expand All @@ -85,10 +85,8 @@ public TokenSuccessResponseDto login(@NonNull final UserLoginRequestDto userLogi
throw new InvalidLoginCredentialsException();
}

final var accessToken = jwtUtility.generateAccessToken(user.getId());
return TokenSuccessResponseDto.builder()
.accessToken(accessToken)
.build();
}
final var accessToken = jwtUtility.generateAccessToken(user.getId());
return TokenSuccessResponseDto.builder().accessToken(accessToken).build();
}

}

0 comments on commit 7955aea

Please sign in to comment.