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

Like button #14

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
aaac1ac
create db
Nanariane Apr 6, 2020
140ac03
set up finished
Apr 6, 2020
9ce9202
update
Nanariane Apr 6, 2020
db68c1a
new update
Nanariane Apr 6, 2020
085ee2b
styling for homepage
Apr 6, 2020
3e68392
set up post and get for dashboard but data not going into the databas…
Nanariane Apr 7, 2020
3f3f064
content and time goes in database
Nanariane Apr 7, 2020
aff4c65
Merge pull request #1 from Nanariane/dashboard
Nanariane Apr 7, 2020
31b2cc8
adding users to database
AvrilHarding Apr 7, 2020
b575227
merged code
AvrilHarding Apr 7, 2020
dc970c5
try to merge
Apr 7, 2020
52c9452
Merge pull request #2 from Nanariane/usernames
AvrilHarding Apr 7, 2020
8f4b6bf
Merge branch 'master' of https://github.com/Nanariane/acebook-java-sp…
Apr 7, 2020
3a43bf3
took out return statement
Nanariane Apr 7, 2020
6b9eebe
Merge pull request #3 from Nanariane/dashboard
Nanariane Apr 7, 2020
03f6df0
Merge branch 'master' of https://github.com/Nanariane/acebook-java-sp…
Apr 7, 2020
2534841
main changes
AvrilHarding Apr 7, 2020
f708032
Merge pull request #4 from Nanariane/usernames
AvrilHarding Apr 7, 2020
fbe164f
Merge branch 'master' of https://github.com/Nanariane/acebook-java-sp…
Apr 7, 2020
3d4cf5f
data is not coming from db
Apr 7, 2020
11d84f7
the posts table is working
Apr 7, 2020
39f95c3
like button wip
Apr 8, 2020
2c85571
signin going to db/controller fixed
Apr 8, 2020
72ef95e
changed order of posts
AvrilHarding Apr 8, 2020
7063dad
Merge pull request #5 from Nanariane/orderofposts
AvrilHarding Apr 8, 2020
5c7d1f5
button added, no counts
Apr 8, 2020
865a0b3
Merge pull request #6 from Nanariane/like_button
educatedFlea Apr 8, 2020
42295c9
navbar and get username
AvrilHarding Apr 8, 2020
1688401
Merge branch 'master' into navbar
AvrilHarding Apr 8, 2020
f218900
Merge pull request #7 from Nanariane/navbar
AvrilHarding Apr 8, 2020
d5997fd
navbar fixed
Apr 8, 2020
cb2886a
sign up page added
Apr 9, 2020
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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ 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
velocity.log.1

