Skip to content

Commit

Permalink
Removing caching from service
Browse files Browse the repository at this point in the history
  • Loading branch information
HusseinYasser committed May 16, 2024
1 parent 6526b55 commit 55f3dd2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ public class GetContractCommand extends ContractCommand<GetContractRequest, GetC
@Override
public GetContractResponse Run(GetContractRequest request) {
try {
String cachingKey = request.getContractId();
GetContractResponse cachedResponse =
(GetContractResponse) redisService.getValue(cachingKey, GetContractResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Contract request response fetched from cache: " + cachedResponse.toString(),
LoggingLevel.TRACE);

return cachedResponse;
}

Optional<Contract> contractOptional =
contractRepository.findById(UUID.fromString(request.getContractId()));

Expand All @@ -51,7 +40,6 @@ public GetContractResponse Run(GetContractRequest request) {
.withErrorMessage("")
.build();

redisService.setValue(cachingKey, response);
return response;
} catch (Exception ex) {
ContractsLogger.print(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.GetMilestoneRequest;
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse;
Expand All @@ -14,17 +12,6 @@ public class GetMilestoneCommand

@Override
public GetMilestoneResponse Run(GetMilestoneRequest request) {
String cachingKey = request.getMilestoneId();

GetMilestoneResponse cachedResponse =
(GetMilestoneResponse) redisService.getValue(cachingKey, GetMilestoneResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Milestone request response fetched from cache: " + cachedResponse.toString(),
LoggingLevel.TRACE);

return cachedResponse;
}

Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
Expand All @@ -38,20 +25,16 @@ public GetMilestoneResponse Run(GetMilestoneRequest request) {

ContractMilestone milestone = milestoneOptional.get();

GetMilestoneResponse response =
GetMilestoneResponse.builder()
.withContractId(milestone.getContractId())
.withMilestoneId(milestone.getMilestoneId().toString())
.withAmount(milestone.getAmount())
.withDescription(milestone.getDescription())
.withDueDate(milestone.getDueDate())
.withStatus(milestone.getStatus())
.withAmount(milestone.getAmount())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();

redisService.setValue(cachingKey, response);
return response;
return GetMilestoneResponse.builder()
.withContractId(milestone.getContractId())
.withMilestoneId(milestone.getMilestoneId().toString())
.withAmount(milestone.getAmount())
.withDescription(milestone.getDescription())
.withDueDate(milestone.getDueDate())
.withStatus(milestone.getStatus())
.withAmount(milestone.getAmount())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,36 @@ public class GetPendingTerminationsCommand

@Override
public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request) {
String cachingKey = request.getContractId() + "/pending_terminations";

GetPendingTerminationsResponse cachedResponse =
(GetPendingTerminationsResponse)
redisService.getValue(cachingKey, GetPendingTerminationsResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Contract terminations response fetched from cache: " + cachedResponse.toString(),
LoggingLevel.TRACE);
try {
List<TerminationRequest> terminationsList =
terminationRequestRepository.findByContractId(request.getContractId());

return cachedResponse;
}
if (terminationsList.isEmpty()) {
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("No pending terminations exist")
.build();
}

List<TerminationRequest> terminationsList =
terminationRequestRepository.findByContractId(request.getContractId());
TerminationRequest terminationRequest = terminationsList.getFirst();

if (terminationsList.isEmpty()) {
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("No pending terminations exist")
.withRequestId(terminationRequest.getRequestId().toString())
.withRequesterId(terminationRequest.getRequesterId())
.withContractId(terminationRequest.getContractId())
.withReason(terminationRequest.getReason())
.withStatus(terminationRequest.getStatus())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();
}

TerminationRequest terminationRequest = terminationsList.getFirst();

GetPendingTerminationsResponse response =
GetPendingTerminationsResponse.builder()
.withRequestId(terminationRequest.getRequestId().toString())
.withRequesterId(terminationRequest.getRequesterId())
.withContractId(terminationRequest.getContractId())
.withReason(terminationRequest.getReason())
.withStatus(terminationRequest.getStatus())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();
} catch (Exception e) {
ContractsLogger.print(e.getMessage(), LoggingLevel.TRACE);

redisService.setValue(cachingKey, response);
return response;
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("Error occurred while fetching pending termination requests")
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class HandleTerminationRequestCommand
extends ContractCommand<HandleTerminationRequest, HandleTerminationResponse> {

// TODO: validation by authorizing the `adminId` from `user service` and adding `adminId` to
// request body
public HandleTerminationResponse Run(HandleTerminationRequest request) {
Optional<TerminationRequest> terminationRequest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,6 @@ void clearAll() {
*
* @throws ParseException
*/
@Test
void testCreateJob() {
// Example test from jobs
// CreateJobRequest createJobRequest =
// CreateJobRequest.builder()
// .withTitle("Convert HTML Template to React 3")
// .withDescription(
// "I have an HTML template that I have purchased and own the rights
// to. I would like"
// + " it converted into a React application.")
// .withSkills(new String[] {"HTML", "CSS", "JavaScript", "React"})
// .withUserId(CLIENT_ONE_ID)
// .build();
//
// CreateJobResponse response =
// (CreateJobResponse)
// template.convertSendAndReceive(ServiceQueueNames.JOBS, createJobRequest);
//
// assertNotNull(response);
// assertTrue(response.getStatusCode() == HttpStatusCode.CREATED);
//
// jobRepository
// .findById(UUID.fromString(response.getJobId()))
// .ifPresentOrElse(
// job -> assertTrue(job.getTitle().equals(createJobRequest.getTitle())),
// () -> new RuntimeException("Job not found"));
}

@Test
void HandleContractTerminationTest1() throws ParseException {
handleContractTerminationTests.acceptingRequest(template);
Expand Down

0 comments on commit 55f3dd2

Please sign in to comment.