package net.tinsae.shop;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)

@Path("/items")
public class Factory {
    private final ItemDao dao = new ItemDao();

    @GET
    public List<Item> getAllItems() {
        return dao.findAll();
    }

    @GET
    @Path("/{id}")
    public Item getItem(@PathParam("id") int id) {
        return dao.findById(id);
    }
    @POST
    public Response createItem(Item Item) {
        dao.insert(Item);
        return Response.status(Response.Status.CREATED).build();
    }

    @PUT
    @Path("/{id}")
    public Response updateItem(@PathParam("id") int id, Item Item) {
        Item.setId(id);
        dao.update(Item);
        return Response.ok().build();
    }

    @DELETE
    @Path("/{id}")
    public Response deleteItem(@PathParam("id") int id) {
        dao.delete(id);
        return Response.noContent().build();
    }
}