Skip to content

Commit

Permalink
Improve validation and error messages in NetworkResource
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-podolskiy90 committed Nov 24, 2021
1 parent 7f4fb1c commit 6bcce6d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public interface NetworkMapper extends BaseNetworkEntityMapper<Network> {

int countDatasetsInNetwork(@Param("networkKey") UUID networkKey);

boolean constituentExists(
@Param("networkKey") UUID networkKey, @Param("datasetKey") UUID datasetKey);

void addDatasetConstituent(
@Param("networkKey") UUID networkKey, @Param("datasetKey") UUID datasetKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@
network_key = #{targetEntityKey,jdbcType=OTHER} AND comment_key = #{commentKey,jdbcType=INTEGER}
</delete>

<select id="constituentExists" resultType="java.lang.Boolean">
SELECT EXISTS (
SELECT *
FROM dataset_network
WHERE dataset_key = #{datasetKey,jdbcType=OTHER} AND network_key = #{networkKey,jdbcType=OTHER}
)
</select>

<!-- DATASET CONSTITUENTS -->
<insert id="addDatasetConstituent">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private void existNetworkCheck(UUID networkKey) {
@Secured({ADMIN_ROLE, EDITOR_ROLE, IPT_ROLE})
@Override
public void addConstituent(@PathVariable("key") UUID networkKey, @PathVariable UUID datasetKey) {
if (networkMapper.constituentExists(networkKey, datasetKey)) {
throw new WebApplicationException(
"Dataset " + datasetKey + " is already connected to the network " + networkKey, HttpStatus.BAD_REQUEST);
}
existDatasetCheck(datasetKey);
existNetworkCheck(networkKey);
networkMapper.addDatasetConstituent(networkKey, datasetKey);
Expand All @@ -158,8 +162,10 @@ public void addConstituent(@PathVariable("key") UUID networkKey, @PathVariable U
@Override
public void removeConstituent(
@PathVariable("key") UUID networkKey, @PathVariable UUID datasetKey) {
existDatasetCheck(datasetKey);
existNetworkCheck(networkKey);
if (!networkMapper.constituentExists(networkKey, datasetKey)) {
throw new WebApplicationException(
"Dataset " + datasetKey + " is not connected to the network " + networkKey, HttpStatus.BAD_REQUEST);
}
networkMapper.deleteDatasetConstituent(networkKey, datasetKey);
eventManager.post(ChangedComponentEvent.newInstance(datasetKey, Network.class, Dataset.class));
}
Expand Down

0 comments on commit 6bcce6d

Please sign in to comment.