Skip to content

Commit

Permalink
Added printContractCommand
Browse files Browse the repository at this point in the history
Co-Authored-By: Adam Abouelmagd <Adam2431@users.noreply.github.com>
  • Loading branch information
HusseinYasser and Adam2431 committed May 17, 2024
1 parent 4ff78ef commit dc051eb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public CompletableFuture<GetMilestoneResponse> receive(GetMilestoneRequest in) t
return CompletableFuture.completedFuture(
((GetMilestoneCommand) commandMap.getCommand("GetMilestone")).Run(in));
}

@RabbitHandler
@Async
public CompletableFuture<PrintContractResponse> receive(PrintContractRequest in)
throws Exception {
return CompletableFuture.completedFuture(
((PrintContractCommand) commandMap.getCommand("PrintContract")).Run(in));
}
// NEW_COMMAND_BOILERPLATE

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void registerCommands() {
commands.put("ProgressMilestone", ProgressMilestoneCommand.class);
commands.put("GetPendingTerminations", GetPendingTerminationsCommand.class);
commands.put("GetMilestone", GetMilestoneCommand.class);
commands.put("PrintContract", PrintContractCommand.class);
// NEW_COMMAND_BOILERPLATE
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.workup.contracts.commands;

import com.workup.contracts.models.Contract;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.PrintContractRequest;
import com.workup.shared.commands.contracts.responses.PrintContractResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.util.Optional;
import java.util.UUID;

public class PrintContractCommand
extends ContractCommand<PrintContractRequest, PrintContractResponse> {

@Override
public PrintContractResponse Run(PrintContractRequest request) {
// Get Contract by ID provided from the request
Optional<Contract> contract =
contractRepository.findById(UUID.fromString(request.getContractId()));

// If the contract is not found, we will return an error message
if (contract.isEmpty()) {
return PrintContractResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("No Contract With This ID Is Found!")
.build();
}

// If the contract is found, we will print it and return the contract string
String contractString = printContract(contract.get());
return PrintContractResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withWithFormatedContractDetails(contractString)
.build();
}

public String printContract(Contract contract) {

StringBuilder contractString = new StringBuilder();
contractString.append("Contract ID: ").append(contract.getContractId()).append("\n");
contractString.append("Contract Status: ").append(contract.getStatus()).append("\n");
contractString
.append("Contract Freelancer ID: ")
.append(contract.getFreelancerId())
.append("\n");
contractString.append("Contract Client ID: ").append(contract.getClientId()).append("\n");
contractString.append("Contract Job ID: ").append(contract.getJobId()).append("\n");
contractString.append("Contract Proposal ID: ").append(contract.getProposalId()).append("\n");
contractString.append("Contract Job Title: ").append(contract.getJobTitle()).append("\n");
contractString.append("Contract Milestones: \n");

// Print all the milestones of the contract

for (String milestoneId : contract.getMilestonesIds()) {

Optional<ContractMilestone> milestone =
contractMilestoneRepository.findById(UUID.fromString(milestoneId));

// If the milestone is not found, we will skip it. Should we or should we throw
// an Error?
if (milestone.isEmpty()) {
continue;
}
ContractMilestone milestoneData = milestone.get();

contractString.append("Milestone ID: ").append(milestoneData.getMilestoneId()).append("\n");
contractString
.append("Milestone Contract ID: ")
.append(milestoneData.getContractId())
.append("\n");
contractString
.append("Milestone Description: ")
.append(milestoneData.getDescription())
.append("\n");
contractString.append("Milestone Due Date: ").append(milestoneData.getDueDate()).append("\n");
contractString.append("Milestone Amount: ").append(milestoneData.getAmount()).append("\n");
}

return contractString.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@Jacksonized
public class PrintContractResponse extends CommandResponse {

private final String contractFileLink;
private final String withFormatedContractDetails;
}

0 comments on commit dc051eb

Please sign in to comment.