Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Luan #2

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
API: GET pedidos, POST pedido, DELETE pedido + refatoração
  • Loading branch information
lsandrade committed Jul 30, 2016
commit 730c0b8233ff1704fdc09447009bf3c325eefc89

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.luan.controllers;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.luan.models.Pedido;
import com.luan.services.PedidoService;

@Path("/pedidos")
public class PedidosController {

private PedidoService pedidoService = new PedidoService();

/* @GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {

String output = " Ol�, API. Seja bem vindo: " + msg;

return Response.status(200).entity(output).build();

}
*/
@GET
@Path("/{numero}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPedido(@PathParam("numero") int numero) {
Pedido pedido = pedidoService.getByNumero(numero);
return Response.status(200).entity(pedidoService.toJson(pedido)).build();
}

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getTodosOsPedidos() {
List<Pedido> pedidos = pedidoService.getAll();
return Response.status(200).entity(pedidoService.toJson(pedidos)).build();
}

@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response salvaPedido(String pedidoJson) {
Pedido pedido = pedidoService.fromJson(pedidoJson);
pedidoService.salvaPedido(pedido);
return Response.status(200).entity(pedidoService.toJson(pedido)).build();
}

@DELETE
@Path("/{numero}")
@Produces(MediaType.APPLICATION_JSON)
public Response deletePedido(@PathParam("numero") int numero) {
pedidoService.remover(numero);
return Response.status(200).entity("").build();
}


}
34 changes: 32 additions & 2 deletions contabilizei/src/main/java/com/luan/services/PedidoService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
package com.luan.services;

import java.util.List;

import com.google.gson.Gson;
import com.luan.dao.MongoDBPedidoDAO;
import com.luan.models.Pedido;

public class PedidoService {

private MongoDBPedidoDAO mdbpdao = new MongoDBPedidoDAO();

public Pedido getByNumero(int numero) {
MongoDBPedidoDAO mdbpdao = new MongoDBPedidoDAO();
return mdbpdao.buscarPorNumero(numero);
}

public List<Pedido> getAll() {
return mdbpdao.listarTodos();
}

public String toJson(Object object){
Gson gson = new Gson();
return gson.toJson(object);
}

public Pedido fromJson(String pedidoJson){
Gson gson = new Gson();
return gson.fromJson(pedidoJson, Pedido.class);
}

public void salvaPedido(Pedido pedido) {
pedido.setNumero(getNumero());
mdbpdao.inserir(pedido);
}

private int getNumero(){
return (mdbpdao.listarTodos().size()+1);
}

public void remover(int numero) {
mdbpdao.remover(numero);;
}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.