Skip to content

Commit 73dffde

Browse files
committed
#82 adds rest endpoint and services methods to handle deletion of a snapshot
1 parent 9a7d111 commit 73dffde

5 files changed

Lines changed: 205 additions & 128 deletions

File tree

src/main/java/com/arcadeanalytics/provider/FileSystemDataProvider.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -109,6 +109,22 @@ public void deleteAllSnapshots(Widget widget) {
109109

110110
}
111111

112+
public boolean deleteSnapshot(Widget widget, String filename) {
113+
114+
final Long id = widget.getId();
115+
116+
final Path file = widgets.resolve(id.toString()).resolve(filename);
117+
log.info("deleting snapshot :: {}", file);
118+
try {
119+
return Files.deleteIfExists(file);
120+
} catch (IOException e) {
121+
log.error("unable to delete snapshot {} due to {} ", filename, e.getMessage());
122+
return false;
123+
}
124+
125+
}
126+
127+
112128
public List<String> getAllSnapshots(Widget widget) {
113129
final Long id = widget.getId();
114130
log.info("getting all snapshots of widget:: {} ", id);

src/main/java/com/arcadeanalytics/repository/FileSystemRepository.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -81,6 +81,19 @@ public byte[] read(String path) {
8181
}
8282

8383

84+
public boolean delete(String path) {
85+
final Path file = resolve(path);
86+
log.info("delete :: {} ", file.toAbsolutePath());
87+
88+
try {
89+
return Files.deleteIfExists(file);
90+
} catch (IOException e) {
91+
log.error("unable to delete :: {} due to {} ", file.toAbsolutePath(), e.getMessage());
92+
}
93+
94+
return false;
95+
}
96+
8497
public Path resolve(String path) {
8598

8699
return rootPath.resolve(path);

src/main/java/com/arcadeanalytics/service/WidgetService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -382,6 +382,15 @@ public boolean saveSnapshot(Long id, String snapshotData) {
382382

383383
}
384384

385+
@Transactional
386+
public boolean deleteSnapshot(Long id, String fileName) {
387+
388+
return getWidgetIfAllowed(id)
389+
.map(widget -> new FileSystemDataProvider(fsRepository)
390+
.deleteSnapshot(widget, SNAPSHOT_PREFIX + fileName))
391+
.orElse(false);
392+
}
393+
385394
public String layout(final String layoutType, final String json) {
386395

387396
return new LayoutUtils().applyLayout(layoutType, json);

src/main/java/com/arcadeanalytics/web/rest/WidgetResource.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -135,7 +135,7 @@ public ResponseEntity<List<WidgetDTO>> getAllWidgets(@ApiParam Pageable pageable
135135
/**
136136
* GET /widgets : get all the widgets.
137137
*
138-
* @param id {@link com.arcadeanalytics.domain.Dashboard} id
138+
* @param id {@link com.arcadeanalytics.domain.Dashboard} id
139139
* @param pageable the pagination information
140140
* @return the ResponseEntity with status 200 (OK) and the list of widgets in body
141141
*/
@@ -242,6 +242,14 @@ public ResponseEntity<String> getLatestWidgetSnapshot(@PathVariable Long id, @Re
242242
return ResponseUtil.wrapOrNotFound(data);
243243
}
244244

245+
@DeleteMapping(value = "/widgets/snapshot/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
246+
@Timed
247+
public ResponseEntity<Boolean> deleteWidgetSnapshot(@PathVariable Long id, @RequestParam(defaultValue = "last") String fileName) {
248+
log.debug("REST request to delete Widget snapshot: {} ", fileName);
249+
250+
return ResponseUtil.wrapOrNotFound(Optional.of(widgetService.deleteSnapshot(id, fileName)));
251+
}
252+
245253

246254
@GetMapping(value = "/widgets/snapshots/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
247255
@Timed

0 commit comments

Comments
 (0)