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

Dashboard #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ target/
.idea/
functional_tests/Gemfile.lock
functional_tests/log_app_err.txt
functional_tests/log_app_out.txt
functional_tests/log_app_out.txt


acebook.iml
example.log.1
velocity.log
example.log
30 changes: 26 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import models.Model;
import models.Sql2oModel;
import org.apache.log4j.BasicConfigurator;
import org.flywaydb.core.Flyway;
import org.sql2o.Sql2o;
import org.sql2o.converters.UUIDConverter;
import org.sql2o.quirks.PostgresQuirks;
import spark.ModelAndView;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.UUID;

import static spark.Spark.get;
import static spark.Spark.post;

public class Main {



public static void main(String[] args) {
String dbName = "acebook";
for(String a:args) {
Expand All @@ -33,16 +37,34 @@ public static void main(String[] args) {
Model model = new Sql2oModel(sql2o);



get("/", (req, res) -> "Hello World");


get("/posts", (req, res) -> {

get("/dashboard", (req, res) -> {

HashMap posts = new HashMap();

// HashMap posts = new HashMap();

return new ModelAndView(posts, "templates/posts.vtl");

return new ModelAndView(new HashMap(), "templates/dashboard.vtl");
}, new VelocityTemplateEngine());



post("/dashboard", (request, response) -> {

String content = request.queryParams("send_post");
LocalDateTime currentTimestamp = LocalDateTime.now();
model.createPost(content, String.valueOf(currentTimestamp));



HashMap dashboard = new HashMap();

return new ModelAndView(dashboard, "templates/dashboard.vtl");
}, new VelocityTemplateEngine());

}
}
6 changes: 3 additions & 3 deletions src/main/java/models/Model.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package models;


import java.sql.Date;
import java.util.List;
import java.util.UUID;

public interface Model {
UUID createPost(String title, String content);

void createPost(String content, String time);

List getAllPosts();
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/models/Post.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package models;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import lombok.Data;
@Data
public class Post {
private UUID post_id;
private String title;
private String content;
private Timestamp time;

public Post(UUID post_id, String content, Timestamp time) {
this.post_id = post_id;
this.content = content;
this.time = time;
}

}
14 changes: 11 additions & 3 deletions src/main/java/models/Sql2oModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ public Sql2oModel(Sql2o sql2o) {
}

@Override
public UUID createPost(String title, String content) {
//TODO - implement this
return null;
public void createPost(String content, String time) {
try (Connection conn = sql2o.beginTransaction()) {
UUID postsUuid = UUID.randomUUID();
conn.createQuery("insert into posts(post_id, content, time) VALUES (:post_id, :content, :time)")
.addParameter("post_id", postsUuid)
.addParameter("content", content)
.addParameter("time", time)
.executeUpdate();
conn.commit();

}
}

@Override
Expand Down
12 changes: 10 additions & 2 deletions src/main/resources/db/migration/V1__create_table.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
create table posts(
post_id VARCHAR,
title VARCHAR,
content VARCHAR
)
);

--In db acebook,table Posts:
ALTER TABLE posts ADD COLUMN time VARCHAR;




--Create new user table in acebook db:
CREATE TABLE person(user_id SERIAL PRIMARY KEY, user_name VARCHAR(60), password VARCHAR);
14 changes: 13 additions & 1 deletion src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
log4j.rootLogger=INFO
#log4j.rootLogger=INFO
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
48 changes: 48 additions & 0 deletions src/main/resources/templates/dashboard.vtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>Acebook!</title>
<link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
<link href='https://fonts.googleapis.com/css?family=El Messiri' rel='stylesheet'>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">


<style>
.title-font{
font-family: 'Sofia';
font-size: 30px;
margin-top: 5rem;
margin-bottom: 5rem;
display: flex;
flex-wrap: wrap;
align-content: center;
justify-content: center;
align-items: center;
}


body {
background: linear-gradient(to right, #feac5e, #c779d0, #4bc0c8);
}
</style>


</head>


<body>
<div class="title-font">
<h1 style="font-size: 50px; color: cornsilk;">&#129412; Acebook &#x2728;</h1>
</div>


<form action="/dashboard" method="post" class="form-group d-flex flex-column align-items-center">
<label for="exampleFormControlTextarea1">Any thoughts today? :</label>
<textarea class="form-control" id="exampleFormControlTextarea1" name= "send_post" rows="10"></textarea>
<button type="submit" class="btn btn-primary" style="width: 100px;">Send</button>
</form>

</body>

</html>
65 changes: 65 additions & 0 deletions src/main/resources/templates/homepage.vtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<title>Acebook!</title>
<link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
<link href='https://fonts.googleapis.com/css?family=El Messiri' rel='stylesheet'>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">


<style>
.title-font{
font-family: 'Sofia';
font-size: 30px;
margin-top: 5rem;
margin-bottom: 5rem;
display: flex;
flex-wrap: wrap;
align-content: center;
justify-content: center;
align-items: center;
}

.sign-in-form{
font-family: 'El Messiri';
align-items: stretch;
width: 20%;
font-size: 2rem;
}


body {
background: linear-gradient(to right, #feac5e, #c779d0, #4bc0c8);
}
</style>


</head>


<body>
<div class="title-font">
<h1 style="font-size: 50px; color: cornsilk;">&#129412; Acebook &#x2728;</h1>
</div>

<div class="container sign-in-form">
<form method="post" action="/dashboard" style="display: flex; flex-direction: column;">
<div class="form-group" >
<label for="Username">Username</label>
<input style="height: 35px" type="username" class="form-control" id="username" name="username" aria-describedby="emailHelp">
</div>

<div class="form-group">
<label for="Password">Password</label>
<input style="height: 35px" type="password" class="form-control" id="password">
</div>

<button style="height: 35px; width: 100px; font-size: 1.5rem; align-self: center;" type="submit" class="btn btn-primary">Submit</button>

</form>
</div>

</body>

</html>
11 changes: 0 additions & 11 deletions src/main/resources/templates/posts.vtl

This file was deleted.

38 changes: 19 additions & 19 deletions src/test/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

import static org.junit.Assert.assertEquals;

public class MainTest {

public static class WebAppTestSparkApp implements SparkApplication {
public void init() {
String[] arr = {};
Main.main(arr);
}
}

@ClassRule
public static SparkServer<WebAppTestSparkApp> testServer = new SparkServer<>(WebAppTestSparkApp.class, 4567);

@Test
public void serverRespondsSuccessfully() throws HttpClientException {
GetMethod request = testServer.get("/", false);
HttpResponse httpResponse = testServer.execute(request);
assertEquals(200, httpResponse.code());
}
}
//public class MainTest {
//
// public static class WebAppTestSparkApp implements SparkApplication {
// public void init() {
// String[] arr = {};
// Main.main(arr, request);
// }
// }
//
// @ClassRule
// public static SparkServer<WebAppTestSparkApp> testServer = new SparkServer<>(WebAppTestSparkApp.class, 4567);
//
// @Test
// public void serverRespondsSuccessfully() throws HttpClientException {
// GetMethod request = testServer.get("/", false);
// HttpResponse httpResponse = testServer.execute(request);
// assertEquals(200, httpResponse.code());
// }
//}