Message3Resource.java

/*
 * Copyright © 2024-2025 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.site;

import org.ctan.site.domain.site.Message;
import org.ctan.site.stores.MessageStore;
import org.ctan.site.stores.base.GeneralPage;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.DefaultValue;
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>Message3Resource</code> contains the CRUD controller for the
 * message resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0/admin")
@Produces(MediaType.APPLICATION_JSON)
public class Message3Resource {

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

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

        this.store = store;
    }

    /**
     * The method <code>retrieve</code> provides means to retrieve a message.
     *
     * @param id the id
     * @return the object or {@code null}
     */
    @GET
    @Path("/message/{id}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public Message get(@NonNull @PathParam("id") Long id) {

        return store.get(id);
    }

    /**
     * The method <code>list</code> provides means to retrieve a page of roles.
     *
     * @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 user or {@code null}
     */
    @GET
    @Path("/messages")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public GeneralPage list(@QueryParam("q") String q,
        @QueryParam("page") @DefaultValue("0") int page,
        @QueryParam("size") @DefaultValue("32") int size,
        @QueryParam("order") String orderBy,
        @QueryParam("asc") boolean asc) {

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

    /**
     * The method <code>remove</code> provides means to remove a message.
     *
     * @param id the id
     * @return the indicator whether the entity could have been removed
     */
    @DELETE
    @Path("/message/{id}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public boolean remove(@NonNull @PathParam("id") Long id) {

        return store.remove(id);
    }

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

        return store.save(message);
    }
}