MenuItemController.java

/*
 * Copyright 2024 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.
 * You may obtain a copy of the License at
 *
 *   http://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.
 */

package org.genesys.server.api.v2.impl;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.v2.CRUDController;
import org.genesys.server.api.v2.TranslatedCRUDController;
import org.genesys.server.api.v2.facade.MenuItemApiService;
import org.genesys.server.api.v2.facade.MenuItemLangApiService;
import org.genesys.server.api.v2.model.impl.MenuDTO;
import org.genesys.server.api.v2.model.impl.MenuItemDTO;
import org.genesys.server.api.v2.model.impl.MenuItemLangDTO;
import org.genesys.server.api.v2.model.impl.TranslatedMenuItemDTO;
import org.genesys.server.model.impl.MenuItem;
import org.genesys.server.model.impl.MenuItemLang;
import org.genesys.server.service.filter.MenuItemFilter;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController("menuItemApi2")
@RequestMapping(MenuItemController.CONTROLLER_URL)
@Tag(name = "MenuItem")
public class MenuItemController extends TranslatedCRUDController<MenuItemDTO, TranslatedMenuItemDTO,
	MenuItemLangDTO, MenuItem, MenuItemLang, MenuItemApiService, MenuItemFilter> {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/menu-item";
	
	@GetMapping(value = "/menu/{key}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getMenu", description = "Get menu with items by key", summary = "GetMenu")
	public MenuDTO getMenu(@PathVariable("key") @Parameter(description = "Menu key") final String key) {
		return translatedApiService.getMenu(key);
	}
	
	/**
	 * Ensure menu item.
	 *
	 * @param menuKey the key of menu
	 * @param source the menu item to be created
	 * @return the menu item
	 */
	@RequestMapping(value = "/ensure-menu-item/{menuKey}", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public MenuItem ensureMenuItem(@PathVariable(value = "menuKey") final String menuKey, @RequestBody final MenuItem source) {
		return translatedApiService.ensureMenuItem(menuKey, source);
	}

	@RestController("menuItemLangApi2")
	@RequestMapping(MenuItemController.MenuItemLangController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "MenuItem")
	public static class MenuItemLangController extends CRUDController<MenuItemLangDTO, MenuItemLang, MenuItemLangApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = MenuItemController.CONTROLLER_URL + "/lang";
	}
}