74 changes: 68 additions & 6 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import models.Model;
import models.Sql2oModel;
import org.apache.log4j.BasicConfigurator;
import models.UserName;
import org.flywaydb.core.Flyway;
import org.sql2o.Sql2o;
import org.sql2o.converters.UUIDConverter;
import org.sql2o.quirks.PostgresQuirks;
import spark.ModelAndView;
import spark.Redirect;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
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) {
for (String a : args) {
dbName = a;
}
System.out.println(dbName);
Flyway flyway = Flyway.configure().dataSource("jdbc:postgresql://localhost:5432/"+dbName, null, null).load();
Flyway flyway = Flyway.configure().dataSource("jdbc:postgresql://localhost:5432/" + dbName, null, null).load();
flyway.migrate();

Sql2o sql2o = new Sql2o("jdbc:postgresql://localhost:5432/" + dbName, null, null, new PostgresQuirks() {
Expand All @@ -32,17 +38,73 @@ public static void main(String[] args) {

Model model = new Sql2oModel(sql2o);

get("/", (request, response) -> {

// HashMap homepage = new HashMap();

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

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

get("/", (req, res) -> "Hello World");
String user_name = request.queryParams("user_name_1");
String password = request.queryParams("psw");
model.addUser(user_name, password);

// HashMap homepage = new HashMap();

get("/posts", (req, res) -> {
response.redirect("/dashboard");
return null;
}

);


get("/dashboard", (req, res) -> {
model.getAllPosts();
HashMap posts = new HashMap();
posts.put("posts", model.getAllPosts());

posts.put("userName", model.getUserName());

return new ModelAndView(posts, "templates/posts.vtl");
return new ModelAndView(posts, "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));



response.redirect("/dashboard");
return null;
});

get("/signup", (request, response) -> {

// HashMap homepage = new HashMap();

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


post("/signup", (request, response) -> {
String first_name = request.queryParams("first_name");
String last_name = request.queryParams("last_name");
String user_name = request.queryParams("user_name_1");
String email = request.queryParams("email");
String password = request.queryParams("psw");
model.signUp(first_name, last_name, user_name, email, password);

response.redirect("/");
return null;
}

);
}


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

import lombok.Data;

import java.util.UUID;

@Data
public class AddUser {
private UUID user_id;
private String user_name;
private String password;

public AddUser(UUID user_id, String user_name, String password) {
this.user_id = user_id;
this.user_name = user_name;
this.password = password;
}
}
7 changes: 4 additions & 3 deletions src/main/java/models/Model.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package models;


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

public interface Model {
UUID createPost(String title, String content);
void addUser(String user_name, String password);
void createPost(String content, String time);
List getAllPosts();
List getUserName();
void signUp(String first_name, String last_name, String user_name, String email, String password);
}


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 String time;

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

}
61 changes: 56 additions & 5 deletions src/main/java/models/Sql2oModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,66 @@ public Sql2oModel(Sql2o sql2o) {
}

@Override
public UUID createPost(String title, String content) {
//TODO - implement this
return null;
public void addUser(String user_name, String password) {
try (Connection conn = sql2o.beginTransaction()) {
UUID personUuid = UUID.randomUUID();
conn.createQuery("insert into person(user_id, user_name, password) VALUES (:user_id, :user_name, :password)")
.addParameter("user_id", personUuid)
.addParameter("user_name", user_name)
.addParameter("password", password)
.executeUpdate();
conn.commit();

}
}

@Override
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
public List<Post> getAllPosts() {
//TODO - implement this
return null;
try (Connection conn = sql2o.open()) {
List<Post> items = conn.createQuery("select * from posts ORDER BY time DESC")
.executeAndFetch(Post.class);
return items;
}
}

@Override
public List getUserName() {
try (Connection conn = sql2o.open()) {
List<UserName> userName = conn.createQuery("select user_name from person ORDER BY password DESC LIMIT 1")
.executeAndFetch(UserName.class);
return userName;
}
}

@Override
public void signUp(String first_name, String last_name, String user_name, String email, String password) {
try (Connection conn = sql2o.beginTransaction()) {
UUID signUpUuid = UUID.randomUUID();
conn.createQuery("insert into signup(user_id, first_name, last_name, user_name, email, password) VALUES (:user_id, :first_name, :last_name, :user_name, :email, :password)")
.addParameter("user_id", signUpUuid)
.addParameter("first_name", first_name)
.addParameter("last_name", last_name)
.addParameter("user_name", user_name)
.addParameter("email", email)
.addParameter("password", password)
.executeUpdate();
conn.commit();

}
}

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

import lombok.Data;

import java.util.UUID;
@Data
public class UserName {
private UUID user_id;
private String user_name;
private String password;


public UserName(UUID user_id, String user_name, String password) {
this.user_id = user_id;
this.user_name = user_name;
this.password = password;
}
}
22 changes: 20 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,23 @@
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 VARCHAR, user_name VARCHAR(60), password VARCHAR);

--create sign up table
create table signup(
user_id VARCHAR,
first_name VARCHAR,
last_name VARCHAR,
user_name VARCHAR,
email VARCHAR,
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
Loading