CrudTopic3Resource.java

/*
 * Copyright © 2024-2026 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.resources.admin;

import org.ctan.site.domain.catalogue.Topic;
import org.ctan.site.stores.TopicStore;
import org.ctan.site.stores.base.GeneralPage;

import com.google.common.collect.ImmutableMap;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import lombok.NonNull;

/**
 * The class <code>CrudTopic3Resource</code> contains the CRUD controller for
 * the topics resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0/admin")
@Produces(MediaType.APPLICATION_JSON)
// @RolesAllowed("ADMIN")
public class CrudTopic3Resource {

    /**
     * The field <code>store</code> contains the underlying store.
     */
    private TopicStore store;

    /**
     * This is the constructor for the class <code>CrudLicense3Resource</code>.
     *
     * @param store the underlying store
     */
    @SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
    public CrudTopic3Resource(@NonNull TopicStore store) {

        this.store = store;
    }

    /**
     * The method <code>create</code> provides an end-point to create a license.
     *
     * @param topic the topic object to store
     * @return the updated entity
     */
    @POST
    @Path("/topic")
    @UnitOfWork(value = "siteDb")
    public Topic create(@NonNull Topic topic) {

        return store.save(topic);
    }

    /**
     * The method <code>get</code> provides an end-point to get a license.
     *
     * @param id the id
     * @return the license
     */
    @GET
    @Path("/topic/{id}")
    @UnitOfWork(value = "siteDb")
    public ImmutableMap<String, Object> get(@NonNull @PathParam("id") Long id) {

        Topic topic = store.get(id);
        if (topic == null) {
            return null;
        }

        return ImmutableMap.of("id", (Object) topic.getId(),
            "key", topic.getKey(),
            "en", topic.getDetails("en"),
            "de", topic.getDetails("de"));
    }

    /**
     * The method <code>list</code> provides means to retrieve a page of topics.
     *
     * @param q the name pattern
     * @param page the page
     * @param size the page size
     * @param orderBy the order
     * @param asc the indicator for ascending
     * @return the page for the topics or {@code null}
     */
    @GET
    @Path("/topics")
    @UnitOfWork(value = "siteDb")
    public GeneralPage list(@QueryParam("q") String q,
        @QueryParam("page") int page,
        @QueryParam("size") int size,
        @QueryParam("order") String orderBy,
        @QueryParam("asc") boolean asc) {

        return store.list(q, page - 1, size, orderBy, asc);
    }

    /**
     * The method <code>remove</code> provides an end-point to remove a ticket.
     *
     * @param id the id
     * @return {@code true} iff the user has existed
     */
    @DELETE
    @Path("/topic/{id}")
    @UnitOfWork(value = "siteDb")
    public boolean remove(@PathParam("id") Long id) {

        return store.remove(id);
    }
}