@POST @Path("/login") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response authenticate( @FormDataParam("username") String userId, @FormDataParam("password") String password) { Session session = databaseManager.getSession(); ResultSet user = session.execute("SELECT * FROM righteous.user where user_id = '" + userId + "'"); Row row = null; if (user.isExhausted()) { session.close(); return Response.status(200).entity("Invalid Username or Password").build(); } else { row = user.one(); } if (row.getString("user_id").equals(userId) && row.getString("user_password").equals(password)) { session.close(); return Response.status(200).entity("success").build(); } else { session.close(); return Response.status(200).entity("Invalid Username or Password").build(); } }
@POST @Path("/register") public Response register( @FormDataParam("id") String userId, @FormDataParam("username") String userName, @FormDataParam("password") String password, @FormDataParam("dob") String dob, @FormDataParam("gender") String gender, @FormDataParam("location") String location, @FormDataParam("proPic") InputStream proPicInputStream, @FormDataParam("proPic") FormDataContentDisposition proPicFileDetail) { Session session = databaseManager.getSession(); ResultSet users = session.execute("SELECT * FROM righteous.user"); Calendar calendar = new GregorianCalendar(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = simpleDateFormat.parse(dob); } catch (ParseException e1) { log.error("", e1); } calendar.setTime(date); for (Row row : users) { if (row.getString("user_id").equalsIgnoreCase(userId)) { return Response.status(200).entity("Username unavailable").build(); } } byte[] imageBytes = null; try { imageBytes = org.apache.commons.io.IOUtils.toByteArray(proPicInputStream); } catch (IOException e) { log.error("Unable to retrieve the image", e); } ByteBuffer imageByteBuffer = ByteBuffer.wrap(imageBytes); PreparedStatement preparedStatement = session.prepare( "INSERT INTO righteous.user (user_id, user_name, user_password, user_dob, user_gender, user_location, user_pic) " + "VALUES (?,?,?,?,?,?,?)"); BoundStatement boundStatement = new BoundStatement(preparedStatement); session.execute( boundStatement.bind( userId, userName, password, calendar.getTime(), gender, location, imageByteBuffer)); session.close(); return Response.status(200).entity("Registration successful").build(); }
static { databaseManager = DatabaseManager.getInstance(); }