RepositoryGalleryController.java
/*
* Copyright 2022 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.admin.v1;
import java.nio.file.Paths;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.mvc.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController("adminRepositoryGalleryControllerV1")
@RequestMapping(RepositoryGalleryController.CONTROLLER_URL)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class RepositoryGalleryController extends BaseController {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/admin/r/g";
public static final Logger LOG = LoggerFactory.getLogger(RepositoryGalleryController.class);
@Autowired
private ImageGalleryService imageGalleryService;
@GetMapping(value = "/{page:\\d+}")
public Page<ImageGallery> listAllFiles(@PathVariable("page") int page) {
return imageGalleryService.listImageGalleries(PageRequest.of(page - 1, 50, Sort.by("path")));
}
@PutMapping(value = "")
public void updateMetadata(@RequestParam String galleryPath, @RequestBody ImageGallery imageGallery) throws Exception {
ImageGallery updatedGallery = imageGalleryService.loadImageGallery(Paths.get(galleryPath));
if (updatedGallery == null) {
imageGalleryService.createImageGallery(Paths.get(galleryPath), imageGallery.getTitle(), imageGallery.getDescription());
} else {
imageGalleryService.updateImageGalery(updatedGallery, imageGallery.getTitle(), imageGallery.getDescription());
}
}
@DeleteMapping(value = "")
public void deleteFile(@RequestParam String galleryPath) throws InvalidRepositoryPathException {
ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get(galleryPath));
imageGalleryService.removeGallery(imageGallery);
}
}