package ms.itsecteam.warpdrink.provider; import java.sql.SQLException; import java.util.List; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import com.google.gson.Gson; import ms.itsecteam.warpdrink.UserDao; import ms.itsecteam.warpdrink.objects.User; @Path("user") public class UserProvider { List<User> users; public UserProvider() { try { users = UserDao.getInstance().getAllUsers(); } catch (SQLException e) { e.printStackTrace(); } } @GET @Path("/all") @Produces(MediaType.APPLICATION_JSON) public String getUsers() { List<User> users; try { users = UserDao.getInstance().getAllUsers(); if(users != null){ return new Gson().toJson(users,users.getClass()); } } catch (SQLException e) { e.printStackTrace(); } return ""; } @GET @Path("/{userid}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userid") int userid) { try { return UserDao.getInstance().getUser(userid); } catch (SQLException e) { e.printStackTrace(); } return null; } @POST @Path("/{userid}") @Produces(MediaType.APPLICATION_JSON) public void setUser(@PathParam("userid") int userid, @FormParam("credit") double credit) throws SQLException { User user = UserDao.getInstance().getUser(userid); user.setCredit(credit); UserDao.getInstance().saveUser(user); } @PUT @Path("/create") @Produces(MediaType.APPLICATION_JSON) public User createUser(@FormParam("name") String name) throws SQLException { if(UserDao.getInstance().getUser(name) == null){ User user = new User(name,0.0); UserDao.getInstance().saveUser(user); return user; } return null; } }