PackagingUnitUpdateController.java

/*
 * Copyright 2005-2025 the original author or authors.
 *
 * 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.openwms.wms.inventory.rest.pu;

import org.ameba.http.MeasuredRestController;
import org.openwms.core.http.AbstractWebController;
import org.openwms.wms.inventory.PackagingUnitService;
import org.openwms.wms.inventory.api.PackagingUnitVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import static java.lang.String.format;
import static org.openwms.wms.inventory.api.InventoryConstants.API_PACKAGING_UNITS;

/**
 * A PackagingUnitUpdateController.
 *
 * @author Heiko Scherrer
 */
@Validated
@MeasuredRestController
public class PackagingUnitUpdateController extends AbstractWebController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PackagingUnitUpdateController.class);
    protected final PackagingUnitService service;

    protected PackagingUnitUpdateController(PackagingUnitService service) {
        this.service = service;
    }

    @PutMapping(API_PACKAGING_UNITS + "/{pKey}")
    public ResponseEntity<PackagingUnitVO> update(
            @PathVariable("pKey") String pKey,
            @RequestBody PackagingUnitVO pu
    ) {
        if (!pKey.equals(pu.pKey)) {
            throw new IllegalArgumentException(format("The resource identified by [%s] does not match the one passed in the request [%s]", pKey, pu.pKey));
        }
        LOGGER.debug("Update PackagingUnit with pKey [{}] to [{}]", pKey, pu);
        pu = service.update(pKey, pu);
        return ResponseEntity.ok(pu);
    }
}