Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/api for entity management #42

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ maven/mavencentral/jakarta.annotation/jakarta.annotation-api/2.1.1, EPL-2.0 OR G
maven/mavencentral/jakarta.inject/jakarta.inject-api/2.0.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/jakarta.persistence/jakarta.persistence-api/3.1.0, EPL-2.0 OR BSD-3-Clause AND (EPL-2.0 OR BSD-3-Clause AND BSD-3-Clause), approved, #7696
maven/mavencentral/jakarta.transaction/jakarta.transaction-api/2.0.1, EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0, approved, #7697
maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, ee4j.validation
tom-rm-meyer-ISST marked this conversation as resolved.
Show resolved Hide resolved
maven/mavencentral/jakarta.xml.bind/jakarta.xml.bind-api/4.0.0, BSD-3-Clause, approved, ee4j.jaxb
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.12.22, Apache-2.0, approved, #1810
maven/mavencentral/net.bytebuddy/byte-buddy/1.12.22, Apache-2.0 AND BSD-3-Clause, approved, #1811
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ private Partner createAndGetSupplierPartner() {
private Partner createAndGetNonScenarioCustomer() {
Partner nonScenarioCustomer = new Partner(
"Non-Scenario Customer",
"(None Provided!)>",
"http://nonscenario-customer.com/api/v1/ids",
"BPNL2222222222RR",
"BPNS2222222222XZ",
"Zentraleinkaufsabteilung",
"BPNA2222222222HH",
"54.321N, 8.7654E"
"BPNA2222222222XZ",
"Fichtenweg 23",
"65432 Waldhausen",
"Germany"
);
nonScenarioCustomer = partnerService.create(nonScenarioCustomer);
log.info(String.format("Created non-scenario customer partner: %s", nonScenarioCustomer));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.tractusx.puris.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.MaterialEntityDto;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("materials")
public class MaterialController {

@Autowired
private MaterialService materialService;
private ModelMapper modelMapper = new ModelMapper();

@PostMapping
@CrossOrigin
@Operation(description = "Creates a new Material entity with the data given in the request body. As a bare minimum, " +
"it must contain a new, unique ownMaterialNumber.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully created a new Material entity."),
@ApiResponse(responseCode = "400", description = "Malformed request body"),
@ApiResponse(responseCode = "409", description = "Material with the given ownMaterialNumber already exists."),
@ApiResponse(responseCode = "500", description = "Internal Server error")
})
public ResponseEntity<?> createMaterial(@RequestBody MaterialEntityDto materialDto) {
if (materialDto.getOwnMaterialNumber() == null || materialDto.getOwnMaterialNumber().isEmpty()) {
// Cannot create material without ownMaterialNumber
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}
if (materialService.findByOwnMaterialNumber(materialDto.getOwnMaterialNumber()) != null) {
// Cannot create material, ownMaterialNumber is already assigned
return new ResponseEntity<>(HttpStatusCode.valueOf(409));
}
Material createdMaterial;
try {
createdMaterial = modelMapper.map(materialDto, Material.class);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}

createdMaterial = materialService.create(createdMaterial);
if (createdMaterial == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(500));
}

return new ResponseEntity<>(HttpStatusCode.valueOf(200));
}

@PutMapping
@CrossOrigin
@Operation(description = "Updates an existing Material entity with the data given in the request body.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Update was accepted."),
@ApiResponse(responseCode = "400", description = "Malformed request body."),
@ApiResponse(responseCode = "404", description = "No existing Material Entity found, no update was performed."),
@ApiResponse(responseCode = "500", description = "Internal Server Error.")
})
public ResponseEntity<?> updateMaterial(@RequestBody MaterialEntityDto materialDto) {
if (materialDto.getOwnMaterialNumber() == null || materialDto.getOwnMaterialNumber().isEmpty()) {
// Cannot update material without ownMaterialNumber
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}
Material existingMaterial = materialService.findByOwnMaterialNumber(materialDto.getOwnMaterialNumber());
if (existingMaterial == null) {
// Cannot update non-existent Material
return new ResponseEntity<>(HttpStatusCode.valueOf(404));
}
Material updatedMaterial;
try {
updatedMaterial = modelMapper.map(materialDto, Material.class);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}
updatedMaterial = materialService.update(updatedMaterial);
if (updatedMaterial == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(500));
}

return new ResponseEntity<>(HttpStatusCode.valueOf(200));
}

@GetMapping
@CrossOrigin
@Operation(description = "Returns the requested Material dto, specified by the given ownMaterialNumber.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Returns the requested Material."),
@ApiResponse(responseCode = "404", description = "Requested Material was not found.")
})
public ResponseEntity<MaterialEntityDto> getMaterial(@RequestParam String ownMaterialNumber) {
eschrewe marked this conversation as resolved.
Show resolved Hide resolved
Material foundMaterial = materialService.findByOwnMaterialNumber(ownMaterialNumber);
if (foundMaterial == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(404));
}

