Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
3
Indexable
Never
package gr.aegean.news;

import jakarta.annotation.security.PermitAll;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
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.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;


@Path("/news")
public class NewsController {
    private final NewsService newsService;

    public NewsController() {
        newsService = new NewsService();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed({"Editor", "Journalist"})
    public Response createNews(@Context UriInfo uriInfo, News news) throws URISyntaxException {
        newsService.createNews(news);

        return Response
                .status(201)
                .location(new URI(uriInfo.getPath() + "/" + news.getNewsID()))
                .build();
    }

    @PUT
    @Path("/{newsID}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed({"Editor", "Journalist"})
    public Response updateNews(@PathParam("newsID") int newsID, News news) {
        newsService.updateNews(newsID, news);

        return Response
                .status(204)
                .build();
    }

    @PUT
    @Path("/news/{newsID}")
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed({"Editor", "Journalist"})
    public Response updateNewsStatus(@PathParam("newsID") int newsID, @QueryParam("status") NewsStatus status) {
        newsService.updateNewsStatus(newsID, status);

        return Response
                .status(204)
                .build();
    }

    @GET
    @Path("/news")
    @Produces(MediaType.APPLICATION_JSON)
    public Response searchNews(@QueryParam("headline") String headline, @QueryParam("content") String content) {
        List<News> news = newsService.searchNews(headline, content);

        return Response
                .status(200)
                .entity(news)
                .build();
    }

    @GET
    @Path("/{newsID}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getNews(@PathParam("newsID") int newsID) {
        News news = newsService.getNews(newsID);

        return Response
                .status(200)
                .entity(news)
                .build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @PermitAll
    public Response getAllNews(@QueryParam("status") String status, @QueryParam("from") String from,
                               @QueryParam("to") String to) {
        List<News> news =  newsService.getAllNews(NewsStatus.valueOf(status), from, to);

        return Response
                .status(200)
                .entity(news)
                .build();
    }

    @GET
    @Path("/topic")
    @Produces(MediaType.APPLICATION_JSON)
    @PermitAll
    public Response getNewsByTopic(@QueryParam("topicID") int topicID, @QueryParam("title") String title) {
        List<News> news = newsService.getNewsByTopic(topicID, title);

        return Response
                .status(200)
                .entity(news)
                .build();
    }
}