Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WhoDidWhat endpoints and test and frontend #33

Merged
merged 7 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
36 changes: 36 additions & 0 deletions src/main/java/commandpattern/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandpattern;

import facades.WhoDidWhatFacade;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

/**
*
* @author Malte
*/
public abstract class Command {
private static HashMap<String, Command> commands;

private static void initCommands()
{
commands = new HashMap<>();
commands.put("makeWork", new MakeWorkCommand()); // Insert work into the database.
}

public static Command from(HttpServletRequest request)
{
String commandName = request.getParameter("command");
if (commands == null)
{
initCommands();
}
return commands.get(commandName);
}

MalteMagnussen marked this conversation as resolved.
Show resolved Hide resolved
public abstract String execute(HttpServletRequest request, WhoDidWhatFacade logic);
}
83 changes: 83 additions & 0 deletions src/main/java/commandpattern/FrontController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandpattern;

import facades.WhoDidWhatFacade;
import java.io.IOException;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.EMF_Creator;
import static utils.EMF_Creator.createEntityManagerFactory;

/**
*
* @author Malte
*/
public class FrontController extends HttpServlet {
private EntityManagerFactory emf = createEntityManagerFactory(EMF_Creator.DbSelector.DEV, EMF_Creator.Strategy.CREATE);
private WhoDidWhatFacade whoDidWhatFacade = WhoDidWhatFacade.getWhoDidWhatFacade(emf);

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Command action = Command.from(request);
String target = action.execute(request, whoDidWhatFacade);
request.getRequestDispatcher(target+".html").forward(request, response);

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
31 changes: 31 additions & 0 deletions src/main/java/commandpattern/MakeWorkCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandpattern;

import facades.WhoDidWhatFacade;
import javax.servlet.http.HttpServletRequest;

/**
*
* @author Malte
*/
public class MakeWorkCommand extends Command {

@Override
public String execute(HttpServletRequest request, WhoDidWhatFacade facade) {

// Input from HTML form
String name = request.getParameter("name");
System.out.println("NAME: " + name);
String work = request.getParameter("work");
System.out.println("DATE: " + work);

facade.makeWork(name, work);

return "groupContract";
}

}
2 changes: 1 addition & 1 deletion src/main/java/rest/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public Set<Class<?>> getClasses() {
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(org.glassfish.jersey.server.wadl.internal.WadlResource.class);
resources.add(rest.JokeResource.class);
resources.add(rest.RenameMeResource.class);
resources.add(rest.WhoDidWhatResource.class);
}

}
43 changes: 43 additions & 0 deletions src/main/java/rest/WhoDidWhatResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import utils.EMF_Creator;
import facades.WhoDidWhatFacade;
import javax.persistence.EntityManagerFactory;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("work")
public class WhoDidWhatResource {

private static final EntityManagerFactory EMF = EMF_Creator.createEntityManagerFactory(EMF_Creator.DbSelector.DEV, EMF_Creator.Strategy.DROP_AND_CREATE);
private static final WhoDidWhatFacade FACADE = WhoDidWhatFacade.getWhoDidWhatFacade(EMF);
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

@GET
@Path("/all")
@Produces({MediaType.APPLICATION_JSON})
public String getAllWork() throws Exception {
try {
return GSON.toJson(FACADE.getAllWorkDone());
} catch (Exception ex) {
return "{\"error\": \"" + ex.getMessage() + "\"}";
}
}

@GET
@Path("/{name}")
@Produces({MediaType.APPLICATION_JSON})
public String getWorkByName(@PathParam("name") String name) throws Exception {
try {
return GSON.toJson(FACADE.getWorkDoneByName(name));
} catch (Exception ex) {
return "{\"error\": \"" + ex.getMessage() + "\"}";
}
}

}
16 changes: 16 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>FrontController</servlet-name>
<servlet-class>commandpattern.FrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>/FrontController</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
10 changes: 1 addition & 9 deletions src/main/webapp/css/globalCSS.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ pre code {

/* GROUP CONTRACT CSS START */

.container {
#OutPutContainer {
display: flex;
}

#GroupContractContent {
order: 2;
}

#WhoDidWhat {
order: 1;
}

.c1 {
margin-left: 36pt;
padding-top: 0pt;
Expand Down
46 changes: 38 additions & 8 deletions src/main/webapp/groupContract.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,44 @@
<!-- Page Content -->

<!-- OutPut container -->
<div class="container">

<div id="WhoDidWhat">

<div class="container" id="OutPutContainer">

<div class='container' id="WhoDidWhat">
<div id='WorkInputDiv'>

<h2> Input Work </h2>

<form method="POST" action="FrontController">
<input type="hidden" name="command" value="makeWork">
<div class="form-group">
<label for="inputName">Your name</label>
<input type="text" name="name" class="form-control" id="inputName" placeholder="Enter your name" required>
</div>
<div class="form-group">
<label for="inputWork">What did you do?</label>
<input type="text" name="work" class="form-control" id="inputWork" placeholder="Enter the work you've done." required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

</div>
<br>
<div id='WorkOutputDiv'>

<h2> View Work </h2>
<label for='nameToView'>Input the name of the person whose work you wish to view.</label>
<input type='text' class='form-control' id='nameToView' placeholder="Name">
<br>
<button class='btn btn-primary' name='ViewAll' id='viewAll'>View All</button>
<button class='btn btn-primary' name='ViewByName' id='viewByName'>View By Name</button>

</div>
<br>
<p id="OutPutReceiver"></p>

</div>


<div id="GroupContractContent">
<div class="topSpace"></div>
<div class='container' id="GroupContractContent">
<h2>
Gruppekontrakt
</h2>
Expand Down Expand Up @@ -104,7 +133,7 @@ <h2>
</ul>
<ul class="c0">
<li class="c3 c5">
<span class="c2">Discord/Github webhook bruges til at annoncere pull requests&amp;pushes</span>
<span class="c2">Discord/Github webhook bruges til at annoncere pull requests &amp; pushes</span>
</li>
<li class="c3 c5">
<span class="c2">mvn clean test &nbsp;f&oslash;r push</span>
Expand Down Expand Up @@ -202,6 +231,7 @@ <h2>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>

<!-- My Custom JavaScript - Input below this line -->
<script src="javascript/tableUtil.js"></script>
<script src="javascript/groupContract.js"></script>


Expand Down
80 changes: 79 additions & 1 deletion src/main/webapp/javascript/groupContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,83 @@
// Only starts once the window has loaded,
// so we know everything is ready for JavaScript.
window.onload = function () {


/* VIEW BY NAME START */

function showData(data) {

let returnString = tableHeader([data.name]);

let done = Object.values(data.done);
for (var i = 0; i < done.length; i++) {
let doneItem = [done[i]];
returnString += tableRow(doneItem);
}

return returnString += endOfTable();

}

// Get viewByName button
let viewByName = document.getElementById("viewByName");

// Listener for click. If clicked, populate OutPutReceiver with Data.
viewByName.addEventListener("click", function () {

// Get Name in input field .
let name = document.getElementById("nameToView").value;

// URL OF GET NAME REST ENDPOINT
let URL = "/CA-1/api/work/" + name;

// GET DATA
fetch(URL)
.then(res => res.json())
.then(data => {

// Get OutPutReceiver
let OutPutReceiver = document.getElementById("OutPutReceiver");

// Inside this callback, and only here, the response data is available
OutPutReceiver.innerHTML = showData(data);
});
});

/* VIEW BY NAME END */

/* VIEW ALL START */

function showAllData(data) {
let students = Object.values(data);

let returnString = "";

students.forEach(element => returnString += showData(element));

return returnString;
}

// Get viewAll button
let viewAll = document.getElementById("viewAll");

// Listener for click. If clicked, populate OutPutReceiver with Data.
viewAll.addEventListener("click", function () {

// URL OF GET ALL REST ENDPOINT
let URL = "/CA-1/api/work/all";

// GET DATA
fetch(URL)
.then(res => res.json())
.then(data => {

// Get OutPutReceiver
let OutPutReceiver = document.getElementById("OutPutReceiver");

// Inside this callback, and only here, the response data is available
OutPutReceiver.innerHTML = showAllData(data);
});
});

/* VIEW ALL END */
};
Loading