MaterialEntityDto dto = modelMapper.map(foundMaterial, MaterialEntityDto.class);
return new ResponseEntity<>(dto, HttpStatusCode.valueOf(200));
}

@CrossOrigin
@GetMapping("/all")
@Operation(description = "Returns a list of all Materials and Products. ")
public ResponseEntity<List<MaterialEntityDto>> listMaterials() {
ArrayList<MaterialEntityDto> outputList = new ArrayList<>();
materialService.findAll().stream().forEach(mat -> outputList.add(modelMapper.map(mat, MaterialEntityDto.class)));
return new ResponseEntity<>(outputList, HttpStatusCode.valueOf(200));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.tractusx.puris.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("materialpartnerrelations")
public class MaterialPartnerRelationsController {


@Autowired
private MaterialService materialService;

@Autowired
private PartnerService partnerService;

@Autowired
private MaterialPartnerRelationService mprService;

@PostMapping
@CrossOrigin
@Operation(description = "Creates a new MaterialPartnerRelation with the given parameter data. You have to provide " +
"either the UUID or the BPNL of the Partner. Please note that this is only possible, if the designated Material " +
"and Partner entities have already been created before to this request. ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully created a new MaterialPartnerRelationEntity."),
@ApiResponse(responseCode = "400", description = "Material and/or Partner do not exist."),
@ApiResponse(responseCode = "409", description = "Relation for given Material and Partner does already exist."),
@ApiResponse(responseCode = "500", description = "Internal Server Error")
})
public ResponseEntity<?> createMaterialPartnerRelation(@RequestParam String ownMaterialNumber,
eschrewe marked this conversation as resolved.
Show resolved Hide resolved
@RequestParam(required = false) UUID partnerUuid,
@RequestParam(required = false) String partnerBpnl,
@RequestParam String partnerMaterialNumber,
@RequestParam boolean partnerSupplies,
@RequestParam boolean partnerBuys) {
Material material = materialService.findByOwnMaterialNumber(ownMaterialNumber);
tom-rm-meyer-ISST marked this conversation as resolved.
Show resolved Hide resolved
if (material == null || (partnerUuid == null && partnerBpnl == null)) {
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}
Partner partner = null;
if (partnerUuid != null) {
partner = partnerService.findByUuid(partnerUuid);
}
if (partner == null && partnerBpnl != null) {
partner = partnerService.findByBpnl(partnerBpnl);
}
if (partner == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}

if (mprService.find(material, partner) != null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(409));
}
MaterialPartnerRelation newMpr = new MaterialPartnerRelation(material, partner, partnerMaterialNumber, partnerSupplies, partnerBuys);

newMpr = mprService.create(newMpr);
if (newMpr == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(500));
}

return new ResponseEntity<>(HttpStatusCode.valueOf(200));
}

@PutMapping
@CrossOrigin
@Operation(description = "Updates an existing MaterialPartnerRelation. You have to specify the ownMaterialNumber and " +
"at least one of either partnerUuid or partnerBpnl. The other three parameters are genuinely optional. Provide them " +
"only if you want to change their values. ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Update was accepted."),
@ApiResponse(responseCode = "404", description = "No existing entity was found. "),
@ApiResponse(responseCode = "500", description = "Internal Server Error")
})
public ResponseEntity<?> updateMaterialPartnerRelation(@RequestParam String ownMaterialNumber,
@RequestParam(required = false) UUID partnerUuid,
eschrewe marked this conversation as resolved.
Show resolved Hide resolved
@RequestParam(required = false) String partnerBpnl,
@RequestParam(required = false) String partnerMaterialNumber,
@RequestParam(required = false) Boolean partnerSupplies,
@RequestParam(required = false) Boolean partnerBuys) {
MaterialPartnerRelation existingRelation = null;
if (partnerUuid != null) {
existingRelation = mprService.find(ownMaterialNumber, partnerUuid);
}
if (existingRelation == null && partnerBpnl != null) {
Partner partner = partnerService.findByBpnl(partnerBpnl);
Material material = materialService.findByOwnMaterialNumber(ownMaterialNumber);
if (partner != null && material != null) {
existingRelation = mprService.find(material, partner);
}
}
if (existingRelation == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(404));
}
if (partnerSupplies != null) {
existingRelation.setPartnerSuppliesMaterial(partnerSupplies);
}
if (partnerBuys != null) {
existingRelation.setPartnerBuysMaterial(partnerBuys);
}
if (partnerMaterialNumber != null) {
existingRelation.setPartnerMaterialNumber(partnerMaterialNumber);
}
existingRelation = mprService.update(existingRelation);
if (existingRelation == null) {
return new ResponseEntity<>(HttpStatusCode.valueOf(500));
}

return new ResponseEntity<>(HttpStatusCode.valueOf(200));
}

}
Loading
Loading