package pri.carol.jeff.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pri.carol.jeff.entity.EventEntity;
import pri.carol.jeff.entity.EventEntityManager;

import com.google.appengine.api.datastore.Key;

@SuppressWarnings("serial")
public class EventServiceImpl extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<EventEntity> list = new EventEntityManager().list();

		PrintWriter writer = resp.getWriter();
		
		writer.write(" <b>Eventos:</b> <br /> ");
		
		for (EventEntity eventEntity : list) {

			writer.write("<br /> Tipo do evento: " + eventEntity.getTipo());
			writer.write("<br /> Descricao do evento: " + eventEntity.getTipo());
		}
		writer.flush();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String tipo = req.getParameter("tipo");
		String descricao = req.getParameter("descricao");
		Key key = new EventEntityManager().persist(tipo, descricao);

		PrintWriter writer = resp.getWriter();
		writer.write("Key do evento: " + key.getId());
		writer.flush();
		super.doPost(req, resp);
	}
}
