package sklep.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import sklep.db.DBConnection;
import sklep.db.DBException;
import sklep.db.OrderDAO;
import sklep.db.RecordNotFound;
import sklep.model.Order;

@Path("/orders")
@Produces({ "application/xml", "application/json" })
@Consumes({ "application/xml", "application/json" })
public class ROrders {
	@GET
	@Path("/{id:[0-9][0-9]*}")
	public Order findById(@PathParam("id") final int orderId) throws DBException, RecordNotFound {
		try(DBConnection db = DBConnection.open()) {
			OrderDAO orderDAO = db.orderDAO();
			return orderDAO.findById(orderId);
		}
	}

	@GET
	public List<Order> listAll() throws DBException {
		try(DBConnection db = DBConnection.open()) {
			OrderDAO orderDAO = db.orderDAO();
			return orderDAO.readAll();
		}
	}

}
