FINERACT-2169: Apply dto in CollateralsApiResource

This commit is contained in:
oleksandr.masliuk
2025-04-01 10:01:00 +02:00
committed by Adam Saghy
parent 833f4a0d33
commit b7ba9bd570
5 changed files with 97 additions and 78 deletions
@@ -20,12 +20,14 @@ package org.apache.fineract.portfolio.collateral.data;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Collection; import java.util.Collection;
import lombok.Getter;
import org.apache.fineract.infrastructure.codes.data.CodeValueData; import org.apache.fineract.infrastructure.codes.data.CodeValueData;
import org.apache.fineract.organisation.monetary.data.CurrencyData; import org.apache.fineract.organisation.monetary.data.CurrencyData;
/** /**
* Immutable data object for Collateral data. * Immutable data object for Collateral data.
*/ */
@Getter
public final class CollateralData { public final class CollateralData {
private final Long id; private final Long id;
@@ -20,7 +20,6 @@ package org.apache.fineract.portfolio.collateral.api;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.parameters.RequestBody;
@@ -55,6 +54,8 @@ import org.apache.fineract.infrastructure.core.serialization.ApiRequestJsonSeria
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.collateral.data.CollateralData; import org.apache.fineract.portfolio.collateral.data.CollateralData;
import org.apache.fineract.portfolio.collateral.data.LoansLoanIdCollateralsRequest;
import org.apache.fineract.portfolio.collateral.data.LoansLoandIdCollateralsCollateralIdRequest;
import org.apache.fineract.portfolio.collateral.service.CollateralReadPlatformService; import org.apache.fineract.portfolio.collateral.service.CollateralReadPlatformService;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -82,18 +83,12 @@ public class CollateralsApiResource {
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Collateral Details Template", description = "This is a convenience resource. It can be useful when building maintenance user interface screens for client applications. The template data returned consists of any or all of:\n" @Operation(summary = "Retrieve Collateral Details Template", description = "This is a convenience resource. It can be useful when building maintenance user interface screens for client applications. The template data returned consists of any or all of:\n"
+ "\n" + "Field Defaults\n" + "Allowed Value Lists\n" + "Example Request:\n" + "\n" + "loans/1/collaterals/template") + "\n" + "Field Defaults\n" + "Allowed Value Lists\n" + "Example Request:\n" + "\n" + "loans/1/collaterals/template")
@ApiResponses({ public CollateralData newCollateralTemplate(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId) {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.GetLoansLoanIdCollateralsTemplateResponse.class))) })
public String newCollateralTemplate(@Context final UriInfo uriInfo,
@PathParam("loanId") @Parameter(description = "loanId") final Long loanId) {
this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION);
final Collection<CodeValueData> codeValues = this.codeValueReadPlatformService.retrieveCodeValuesByCode("LoanCollateral"); final List<CodeValueData> codeValues = this.codeValueReadPlatformService.retrieveCodeValuesByCode("LoanCollateral");
final CollateralData collateralData = CollateralData.template(codeValues); return CollateralData.template(codeValues);
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.apiJsonSerializerService.serialize(settings, collateralData, RESPONSE_DATA_PARAMETERS);
} }
@GET @GET
@@ -101,17 +96,10 @@ public class CollateralsApiResource {
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "List Loan Collaterals", description = "Example Requests:\n" + "\n" + "loans/1/collaterals\n" + "\n" + "\n" @Operation(summary = "List Loan Collaterals", description = "Example Requests:\n" + "\n" + "loans/1/collaterals\n" + "\n" + "\n"
+ "loans/1/collaterals?fields=value,description") + "loans/1/collaterals?fields=value,description")
@ApiResponses({ public List<CollateralData> retrieveCollateralDetails(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId) {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = CollateralsApiResourceSwagger.GetLoansLoanIdCollateralsResponse.class)))) })
public String retrieveCollateralDetails(@Context final UriInfo uriInfo,
@PathParam("loanId") @Parameter(description = "loanId") final Long loanId) {
this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION); this.context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSION);
final List<CollateralData> collateralDatas = this.collateralReadPlatformService.retrieveCollateralsForValidLoan(loanId); return this.collateralReadPlatformService.retrieveCollateralsForValidLoan(loanId);
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.apiJsonSerializerService.serialize(settings, collateralDatas, RESPONSE_DATA_PARAMETERS);
} }
@GET @GET
@@ -143,17 +131,16 @@ public class CollateralsApiResource {
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Create a Collateral", description = "Note: Currently, Collaterals may be added only before a Loan is approved") @Operation(summary = "Create a Collateral", description = "Note: Currently, Collaterals may be added only before a Loan is approved")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PostLoansLoanIdCollateralsRequest.class))) @RequestBody(required = true, content = @Content(schema = @Schema(implementation = LoansLoanIdCollateralsRequest.class)))
@ApiResponses({ @ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PostLoansLoanIdCollateralsResponse.class))) }) @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PostLoansLoanIdCollateralsResponse.class))) })
public String createCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId, public CommandProcessingResult createCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@Parameter(hidden = true) final String apiRequestBodyAsJson) { @Parameter(hidden = true) final LoansLoanIdCollateralsRequest loansLoanIdCollateralsRequest) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().createCollateral(loanId).withJson(apiRequestBodyAsJson).build(); final CommandWrapper commandRequest = new CommandWrapperBuilder().createCollateral(loanId)
.withJson(apiJsonSerializerService.serialize(loansLoanIdCollateralsRequest)).build();
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return this.apiJsonSerializerService.serialize(result);
} }
@PUT @PUT
@@ -161,18 +148,16 @@ public class CollateralsApiResource {
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update a Collateral") @Operation(summary = "Update a Collateral")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PutLoansLoandIdCollateralsCollateralIdRequest.class))) @RequestBody(required = true, content = @Content(schema = @Schema(implementation = LoansLoandIdCollateralsCollateralIdRequest.class)))
@ApiResponses({ @ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PutLoansLoanIdCollateralsCollateralIdResponse.class))) }) @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.PutLoansLoanIdCollateralsCollateralIdResponse.class))) })
public String updateCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId, public CommandProcessingResult updateCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId, @PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId,
@Parameter(hidden = true) final String jsonRequestBody) { @Parameter(hidden = true) final LoansLoandIdCollateralsCollateralIdRequest loansLoandIdCollateralsCollateralIdRequest) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCollateral(loanId, collateralId).withJson(jsonRequestBody) final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCollateral(loanId, collateralId)
.build(); .withJson(apiJsonSerializerService.serialize(loansLoandIdCollateralsCollateralIdRequest)).build();
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return this.apiJsonSerializerService.serialize(result);
} }
@DELETE @DELETE
@@ -182,12 +167,10 @@ public class CollateralsApiResource {
@Operation(summary = "Remove a Collateral", description = "Note: A collateral can only be removed from Loans that are not yet approved.") @Operation(summary = "Remove a Collateral", description = "Note: A collateral can only be removed from Loans that are not yet approved.")
@ApiResponses({ @ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.DeleteLoansLoanIdCollateralsCollateralIdResponse.class))) }) @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CollateralsApiResourceSwagger.DeleteLoansLoanIdCollateralsCollateralIdResponse.class))) })
public String deleteCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId, public CommandProcessingResult deleteCollateral(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId) { @PathParam("collateralId") @Parameter(description = "collateralId") final Long collateralId) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteCollateral(loanId, collateralId).build(); final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteCollateral(loanId, collateralId).build();
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
return this.apiJsonSerializerService.serialize(result);
} }
} }
@@ -19,7 +19,7 @@
package org.apache.fineract.portfolio.collateral.api; package org.apache.fineract.portfolio.collateral.api;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Set; import org.apache.fineract.portfolio.collateral.data.LoansLoanIdCollateralsRequest;
/** /**
* Created by Chirag Gupta on 12/01/17. * Created by Chirag Gupta on 12/01/17.
@@ -71,15 +71,6 @@ final class CollateralsApiResourceSwagger {
public GetCollateralCurrencyResponse currency; public GetCollateralCurrencyResponse currency;
} }
@Schema(description = "PostLoansLoanIdCollateralsRequest")
public static final class PostLoansLoanIdCollateralsRequest {
private PostLoansLoanIdCollateralsRequest() {}
@Schema(example = "9")
public Long collateralTypeId;
}
@Schema(description = "PostLoansLoanIdCollateralsResponse") @Schema(description = "PostLoansLoanIdCollateralsResponse")
public static final class PostLoansLoanIdCollateralsResponse { public static final class PostLoansLoanIdCollateralsResponse {
@@ -89,15 +80,6 @@ final class CollateralsApiResourceSwagger {
public Long resourceId; public Long resourceId;
} }
@Schema(description = "PutLoansLoandIdCollateralsCollateralIdRequest")
public static final class PutLoansLoandIdCollateralsCollateralIdRequest {
private PutLoansLoandIdCollateralsCollateralIdRequest() {}
@Schema(example = "22 Carat Gold chain weighing 12 grams")
public String description;
}
@Schema(description = "PutLoansLoanIdCollateralsCollateralIdResponse") @Schema(description = "PutLoansLoanIdCollateralsCollateralIdResponse")
public static final class PutLoansLoanIdCollateralsCollateralIdResponse { public static final class PutLoansLoanIdCollateralsCollateralIdResponse {
@@ -107,27 +89,7 @@ final class CollateralsApiResourceSwagger {
public Long loanId; public Long loanId;
@Schema(example = "12") @Schema(example = "12")
public Long resourceId; public Long resourceId;
public PutLoansLoandIdCollateralsCollateralIdRequest changes; public LoansLoanIdCollateralsRequest changes;
}
@Schema(description = "GetLoansLoanIdCollateralsTemplateResponse")
public static final class GetLoansLoanIdCollateralsTemplateResponse {
private GetLoansLoanIdCollateralsTemplateResponse() {}
static final class GetCollateralsTemplateAllowedTypes {
private GetCollateralsTemplateAllowedTypes() {}
@Schema(example = "9")
public Long id;
@Schema(example = "Silver")
public String name;
@Schema(example = "0")
public Integer position;
}
public Set<GetCollateralsTemplateAllowedTypes> allowedCollateralTypes;
} }
@Schema(description = "DeleteLoansLoanIdCollateralsCollateralIdResponse") @Schema(description = "DeleteLoansLoanIdCollateralsCollateralIdResponse")
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.collateral.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class LoansLoanIdCollateralsRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long collateralTypeId;
}
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.collateral.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class LoansLoandIdCollateralsCollateralIdRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String description;
}