-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement/261 complete commercial endpoint functionality (#283)
Co-authored-by: Alexander Stanik <astanik@users.noreply.github.com>
- Loading branch information
1 parent
9943119
commit 6b14afb
Showing
11 changed files
with
703 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,4 +99,4 @@ | |
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
</project> |
101 changes: 101 additions & 0 deletions
101
remsfal-core/src/main/java/de/remsfal/core/api/project/CommercialEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package de.remsfal.core.api.project; | ||
|
||
import de.remsfal.core.api.ProjectEndpoint; | ||
import de.remsfal.core.validation.PatchValidation; | ||
import de.remsfal.core.validation.PostValidation; | ||
import de.remsfal.core.validation.UUID; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.groups.ConvertGroup; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.PATCH; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
|
||
|
||
import org.eclipse.microprofile.openapi.annotations.Operation; | ||
import org.eclipse.microprofile.openapi.annotations.headers.Header; | ||
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; | ||
|
||
import de.remsfal.core.json.project.CommercialJson; | ||
|
||
/** | ||
* Endpoint for managing Commercial properties within buildings. | ||
*/ | ||
@Path(ProjectEndpoint.CONTEXT + "/" + ProjectEndpoint.VERSION + "/" | ||
+ ProjectEndpoint.SERVICE + "/{projectId}/" + PropertyEndpoint.SERVICE | ||
+ "/{propertyId}/" + BuildingEndpoint.SERVICE | ||
+ "/{buildingId}/" + CommercialEndpoint.SERVICE) | ||
public interface CommercialEndpoint { | ||
|
||
String SERVICE = "commercials"; | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "Create a new commercial unit.") | ||
@APIResponse(responseCode = "201", description = "Commercial unit created successfully", | ||
headers = @Header(name = "Location", description = "URL of the new commercial")) | ||
Response createCommercial( | ||
@Parameter(description = "ID of the project", required = true) | ||
@PathParam("projectId") @NotNull @UUID String projectId, | ||
@Parameter(description = "ID of the building", required = true) | ||
@PathParam("buildingId") @NotNull @UUID String buildingId, | ||
@Parameter(description = "Commercial unit information", required = true) | ||
@Valid @ConvertGroup(to = PostValidation.class) CommercialJson commercial | ||
); | ||
|
||
@GET | ||
@Path("/{commercialId}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "Retrieve information about a commercial unit.") | ||
@APIResponse(responseCode = "404", description = "The commercial unit does not exist") | ||
@APIResponse(responseCode = "401", description = "No user authentication provided via session cookie") | ||
CommercialJson getCommercial( | ||
@Parameter(description = "ID of the project", required = true) | ||
@PathParam("projectId") @NotNull @UUID String projectId, | ||
@Parameter(description = "ID of the building", required = true) | ||
@PathParam("buildingId") @NotNull @UUID String buildingId, | ||
@Parameter(description = "ID of the commercial unit", required = true) | ||
@PathParam("commercialId") @NotNull @UUID String commercialId | ||
); | ||
|
||
@PATCH | ||
@Path("/{commercialId}") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation(summary = "Update information of a commercial unit") | ||
@APIResponse(responseCode = "401", description = "No user authentication provided via session cookie") | ||
@APIResponse(responseCode = "404", description = "The commercial unit does not exist") | ||
CommercialJson updateCommercial( | ||
@Parameter(description = "ID of the project", required = true) | ||
@PathParam("projectId") @NotNull @UUID String projectId, | ||
@Parameter(description = "ID of the building", required = true) | ||
@PathParam("buildingId") @NotNull @UUID String buildingId, | ||
@Parameter(description = "ID of the commercial unit", required = true) | ||
@PathParam("commercialId") @NotNull @UUID String commercialId, | ||
@Parameter(description = "Commercial unit object with information", required = true) | ||
@Valid @ConvertGroup(to = PatchValidation.class) CommercialJson commercial | ||
); | ||
|
||
@DELETE | ||
@Path("/{commercialId}") | ||
@Operation(summary = "Delete an existing commercial unit") | ||
@APIResponse(responseCode = "204", description = "The commercial unit was deleted successfully") | ||
@APIResponse(responseCode = "401", description = "No user authentication provided via session cookie") | ||
void deleteCommercial( | ||
@Parameter(description = "ID of the project", required = true) | ||
@PathParam("projectId") @NotNull @UUID String projectId, | ||
@Parameter(description = "ID of the building", required = true) | ||
@PathParam("buildingId") @NotNull @UUID String buildingId, | ||
@Parameter(description = "ID of the commercial unit", required = true) | ||
@PathParam("commercialId") @NotNull @UUID String commercialId | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
remsfal-service/src/main/java/de/remsfal/service/boundary/project/CommercialResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.remsfal.service.boundary.project; | ||
|
||
import de.remsfal.core.api.project.CommercialEndpoint; | ||
import de.remsfal.core.json.project.CommercialJson; | ||
import de.remsfal.core.model.project.CommercialModel; | ||
import de.remsfal.service.control.CommercialController; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* Resource for managing Commercial units via the API. | ||
*/ | ||
@RequestScoped | ||
public class CommercialResource extends ProjectSubResource implements CommercialEndpoint { | ||
|
||
@Inject | ||
CommercialController controller; | ||
|
||
@Override | ||
public Response createCommercial(final String projectId, final String buildingId, | ||
final CommercialJson commercial) { | ||
checkPrivileges(projectId); | ||
final CommercialModel model = controller.createCommercial(projectId, buildingId, commercial); | ||
final URI location = uri.getAbsolutePathBuilder().path(model.getId()).build(); | ||
return Response.created(location) | ||
.type(MediaType.APPLICATION_JSON) | ||
.entity(CommercialJson.valueOf(model)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public CommercialJson getCommercial(final String projectId, final String buildingId, final String commercialId) { | ||
checkPrivileges(projectId); | ||
return CommercialJson.valueOf(controller.getCommercial(projectId, buildingId, commercialId)); | ||
} | ||
|
||
@Override | ||
public CommercialJson updateCommercial(final String projectId, | ||
final String buildingId, final String commercialId, | ||
final CommercialJson commercial) { | ||
checkPrivileges(projectId); | ||
return CommercialJson.valueOf(controller.updateCommercial(projectId, buildingId, commercialId, commercial)); | ||
} | ||
|
||
@Override | ||
public void deleteCommercial(final String projectId, final String buildingId, | ||
final String commercialId) { | ||
checkPrivileges(projectId); | ||
controller.deleteCommercial(projectId, buildingId, commercialId); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
remsfal-service/src/main/java/de/remsfal/service/control/CommercialController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package de.remsfal.service.control; | ||
|
||
import de.remsfal.core.model.project.CommercialModel; | ||
import de.remsfal.service.entity.dao.CommercialRepository; | ||
import de.remsfal.service.entity.dto.CommercialEntity; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.NoResultException; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.ws.rs.NotFoundException; | ||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* Controller for managing Commercial units. | ||
*/ | ||
@RequestScoped | ||
public class CommercialController { | ||
|
||
@Inject | ||
Logger logger; | ||
|
||
@Inject | ||
CommercialRepository commercialRepository; | ||
|
||
@Inject | ||
TenancyController tenancyController; | ||
|
||
@Transactional | ||
public CommercialModel createCommercial(final String projectId, final String buildingId, | ||
final CommercialModel commercial) { | ||
logger.infov("Creating a commercial (projectId={0}, buildingId={1}, commercial={2})", | ||
projectId, buildingId, commercial); | ||
CommercialEntity entity = CommercialEntity.fromModel(commercial); | ||
entity.generateId(); | ||
entity.setProjectId(projectId); | ||
entity.setBuildingId(buildingId); | ||
commercialRepository.persistAndFlush(entity); | ||
commercialRepository.getEntityManager().refresh(entity); | ||
return getCommercial(projectId, buildingId, entity.getId()); | ||
} | ||
|
||
public CommercialModel getCommercial(final String projectId, | ||
final String buildingId, final String commercialId) { | ||
logger.infov("Retrieving a commercial (projectId={0}, buildingId={1}, commercialId={2})", | ||
projectId, buildingId, commercialId); | ||
CommercialEntity entity = commercialRepository.findCommercialById(projectId, buildingId,commercialId) | ||
.orElseThrow(() -> new NotFoundException("Commercial not exist")); | ||
|
||
if (!entity.getProjectId().equals(projectId)) { | ||
throw new NoResultException("Unable to find commercial, because the project ID is invalid"); | ||
} | ||
|
||
return entity; | ||
} | ||
|
||
@Transactional | ||
public CommercialModel updateCommercial(final String projectId, final String buildingId, final String commercialId, | ||
final CommercialModel commercial) { | ||
logger.infov("Updating a commercial (commercialId={0})", commercialId); | ||
CommercialEntity entity = commercialRepository.findCommercialById(projectId, buildingId, commercialId) | ||
.orElseThrow(() -> new NotFoundException("Commercial not exist")); | ||
|
||
if (commercial.getTitle() != null) { | ||
entity.setTitle(commercial.getTitle()); | ||
} | ||
if (commercial.getLocation() != null) { | ||
entity.setLocation(commercial.getLocation()); | ||
} | ||
if (commercial.getCommercialSpace() != null) { | ||
entity.setCommercialSpace(commercial.getCommercialSpace()); | ||
} | ||
if (commercial.getHeatingSpace() != null) { | ||
entity.setHeatingSpace(commercial.getHeatingSpace()); | ||
} | ||
if (commercial.getTenancy() != null){ | ||
entity.setTenancy(tenancyController.updateTenancy(projectId, entity.getTenancy(), commercial.getTenancy())); | ||
} | ||
return commercialRepository.merge(entity); | ||
} | ||
|
||
@Transactional | ||
public void deleteCommercial(final String projectId, final String buildingId, | ||
final String commercialId) throws NotFoundException { | ||
logger.infov("Delete a commercial (projectId{0} buildingId={1} commercialId{2})", | ||
projectId, buildingId, commercialId); | ||
if (commercialRepository.findCommercialById(projectId,buildingId,commercialId).isEmpty()) { | ||
throw new NotFoundException("Commercial does not exist"); | ||
} | ||
commercialRepository.deleteCommercialById(projectId, buildingId, commercialId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.