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
21 changes: 19 additions & 2 deletions portfolio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>portfolio</artifactId>
<version>1</version>
<packaging>war</packaging>

<properties>
<!-- This project uses Java 8 -->
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -23,8 +23,25 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.59</version>
</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>
<plugins>
<!-- Provides `mvn package appengine:run` for local testing
Expand Down
65 changes: 60 additions & 5 deletions portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,74 @@

package com.google.sps.servlets;

import com.google.gson.Gson;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
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;

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

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

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Query query = new Query("comment").addSort("timestamp", SortDirection.DESCENDING);

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
PreparedQuery results = datastore.prepare(query);

List<String> tasks = new ArrayList<>();
for (Entity entity : results.asIterable()) {
long id = entity.getKey().getId();
String comm = (String) entity.getProperty("content");

tasks.add(comm);


}

response.setContentType("application/json;");
String json = new Gson().toJson(tasks);
System.out.println("get: " + tasks);
response.getWriter().println(json);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String text = getParameter(request, "textarea_field", "");//returns what is in text firld

commStream.add(text);
System.out.println("post: " + commStream);
long timestamp = System.currentTimeMillis();

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity taskEntity = new Entity("comment");
taskEntity.setProperty("content", text);
taskEntity.setProperty("timestamp", timestamp); //adds a timestamp to the comment

datastore.put(taskEntity);

response.sendRedirect("/index.html");
}

private String getParameter(HttpServletRequest request, String name, String defaultValue) {
String value = request.getParameter(name);
if (value == null) {
return defaultValue;
}
return value;
}
}
}
73 changes: 73 additions & 0 deletions portfolio/src/main/java/com/google/sps/servlets/HomeServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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.appengine.api.datastore.*;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
UserService userService = UserServiceFactory.getUserService();

// If user is not logged in, show a login form (could also redirect to a login page)
if (!userService.isUserLoggedIn()) {
String loginUrl = userService.createLoginURL("/home");
out.println("<p>Login <a href=\"" + loginUrl + "\">here</a>.</p>");
return;
}

// If user has not set a nickname, redirect to nickname page
String nickname = getUserNickname(userService.getCurrentUser().getUserId());
if (nickname == null) {
response.sendRedirect("/nickname");
//in implementation, redirect to index
return;
}

// User is logged in and has a nickname, so the request can proceed
String logoutUrl = userService.createLogoutURL("/home");
out.println("<h1>Home</h1>");
out.println("<p>Hello " + nickname + "!</p>");
out.println("<p>Logout <a href=\"" + logoutUrl + "\">here</a>.</p>");
out.println("<p>Change your nickname <a href=\"/nickname\">here</a>.</p>");
}

/** Returns the nickname of the user with id, or null if the user has not set a nickname. */
private String getUserNickname(String id) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query =
new Query("UserInfo")
.setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));
PreparedQuery results = datastore.prepare(query);
Entity entity = results.asSingleEntity();
if (entity == null) {
return null;
}
String nickname = (String) entity.getProperty("nickname");
return nickname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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.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.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/nickname")
public class NicknameServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Set Nickname</h1>");

UserService userService = UserServiceFactory.getUserService();
if (userService.isUserLoggedIn()) {
String nickname = getUserNickname(userService.getCurrentUser().getUserId());
out.println("<p>Set your nickname here:</p>");
out.println("<form method=\"POST\" action=\"/nickname\">");
out.println("<input name=\"nickname\" value=\"" + nickname + "\" />");
out.println("<br/>");
out.println("<button>Submit</button>");
out.println("</form>");
} else {
String loginUrl = userService.createLoginURL("/nickname");
out.println("<p>Login <a href=\"" + loginUrl + "\">here</a>.</p>");
}
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserService userService = UserServiceFactory.getUserService();
if (!userService.isUserLoggedIn()) {
response.sendRedirect("/nickname");
return;
}

String nickname = request.getParameter("nickname");
String id = userService.getCurrentUser().getUserId();

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity = new Entity("UserInfo", id);
entity.setProperty("id", id);
entity.setProperty("nickname", nickname);
// The put() function automatically inserts new data or updates existing data based on ID
datastore.put(entity);

response.sendRedirect("/home");
}

/**
* Returns the nickname of the user with id, or empty String if the user has not set a nickname.
*/
private String getUserNickname(String id) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query =
new Query("UserInfo")
.setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));
PreparedQuery results = datastore.prepare(query);
Entity entity = results.asSingleEntity();
if (entity == null) {
return "";
}
String nickname = (String) entity.getProperty("nickname");
return nickname;
}
}
50 changes: 23 additions & 27 deletions portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,42 @@
<script src="script.js"></script>
</head>

<body>
<body onload="servlet()">
<div id="content">

<h1 style="text-align:center;">Noelle's Portfolio</h1>
<p style="text-align:center;"> Hi! I'm Noelle and this is my portfolio!</p>
<h1>Noelle's Portfolio</h1>
<p> Hi! I'm Noelle and this is my portfolio!</p>
<img src="images/f94.gif" id="cenImage">
<p style="text-align:center;"> (a real picture of me)</p>
<p style="text-align:center;">Click here to get a random D&D quote:</p>
<p> (a real picture of me)</p>
<p>Click here to get a random D&D quote:</p>
<div id="wrapper">
<button onclick="randMessage()" type="button" class="nes-btn is-primary" > *roll dice*</button>
</div>
<!--
ideally, i'd like to move this script section, but the index can't find the script file if i don't keep it here.
a problem for future noelle!
-->
<script>
function randMessage() {
const msgList =
['“With a successful attack roll, the wizard maintains the thief in surfboard position.”',
'DM: So, what are your character’s flaws? Player: Acid reflux.',
'That’s too indiscriminate. We should go with the rampaging bear.',
'We’ve managed to teach the robot about the most important human emotion: Humiliation.',
'“Do you have a permit for that facial hair?”',
'Cleric: “Everybody chill, it’s totes God’s will.”',
'Did asterisks exist in 2006?'];
const msg = msgList[Math.floor(Math.random() * msgList.length)];
const msgContainer = document.getElementById('message-container');
msgContainer.innerText = msg;
}
</script>
<!--
<button type="button" class="nes-btn is-primary" onclick="randMessage()">*roll ldice*</button>
-->
<div class="nes-container is-rounded" id="message-container" style=" margin-top: 15px; margin-bottom: 15px; ">
<p></p>
</div>
<p style="text-align:center;">Click <a href="/data">here</a> to view content generated by a servlet.</p>
<p style="text-align:center;"><a href=" resume.html">Wanna see my resume? </a></p>
<p style="text-align:center;"><a href="cosplay.html">Here's some cosply stuff </a></p>

<!-- <p>Click below to see content generated from a servlet!</p>
<div id="wrapper">
<button onclick="servlet()" type="button" class="nes-btn is-success">Fetch a quote!</button>
</div> -->
<div class="nes-container with-title is-centered" style=" margin-top: 15px; margin-bottom: 15px; ">
<p class="title">comments</p>
<!-- comment stream -->
<p name="quote-container" id="quote-container"></p>

<form action="/data" method="POST">
<!-- here you can post a comment -->
<label for="textarea_field">post your comment:</label>
<textarea name="textarea_field" class="nes-textarea"></textarea>
<input type="submit" />
</form>
</div>
<p><a href=" resume.html">Wanna see my resume? </a></p>
<p><a href="cosplay.html">Here's some cosply stuff </a></p>

</div>
</body>
Expand Down
Loading