protected void initWebServer(ServiceProvider serviceProvider, IdeArguments ideArguments) { port(ideArguments.getPort()); staticFileLocation("/public"); externalStaticFileLocation(staticFolderForSpark); System.out.println("Reports are in: " + reportFolder); new DeviceController(serviceProvider.deviceService()); new DomSnapshotController(serviceProvider.domSnapshotService()); new FileBrowserController(serviceProvider.fileBrowserService()); new SettingsController(serviceProvider.settingsService()); new ProfilesController(serviceProvider.profilesService(), serviceProvider.settingsService()); new TaskResultController(serviceProvider.taskResultService()); new TesterController(serviceProvider.testerService()); new HelpController(); scheduledExecutorService.scheduleAtFixedRate( new TaskResultsStorageCleanupJob( taskResultsStorage, ideArguments.getKeepLastResults(), ideArguments.getZombieResultsTimeout(), reportFolder), ideArguments.getCleanupPeriodInMinutes(), ideArguments.getCleanupPeriodInMinutes(), TimeUnit.MINUTES); if (ideArguments.getProfile() != null) { serviceProvider.profilesService().loadProfile(ideArguments.getProfile()); } }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/rockpaperscissor.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/detector", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/detector.vtl"); String p1 = request.queryParams("Player1"); String p2 = request.queryParams("Player2"); Boolean isplayer1Wins = isplayer1Wins(p1, p2); model.put("isplayer1Wins", isplayer1Wins); model.put("Player1", p1); model.put("Player2", p2); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", request.session().attribute("tasks")); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); ArrayList<Task> tasks = request.session().attribute("tasks"); if (tasks == null) { tasks = new ArrayList<Task>(); request.session().attribute("tasks", tasks); } String description = request.queryParams("description"); Task newTask = new Task(description); tasks.add(newTask); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public void start() { port(port); Map<String, String> map = new HashMap<>(); staticFileLocation("/public"); get("/", (req, res) -> new ModelAndView(map, "main"), new JadeTemplateEngine()); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", request.session().attribute("tasks")); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", Task.all()); model.put("template", "templates/tasks.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "tasks/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/task-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String description = request.queryParams("description"); Task newTask = new Task(description); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/tasks/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Task task = Task.find(Integer.parseInt(request.params(":id"))); model.put("task", task); model.put("template", "templates/task.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { port(Integer.valueOf(System.getenv("PORT"))); staticFileLocation("/public"); // get("/hello", (req, res) -> "Hello World"); // get("/hello", (req, res) -> { // RelativisticModel.select(); // String energy = System.getenv().get("ENERGY"); // Amount<Mass> m = Amount.valueOf(energy).to(KILOGRAM); // return "E=mc^2: " + energy + " = " + m.toString(); // }); // get("/", (request, response) -> { // Map<String, Object> attributes = new HashMap<>(); // attributes.put("message", "Hello World!"); // return new ModelAndView(attributes, "index.ftl"); // }, new FreeMarkerEngine()); get( "/db", (req, res) -> { Connection connection = null; Map<String, Object> attributes = new HashMap<>(); try { connection = DatabaseUrl.extract().getConnection(); Statement stmt = connection.createStatement(); stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); stmt.executeUpdate("INSERT INTO ticks VALUES (now())"); ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks"); ArrayList<String> output = new ArrayList<String>(); while (rs.next()) { output.add("Read from DB: " + rs.getTimestamp("tick")); } attributes.put("results", output); return new ModelAndView(attributes, "db.ftl"); } catch (Exception e) { attributes.put("message", "There was an error: " + e); return new ModelAndView(attributes, "error.ftl"); } finally { if (connection != null) try { connection.close(); } catch (SQLException e) { } } }, new FreeMarkerEngine()); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/catalog", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/catalog.vtl"); ArrayList<String> artistList = new ArrayList<String>(); artistList = CDOrganizer.artistsList(); model.put("artistList", artistList); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/catalog/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/new-CD-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/catalog", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { staticFileLocation("/public"); get( "/", (request, response) -> { // "/" will automatically take us to the root url HashMap model = new HashMap(); model.put("template", "templates/hello.vtl"); return new ModelAndView(model, "templates/layout.vtl"); }, new VelocityTemplateEngine()); get( "/favorite_photos", (request, response) -> { HashMap model = new HashMap(); model.put("template", "templates/favorite_photos.vtl"); return new ModelAndView(model, "templates/layout.vtl"); }, new VelocityTemplateEngine()); get( "/form", (request, response) -> { HashMap model = new HashMap(); model.put("template", "templates/form.vtl"); return new ModelAndView(model, "templates/layout.vtl"); }, new VelocityTemplateEngine()); get( "/greeting_card", (request, response) -> { HashMap model = new HashMap(); String recipient = request.queryParams("recipient"); String sender = request.queryParams("sender"); model.put("recipient", recipient); model.put("sender", sender); model.put("template", "templates/greeting_card.vtl"); return new ModelAndView(model, "templates/layout.vtl"); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { ArrayList<People> peoples = new ArrayList<People>(); TwilioRestClient client = new TwilioRestClient( "AC24dcb8ff451fefca20dcf24db7169cc6", "1232ab47c4eb8674dffe239456c4c582"); // replace this Account mainAccount = client.getAccount(); staticFileLocation("/public"); // Static files get( "/name", (req, res) -> { String name = req.queryParams("foo"); return "Hello, " + name; }); get( "/form", (req, res) -> { String delay = req.queryParams("delay"); int delayInt = Integer.parseInt(delay); String phoneNum = req.queryParams("phoneNum"); String questions = req.queryParams("questions"); peoples.add(new People(phoneNum, delayInt, questions)); return "Message Sent :)"; }); post( "/sms", (req, res) -> { TwiMLResponse twiml = new TwiMLResponse(); twiml.append(new Message("Notes Stopped")); return twiml.toXML(); }); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; Song song = new Song("Michael Jackson", "Beat It", "https://www.youtube.com/watch?v=SaEC9i9QOvk"); SongBook songBook = new SongBook(); songBook.addSong(song); get( "/", (request, response) -> { Map<String, Object> model = new HashMap<>(); model.put("template", "templates/index.vtl"); model.put("song", song); model.put("songbook", songBook); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
private void start() { Spark.port(port); Spark.ipAddress(listen); Spark.staticFileLocation(view); logger.info("API Token: " + token); Spark.before( "/api/*", new Filter() { @Override public void handle(Request req, Response resp) throws Exception { if (!token.equals(req.queryParams("token"))) { Spark.halt(403); } } }); new FullReportRoute(settings); new RunRoute(settings); new RunHTMLRoute(settings); new SearchHTMLRoute(settings); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/players.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/playerOne", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); String playerOne = request.queryParams("playerOne"); request.session().attribute("playerOne", playerOne); String playerTwo = request.queryParams("playerTwo"); request.session().attribute("playerTwo", playerTwo); Player player1 = new Player(playerOne); Player player2 = new Player(playerTwo); Deck gameDeck = new Deck(); // gameDeck.getFullDeck() Card testCard = new Card("test", "card"); ArrayList<Card> playerOneCards = player1.get7Cards(gameDeck); ArrayList<Card> playerTwoCards = player2.get7Cards(gameDeck); model.put("playerOneCards", playerOneCards); model.put("playerTwoCards", playerTwoCards); model.put("testcard", testCard); model.put("gameDeck", gameDeck.getFullDeck()); model.put("template", "templates/playerOne.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { // precisa de um package publico (pub) // onde ficam os css, js, imgs, conteúdo fixo Spark.staticFileLocation("/pub"); // precisa de um package apresentacao (views) // onde ficam os HTML's MustacheTemplateEngine engine = new MustacheTemplateEngine("apresentacao"); PaginaInicialControlador paginaInicial = new PaginaInicialControlador(); // get ou post (get ler informações, post submeter informações) Spark.get("/", paginaInicial, engine); NovoControlador novoControlador = new NovoControlador(); // abrir o form Spark.get("/novo", novoControlador, engine); SalvaControlador salvaControlador = new SalvaControlador(); // submissão do form Spark.post("/salva", salvaControlador, engine); ListaControlador listaControlador = new ListaControlador(); Spark.get("/lista", listaControlador, engine); ExcluiControlador excluiControlador = new ExcluiControlador(); Spark.get("/exclui/:numero", excluiControlador, engine); // FotoControlador upador = new FotoControlador(); // Spark.post("/recebefoto", upador); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("stylists", Stylist.all()); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/add-stylist", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/add-stylist.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/stylists", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String stylist = request.queryParams("stylist_name"); Stylist newStylist = new Stylist(stylist); newStylist.save(); model.put("stylists", Stylist.all()); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/delete/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); int stylist_id = Integer.parseInt(request.params(":id")); Stylist.deleteStylistById(stylist_id); model.put("stylists", Stylist.all()); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylist/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); int stylist_id = Integer.parseInt(request.params(":id")); model.put("clients", Client.getClientsByStylistId(stylist_id)); model.put("stylist", Stylist.find(stylist_id)); model.put("template", "templates/stylist.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/client/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String client_name = request.queryParams("client_name"); int stylist_id = Integer.parseInt(request.queryParams("stylistId")); Client newClient = new Client(client_name, stylist_id); if (client_name != null) { newClient.save(); } model.put("stylist", Stylist.find(Integer.parseInt(request.params(":id")))); model.put("clients", Client.getClientsByStylistId(stylist_id)); model.put("template", "templates/stylist.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/delete-client/:id/stylist/:stylist_id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); int client_id = Integer.parseInt(request.params(":id")); Client.deleteClientById(client_id); Integer stylist_id = Integer.parseInt(request.params(":stylist_id")); model.put("stylist", Stylist.find(stylist_id)); model.put("clients", Client.getClientsByStylistId(stylist_id)); model.put("template", "templates/stylist.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; // homepage with a list of all the restaurants get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); // start session String inputtedUsername = request.queryParams("username"); // check for not-null username if (inputtedUsername != null) { request.session().attribute("username", inputtedUsername); model.put("username", inputtedUsername); User newUser = new User(inputtedUsername, "123"); if (newUser.checkExistingUser(inputtedUsername) != null) { Integer userId = newUser.checkExistingUser(inputtedUsername); request.session().attribute("userId", userId); } else { newUser.save(); Integer userId = newUser.getId(); request.session().attribute("userId", userId); } } List<Restaurant> restaurants = Restaurant.all(); List<String> types = Restaurant.listTypes(); model.put("restaurants", restaurants); model.put("types", types); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // form to add new restaurant (same?) get( "/restaurants/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/restaurant-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // after adding new restaurant update restaurant list post( "/restaurants", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String name = request.queryParams("name"); String type = request.queryParams("type"); String veganString = request.queryParams("vegan"); boolean vegan = veganString.equals("yes"); String price_range = request.queryParams("price_range"); String area = request.queryParams("area"); String address = request.queryParams("address"); String phone = request.queryParams("phone"); String website = request.queryParams("website"); int ranking_average = Integer.parseInt(request.queryParams("ranking_average")); Restaurant newRestaurant = new Restaurant( name, type, ranking_average, vegan, price_range, area, address, phone, website); newRestaurant.save(); List<Restaurant> restaurants = Restaurant.all(); List<String> types = Restaurant.listTypes(); model.put("restaurants", restaurants); model.put("types", types); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // page for a specific restaurant get( "/restaurants/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); int restaurantId = Integer.parseInt(request.params(":id")); Restaurant restaurant = Restaurant.find(restaurantId); // get the username from the session model.put("username", request.session().attribute("username")); Integer reviewer_id = request .session() .attribute( "userId"); // doesn't need two arguments because we are just retrieving! String reviewer = Review.getReviewer(reviewer_id); model.put("reviewer", reviewer); // get reviews by restaurant id List<Review> listreviews = Review.listReviews(restaurantId); model.put("listreviews", listreviews); model.put("restaurant", restaurant); model.put("template", "templates/restaurant.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // remove restaurant get( "/restaurants/:id/removerestaurant", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); int restaurantId = Integer.parseInt(request.params(":id")); Restaurant.removeRestaurant(restaurantId); List<Restaurant> restaurants = Restaurant.all(); model.put("restaurants", restaurants); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // search restaurant by type post( "/searchresults", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String searchtype = request.queryParams("type"); List<Restaurant> searchresults = Restaurant.findType(searchtype); List<String> types = Restaurant.listTypes(); model.put("types", types); model.put("searchresults", searchresults); model.put("template", "templates/searchresults.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // //add review to restaurant post( "/restaurants/:id", (request, response) -> { // This section does not work! :( HashMap<String, Object> model = new HashMap<String, Object>(); int restaurantId = Integer.parseInt(request.params(":id")); Restaurant restaurant = Restaurant.find(restaurantId); // get the username from the session model.put("username", request.session().attribute("username")); Integer reviewer_id = request .session() .attribute( "userId"); // doesn't need two arguments because we are just retrieving! // get reviewer by user id String reviewer = Review.getReviewer(reviewer_id); model.put("reviewer", reviewer); // post new review String review_description = request.queryParams("review_description"); Integer ranking = Integer.parseInt(request.queryParams("ranking")); String review_date = request.queryParams("review_date"); Review newReview = new Review(review_description, ranking, reviewer_id, review_date, restaurantId); newReview.save(); // get reviews by restaurant id List<Review> listreviews = Review.listReviews(restaurantId); model.put("listreviews", listreviews); // add restaurant to model model.put("restaurant", restaurant); model.put("template", "templates/restaurant.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) { ProcessBuilder process = new ProcessBuilder(); Integer port; if (process.environment().get("PORT") != null) { port = Integer.parseInt(process.environment().get("PORT")); } else { port = 4567; } staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // get(/) type of route get this url.. get( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", Task.all()); model.put("template", "templates/tasks.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // After they submit the form, this is where they will be taken /tasks.vtl // get("tasks/new", (request, response) -> { // HashMap<String, Object> model = new HashMap<String, Object>(); // model.put("template", "templates/task-form.vtl"); // return new ModelAndView(model, layout); // }, new VelocityTemplateEngine()); // //task-form is where client inputs data and hits submit post( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Category category = Category.find(Integer.parseInt(request.queryParams("categoryId"))); String description = request.queryParams("description"); Task newTask = new Task(description); category.addTask(newTask); model.put("category", category); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // grabs information and makes a new description of the information in the array // takes you to a new page get( "/tasks/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Task task = Task.find(Integer.parseInt(request.params(":id"))); model.put("task", task); model.put("template", "templates/task.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("categories", Category.all()); model.put("template", "templates/categories.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/category-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String name = request.queryParams("name"); Category newCategory = new Category(name); model.put("category", newCategory); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id/tasks/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category-tasks-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); } // end of main
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { response.redirect("/stores"); return null; }); get( "/stores", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("stores", Store.all()); model.put("template", "templates/stores.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stores/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("store", Store.find(Integer.parseInt(request.params("id")))); model.put("brands", Brand.all()); model.put("template", "templates/store.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/stores/addstore", (request, response) -> { Store store = new Store(request.queryParams("store-name")); store.save(); response.redirect("/stores/" + store.getId()); return null; }); post( "/stores/:id/addbrand", (request, response) -> { Store store = Store.find(Integer.parseInt(request.queryParams("addbrand-store-id"))); store.add(Integer.parseInt(request.queryParams("addbrand-brand-id"))); response.redirect("/stores/" + store.getId()); return null; }); post( "/stores/:id/unlinkbrand", (request, response) -> { Store store = Store.find(Integer.parseInt(request.params("id"))); store.unlink(Integer.parseInt(request.queryParams("unlink-brand"))); response.redirect("/stores/" + store.getId()); return null; }); post( "/stores/:id/update", (request, response) -> { Store store = Store.find(Integer.parseInt(request.params("id"))); store.update(request.queryParams("update-store-name")); response.redirect("/stores/" + store.getId()); return null; }); post( "/stores/deletestore", (request, response) -> { Store store = Store.find(Integer.parseInt(request.queryParams("store-id"))); store.delete(); response.redirect("/stores"); return null; }); get( "/brands", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/brands.vtl"); model.put("brands", Brand.all()); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/brands/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("brand", Brand.find(Integer.parseInt(request.params("id")))); model.put("stores", Store.all()); model.put("template", "templates/brand.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/brands/addbrand", (request, response) -> { Brand brand = new Brand(request.queryParams("brand-name")); brand.save(); response.redirect("/brands/" + brand.getId()); return null; }); post( "/brands/:id/addstore", (request, response) -> { Brand brand = Brand.find(Integer.parseInt(request.queryParams("addstore-brand-id"))); brand.add(Integer.parseInt(request.queryParams("addstore-store-id"))); response.redirect("/brands/" + brand.getId()); return null; }); post( "/brands/:id/unlinkstore", (request, response) -> { Brand brand = Brand.find(Integer.parseInt(request.params("id"))); brand.unlink(Integer.parseInt(request.queryParams("unlink-store"))); response.redirect("/brands/" + brand.getId()); return null; }); post( "/brands/:id/update", (request, response) -> { Brand brand = Brand.find(Integer.parseInt(request.params("id"))); brand.update(request.queryParams("update-brand-name")); response.redirect("/brands/" + brand.getId()); return null; }); post( "/brands/deletebrand", (request, response) -> { Brand brand = Brand.find(Integer.parseInt(request.queryParams("brand-id"))); brand.delete(); response.redirect("/brands"); return null; }); }
public static void main(String[] args) { // serve static files from /www in resources staticFileLocation("/www"); // setup the database model Sql2o sql2o = new Sql2o("jdbc:h2:~/test", "sa", ""); Sql2oModel model = new Sql2oModel(sql2o); // url /hello // simple response return get("/hello", (req, res) -> "Hello Spark!"); // url /test-view // simple template "test-view.ftl" get( "/test-view", (req, res) -> { Map<String, Object> viewModel = new HashMap<>(); viewModel.put("message", "Hello Spark!"); return new ModelAndView(viewModel, "test-view.ftl"); }, new FreeMarkerEngine()); // url /books // data bound template get( "/books", (req, res) -> { Map<String, Object> viewModel = new HashMap<>(); viewModel.put("message", "Books"); List<Book> books = model.getBooks(); viewModel.put("books", books); return new ModelAndView(viewModel, "books.ftl"); }, new FreeMarkerEngine()); // url /books/new // initial get of input form for new book in database get( "/books/new", (req, res) -> { Map<String, Object> viewModel = new HashMap<>(); viewModel.put("message", "Add Book"); return new ModelAndView(viewModel, "new-book.ftl"); }, new FreeMarkerEngine()); // url /books/new // subsequent post of new book data back to the // server to get saved back to the database post( "/books/new", (req, res) -> { // todo: add response book to database, use custom viewModel class MultiMap<String> params = new MultiMap<String>(); UrlEncoded.decodeTo(req.body(), params, "UTF-8"); NewBookEditModel editModel = new NewBookEditModel(); BeanUtils.populate(editModel, params); long id = model.createBook( editModel.getTitle(), editModel.getAuthor(), editModel.getImageUrl()); res.redirect("/books"); halt(); return id; // ?? json & rest?? }); get( "/books/delete/:id", (req, res) -> { int id = Integer.parseInt(req.params().get(":id")); model.deleteBook(id); res.redirect("/books"); halt(); return null; }); // url /spi-test // json testbed page "spi-test.ftl" get( "/spi-test", (req, res) -> { Map<String, Object> viewModel = new HashMap<>(); viewModel.put("message", "SPI Test Page"); List<Book> books = model.getBooks(); viewModel.put("books", books); return new ModelAndView(viewModel, "spi-test.ftl"); }, new FreeMarkerEngine()); // Additional JSON Services? }
public static void main(String[] args) { staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { Map<String, Object> model = new HashMap<String, Object>(); model.put("clients", request.session().attribute("clients")); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylists/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/stylist-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylists", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("stylists", Stylist.all()); model.put("template", "templates/stylists.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/stylists", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String styler = request.queryParams("styler"); Stylist newStylist = new Stylist(styler); newStylist.save(); // *** ADDED FOR DB VERSION *** model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylists/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id"))); model.put("stylist", stylist); model.put("template", "templates/stylist.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylists/:id/clients/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id"))); model.put("stylist", stylist); model.put("template", "templates/client-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/clients", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("client", Client.all()); model.put("template", "templates/client.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/clients", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Stylist stylist = Stylist.find(Integer.parseInt(request.queryParams("stylist_id"))); String name = request.queryParams("name"); // ** THIS SECTION UPDATED FOR DB VERSION *** Client newClient = new Client(name, stylist.getId()); newClient.save(); model.put("stylist", stylist); model.put("template", "templates/client-success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/stylists/:id/delete", (request, response) -> { Map model = new HashMap(); Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id"))); Stylist.delete(stylist); model.put("template", "templates/delete-success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); }
public static void main(String[] args) throws SQLException { Connection db = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/quiz?useSSL=false", "root", "root"); Entity.setDb(db); ClasspathLoader loader = new ClasspathLoader(); loader.setPrefix("views"); loader.setSuffix(".html.twig"); Cache<Object, PebbleTemplate> cache = CacheBuilder.newBuilder().maximumSize(0).build(); PebbleEngine pebbleEngine = new PebbleEngine.Builder() .defaultLocale(Locale.GERMAN) .loader(loader) .templateCache(cache) .build(); PebbleTemplateEngine engine = new PebbleTemplateEngine(pebbleEngine); Spark.staticFileLocation("public"); Spark.get( "/stop", (request, response) -> { Spark.stop(); return null; }); Spark.get( "/", (request, response) -> { response.redirect("/spiel/start"); return null; }); Spark.get( "/login", (request, response) -> { return new ModelAndView(null, "login"); }, engine); Spark.post( "/login", (request, response) -> { Spieler spieler = Spieler.findeNachEmail(request.queryParams("email")); Session session = request.session(true); if (spieler != null && spieler.istPasswortKorrekt(request.queryParams("passwort"))) { session.attribute("spieler", spieler); response.redirect("/spiel/start"); } else { session.removeAttribute("spieler"); response.redirect("/login"); } return null; }); Spark.get( "/logout", (request, response) -> { request.session(true).removeAttribute("spieler"); response.redirect("/login"); return null; }); Spark.before( "/admin/*", (request, response) -> { Session session = request.session(true); if (session.attribute("spieler") == null) { response.redirect("/login"); Spark.halt(401); } }); Spark.before( "/spiel/*", (request, response) -> { Session session = request.session(true); if (session.attribute("spieler") == null) { response.redirect("/login"); Spark.halt(401); } }); Spark.get( "/spiel/start", (request, response) -> { Spieler spieler = request.session(true).attribute("spieler"); ArrayList<Zuordnung> zuordnungen = Zuordnung.findeUnbeantworteteNachSpieler(spieler); if (zuordnungen.size() > 0) { response.redirect("/spiel/runde"); return null; } Map<String, Object> werte = new HashMap<>(); werte.put("fragen", Frage.findeAlle()); return new ModelAndView(werte, "spiel_start"); }, engine); Spark.post( "/spiel/start", (request, response) -> { Spieler spieler = request.session(true).attribute("spieler"); ArrayList<Frage> fragen = Frage.findeAlle(); Collections.shuffle(fragen); int anzahl = Integer.parseInt(request.queryParams("anzahl").trim()); Runde runde = new Runde(spieler); for (int i = 0; i < anzahl; i++) { new Zuordnung(fragen.get(i), runde).speichern(); } response.redirect("/spiel/runde"); return null; }); Spark.get( "/spiel/runde", (request, response) -> { Spieler spieler = request.session(true).attribute("spieler"); ArrayList<Zuordnung> zuordnungen = Zuordnung.findeUnbeantworteteNachSpieler(spieler); if (zuordnungen.isEmpty()) { response.redirect("/spiel/zusammenfassung"); Spark.halt(); return null; } else { Map<String, Object> werte = new HashMap<>(); Zuordnung naechsteZuordung = zuordnungen.get(0); naechsteZuordung.setGestellt(); werte.put("zuordnung", naechsteZuordung); return new ModelAndView(werte, "spiel_runde"); } }, engine); Spark.post( "/spiel/runde", (request, response) -> { Zuordnung zuordnung = Zuordnung.findeNachId(request.queryParams("zuordnung_id")); Antwort antwort = Antwort.findeNachId(request.queryParams("antwort_id")); zuordnung.setBeantwortet(); zuordnung.setRichtig(antwort.isRichtig()); zuordnung.speichern(); response.redirect("/spiel/runde"); return null; }); Spark.get( "/spiel/zusammenfassung", (request, response) -> { Map<String, Object> werte = new HashMap<>(); Spieler spieler = request.session(true).attribute("spieler"); ArrayList<Zuordnung> zuordnungen = Zuordnung.findeNachSpieler(spieler); int richtigeAntworten = 0; int beantworteteFragen = 0; for (Zuordnung zuordnung : zuordnungen) { if (zuordnung.getBeantwortet() != null) { beantworteteFragen++; if (zuordnung.isRichtig()) { richtigeAntworten++; } } } werte.put("gestellteFragen", zuordnungen.size()); werte.put("richtigeAntworten", richtigeAntworten); werte.put("beantworteteFragen", beantworteteFragen); return new ModelAndView(werte, "spiel_zusammenfassung"); }, engine); Spark.get( "/admin/frage", (request, response) -> { Map<String, Object> werte = new HashMap<>(); werte.put("fragen", Frage.findeAlle()); return new ModelAndView(werte, "admin_frage"); }, engine); Spark.post( "/admin/frage/neu", (request, response) -> { String aufgabe = request.queryParams("aufgabe").trim(); if (!aufgabe.isEmpty()) { new Frage(aufgabe).speichern(); } response.redirect("/admin/frage"); return null; }); Spark.get( "/admin/frage/loeschen/:frage_id", (request, response) -> { Frage.findeNachId(request.params(":frage_id")).loeschen(); response.redirect("/admin/frage"); return null; }); Spark.get( "/admin/frage/aendern/:frage_id", (request, response) -> { Map<String, Object> werte = new HashMap<>(); werte.put("frage", Frage.findeNachId(request.params(":frage_id"))); return new ModelAndView(werte, "admin_frage_aendern"); }, engine); Spark.post( "/admin/frage/aendern", (request, response) -> { Frage zuAenderndeFrage = Frage.findeNachId(request.queryParams("frage_id")); zuAenderndeFrage.setAufgabe(request.queryParams("aufgabe").trim()); zuAenderndeFrage.speichern(); response.redirect("/admin/frage"); return null; }); Spark.get( "/admin/frage/:frage_id/antwort", (request, response) -> { Map<String, Object> werte = new HashMap<>(); Frage frage = Frage.findeNachId(request.params(":frage_id")); werte.put("frage", frage); werte.put("antworten", frage.getAntworten()); return new ModelAndView(werte, "admin_frage_antwort"); }, engine); Spark.post( "/admin/antwort/neu", (request, response) -> { Frage frage = Frage.findeNachId(request.queryParams("frage_id")); String loesung = request.queryParams("loesung"); String richtig = request.queryParams("richtig"); Antwort antwort; if (richtig != null) { for (Antwort andereAntwort : Antwort.findeNachFrage(frage)) { if (andereAntwort.isRichtig()) { andereAntwort.setRichtig(false); andereAntwort.speichern(); } } antwort = new Antwort(loesung, true, frage); } else { antwort = new Antwort(loesung, false, frage); } antwort.speichern(); response.redirect("/admin/frage/" + frage.getId() + "/antwort"); return null; }); Spark.post( "/admin/frage/:frage_id/antwort/richtig/aendern", (Request request, Response response) -> { int richtigeAntwortId = Integer.parseInt(request.queryParams("richtig").trim()); Frage frage = Frage.findeNachId(request.params(":frage_id")); for (Antwort antwort : Antwort.findeNachFrage(frage)) { if (antwort.isRichtig() && antwort.getId() != richtigeAntwortId) { antwort.setRichtig(false); antwort.speichern(); } else if (!antwort.isRichtig() && antwort.getId() == richtigeAntwortId) { antwort.setRichtig(true); antwort.speichern(); } } response.redirect("/admin/frage/" + frage.getId() + "/antwort"); return null; }); Spark.get( "/admin/antwort/loeschen/:antwort_id", (request, response) -> { Antwort antwort = Antwort.findeNachId(request.params(":antwort_id")); antwort.loeschen(); response.redirect("/admin/frage/" + antwort.getFrage().getId() + "/antwort"); return null; }); Spark.get( "/admin/antwort/aendern/:antwort_id", (request, response) -> { Map<String, Object> werte = new HashMap<>(); werte.put("antwort", Antwort.findeNachId(request.params(":antwort_id"))); return new ModelAndView(werte, "admin_frage_antwort_aendern"); }, engine); Spark.post( "/admin/antwort/aendern", (Request request, Response response) -> { Antwort antwort = Antwort.findeNachId(request.queryParams("antwort_id")); antwort.setLoesung(request.queryParams("loesung")); antwort.speichern(); response.redirect("/admin/frage/" + antwort.getFrage().getId() + "/antwort"); return null; }); }