Commit eca346af authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

CMS: API for deleting MenuItem

parent 3ddb9bf6
......@@ -421,6 +421,12 @@ public class CMSController {
return contentService.updateMenuItem(id, source);
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@DeleteMapping(value = "/menu-item/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
public MenuItem deleteMenuItem(@PathVariable(value = "id") final long id) {
return contentService.deleteMenuItem(id);
}
@PostMapping(value = "/transifex")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
public Article postToTransifex(@RequestParam("slug") String slug,
......
/**
* Copyright 2015 Global Crop Diversity Trust
/*
* Copyright 2021 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,14 +12,16 @@
* 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.genesys2.server.persistence;
import org.genesys2.server.model.impl.Menu;
import org.genesys2.server.model.impl.MenuItem;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MenuItemRepository extends JpaRepository<MenuItem, Long> {
boolean existsMenuItemsByMenu(Menu menu);
}
......@@ -211,5 +211,9 @@ public interface ContentService {
MenuItem updateMenuItem(long id, @NotNull MenuItem source);
MenuItem deleteMenuItem(long id);
MenuItem getMenuItem(long id);
void sanitizeAll();
}
......@@ -646,9 +646,7 @@ public class ContentServiceImpl implements ContentService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem updateMenuItem(final long id, final MenuItem source) {
MenuItem loaded = repoMenuItem.findById(id).orElseThrow(() -> {
throw new NotFoundElement("No such MenuItem");
});
MenuItem loaded = getMenuItem(id);
LOG.debug("Update menu item. Input data: {}", source);
loaded.setTarget(source.getTarget());
......@@ -661,6 +659,29 @@ public class ContentServiceImpl implements ContentService {
return repoMenuItem.save(loaded);
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public MenuItem deleteMenuItem(final long id) {
final MenuItem loaded = getMenuItem(id);
repoMenuItem.delete(loaded);
final Menu menu = loaded.getMenu();
if (menu != null && !repoMenuItem.existsMenuItemsByMenu(menu)) {
LOG.info("Deleting CMS menu {}", menu.getKey());
repoMenu.delete(menu);
}
return loaded;
}
@Override
public MenuItem getMenuItem(final long id) {
return repoMenuItem.findById(id).orElseThrow(() -> {
throw new NotFoundElement("No such MenuItem");
});
}
/**
* Update the CMS menu specified by key with the provided structure
*
......
......@@ -330,6 +330,65 @@ public class CMSControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
@Test
public void removeMenuItemTest() throws Exception {
final String menuKey = "testMenuKey";
MenuItem menuItem1 = contentService.ensureMenuItem(menuKey, "testURL", "testText");
assertThat(menuItem1, notNullValue());
assertThat(menuItem1.getId(), notNullValue());
assertThat(repoMenuItem.findById(menuItem1.getId()).isPresent(), is(true));
MenuItem menuItem2 = contentService.ensureMenuItem(menuKey, "testURL2", "testText2");
assertThat(menuItem2, notNullValue());
assertThat(menuItem2.getId(), notNullValue());
assertThat(repoMenuItem.findById(menuItem2.getId()).isPresent(), is(true));
/*@formatter:off*/
mockMvc
.perform(delete(CMSController.CONTROLLER_URL.concat("/menu-item/{id}"), menuItem2.getId())
.contentType(MediaType.APPLICATION_JSON_VALUE)
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.id", is(menuItem2.getId().intValue())))
.andExpect(jsonPath("$.text", is(menuItem2.getText())))
.andExpect(jsonPath("$.url", is(menuItem2.getUrl())))
;
/*@formatter:on*/
assertThat(repoMenuItem.findById(menuItem2.getId()).isPresent(), is(false));
assertThat(repoMenu.findByKey(menuKey), notNullValue());
}
@Test
public void removeTheLastMenuItemWithMenuTest() throws Exception {
final String menuKey = "testMenuKey";
MenuItem menuItem = contentService.ensureMenuItem(menuKey, "testURL", "testText");
assertThat(menuItem, notNullValue());
assertThat(menuItem.getId(), notNullValue());
assertThat(repoMenuItem.findById(menuItem.getId()).isPresent(), is(true));
/*@formatter:off*/
mockMvc
.perform(delete(CMSController.CONTROLLER_URL.concat("/menu-item/{id}"), menuItem.getId())
.contentType(MediaType.APPLICATION_JSON_VALUE)
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.id", is(menuItem.getId().intValue())))
.andExpect(jsonPath("$.text", is(menuItem.getText())))
.andExpect(jsonPath("$.url", is(menuItem.getUrl())))
;
/*@formatter:on*/
assertThat(repoMenuItem.findById(menuItem.getId()).isPresent(), is(false));
assertThat(repoMenu.findByKey(menuKey), nullValue());
}
@Test
@WithMockOAuth2Authentication(roles = { "ADMINISTRATOR" })
public void uploadFileToActivityPostFolderTest() throws Exception {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment