Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 0 additions & 8 deletions portfolio/README.md

This file was deleted.

14 changes: 13 additions & 1 deletion portfolio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.59</version>
</dependency>

</dependencies>

<build>
Expand All @@ -35,7 +47,7 @@
<version>2.2.0</version>
<configuration>
<!-- TODO: set project ID. -->
<deploy.projectId>YOUR_PROJECT_ID_HERE</deploy.projectId>
<deploy.projectId>qnn-step-2020</deploy.projectId>
<deploy.version>1</deploy.version>
</configuration>
</plugin>
Expand Down
26 changes: 26 additions & 0 deletions portfolio/src/main/java/com/google/sps/data/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.sps.data;
import java.util.Date;

public final class Comment{
private final Object user;
private final Object content;
private final Object timestamp;
public Comment(Object _user, Object _content, Object _timestamp){
this.user = _user;
this.content = _content;
this.timestamp = _timestamp;
}
}
114 changes: 111 additions & 3 deletions portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
// limitations under the License.

package com.google.sps.servlets;
import com.google.sps.data.Comment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

import java.lang.Integer;

import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.QueryResultList;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
Expand All @@ -23,10 +42,99 @@
/** Servlet that returns some example content. TODO: modify this file to handle comments data */
@WebServlet("/data")
public class DataServlet extends HttpServlet {

HashMap<Integer, String> cursors = new HashMap<Integer, String>();
int pageCount = 0;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;");
response.getWriter().println("<h1>Hello world!</h1>");
ArrayList<String> messages = new ArrayList<>();

String nextPage = request.getParameter("next");
String cursor = request.getParameter("cursor");
pageCount += 1;
if(cursor != null && nextPage.equals("false")){
pageCount -= 2;
cursor = cursors.get(pageCount);
}

FetchOptions fetchOptions = FetchOptions.Builder.withLimit(5);
if(cursor != null){

fetchOptions.startCursor(Cursor.fromWebSafeString(cursor));
} else {
cursors.put(pageCount, "");
}



DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

Query query = new Query("Comment").addSort("timestamp", SortDirection.DESCENDING);
if(nextPage == "false"){
query = new Query("Comment").addSort("timestamp", SortDirection.ASCENDING);
}
PreparedQuery resultsPQ = datastore.prepare(query);

QueryResultList<Entity> results;
try {
results = resultsPQ.asQueryResultList(fetchOptions);
} catch (IllegalArgumentException e) {
response.sendRedirect("/");
return;
}
for (Entity entity : results) {

Object content = entity.getProperty("content");
Object timestamp = entity.getProperty("timestamp");
Comment c = new Comment("None", content, timestamp);
messages.add(commentToJson(c));
}
String cursorString = results.getCursor().toWebSafeString();
cursors.put(pageCount, cursorString);
messages.add(cursorString);
Gson gson = new Gson();
String json = gson.toJson(messages);
response.setContentType("application/json;");
response.getWriter().println(json);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Get the input from the form.
String user = "None";
String text = getParameter(request, "comment-input", "");

Entity comment = commentToEntity("None", text);

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(comment);
response.sendRedirect("/index.html");

}


private String commentToJson(Comment comment) {
Gson gson = new Gson();

String json = gson.toJson(comment);
return json;
}

private String getParameter(HttpServletRequest request, String name, String defaultValue) {
String value = request.getParameter(name);
if (value == null) {
return defaultValue;
}
return value;
}

private Entity commentToEntity(String user, String content){
Entity commentEntity = new Entity("Comment");
long current_time = System.currentTimeMillis();
commentEntity.setProperty("user", user);
commentEntity.setProperty("timestamp", current_time);
commentEntity.setProperty("content", content);
return commentEntity;
}

}
65 changes: 65 additions & 0 deletions portfolio/src/main/java/com/google/sps/servlets/LoginServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;
import com.google.sps.data.Comment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

import java.lang.Integer;

import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.QueryResultList;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that returns some example content. TODO: modify this file to handle comments data */
@WebServlet("/log")
public class LoginServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");

UserService userService = UserServiceFactory.getUserService();
if (!userService.isUserLoggedIn()) {
String urlToRedirectToAfterUserLogsOut = "/";
String loginUrl = userService.createLoginURL(urlToRedirectToAfterUserLogsOut);
response.getWriter().println("{\"loginUrl\": \"" + loginUrl + "\",");
response.getWriter().println("\"logoutUrl\": \"\"}");
} else {
String urlToRedirectToAfterUserLogsOut = "/";
String logoutUrl = userService.createLogoutURL(urlToRedirectToAfterUserLogsOut);
response.getWriter().println("{\"logoutUrl\": \"" + logoutUrl + "\",");
response.getWriter().println("\"loginUrl\": \"\"}");
}
}
}
17 changes: 16 additions & 1 deletion portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<body onload = "fetchCommentContent('', '')">
<div id="fact-box-1"></div>
<div id="content">
<h1>Quinn Collins!</h1>
Expand All @@ -17,6 +17,21 @@ <h1>Quinn Collins!</h1>
<img src = "images/me.jpg" id = "pics" align="left"/>
<p> I'm very interested in audio! I've implemented dozens of audio manipulation techniques in python including denoisers, sound seperators, classifiers, etc. I'm currently working on a scuffed real time sound seperator that'll help with my jazz transcriptions.</p>
<a href="https://github.com/QuinnDACollins"><img class = "widget" src = "images/github.png"/></a> <a href = "https://www.linkedin.com/in/quinn-collins-28a46416a/"><img class = "widget" src = "images/linkedin.png"/></a>

</div>
<input type="hidden" id="cursor" name="cursor" value="">
<form id = "comment-form" method = "POST" action = "/data">
<textarea name = "comment-input" form = "comment-form"></textarea>
<input type="submit">
<button value = "" onclick = "fetchCommentContent(document.getElementById('cursor').value, 'true')">Next Page</button>
<button value = "" onclick = "fetchCommentContent(document.getElementById('cursor').value, 'false')">Prev Page</button>

</form>
<a href = "" id = "login-link">Login</a>

<a href = "" id = "logout-link">Logout</a>

<div id = "comment-area">
</div>
<div id="fact-box-2"></div>

Expand Down
52 changes: 45 additions & 7 deletions portfolio/src/main/webapp/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
/**
* Adds a fact greeting to the page.
*/

window.addEventListener("load", myInit, true); function myInit(){
checkLogin()
}
const facts =
['I\'ve read all of One Piece', 'I like Winnie the Pooh!', 'Favourite pianist is either Ahmad Jamal or Ryo Fukui', 'Took 2 years of Chinese!', 'My cat\'s name is Kai!', 'I interned at Google!', 'Why are these sideways!?', 'I have 2 brothers!', 'I like to bake pastries!'];

Expand All @@ -30,17 +34,51 @@ function addFact() {
const fact = facts[factIndex];
facts.splice(factIndex, 1);
// Add it to the page, randomly selecting one of 2 divs to add it to
const rand_padding = Math.floor(Math.random() * 100)
const fact_element = document.createElement('div');
fact_element.setAttribute("class", "fact-text");
fact_element.setAttribute("style", "margin-left: " + rand_padding.toString() + "%;");
fact_element.innerText = "\n" + fact;
const randPadding = Math.floor(Math.random() * 100)
const factElement = document.createElement('div');
factElement.setAttribute("class", "fact-text");
factElement.setAttribute("style", "margin-left: " + randPadding.toString() + "%;");
factElement.innerText = "\n" + fact;

if (Math.floor(Math.random() * 2) == 0) {
document.getElementById("fact-box-1").append(fact_element);
document.getElementById("fact-box-1").append(factElement);
} else {
document.getElementById("fact-box-2").append(fact_element);
document.getElementById("fact-box-2").append(factElement);
}

}

async function fetchCommentContent(cursor, next) {
fetch('/data?cursor=' + cursor + "&next=" + next) // sends a request to /my-data-url
.then(response => response.json()) // parses the response as JSON
.then((commentArray) => { // now we can reference the fields in myObject!
var i = 0;
document.getElementById("comment-area").innerHTML = ""
for(i = 0; i < commentArray.length - 1; i++){
var c = JSON.parse(commentArray[i]);
document.getElementById("comment-area").innerHTML += "<p>" + c.content + "<p>";
}

document.getElementById("cursor").value = commentArray[commentArray.length -1];
});
}

async function checkLogin() {
fetch('/log') // sends a request to /my-data-url
.then(response => response.json()) // parses the response as JSON
.then((logged) => { // now we can reference the fields in myObject!
if(logged.loginUrl != ""){
console.log(logged);
document.getElementById("comment-form").setAttribute("hidden", "true");
document.getElementById("logout-link").setAttribute("hidden", "true");
document.getElementById("login-link").removeAttribute("hidden");
document.getElementById("login-link").setAttribute("href", logged.loginUrl);
} else {
console.log(logged);
document.getElementById("comment-form").removeAttribute("hidden");
document.getElementById("login-link").setAttribute("hidden", "true");
document.getElementById("logout-link").removeAttribute("hidden");
document.getElementById("logout-link").setAttribute("href", logged.logoutUrl);
}
});
}