From 830a95be7a4b418c852081b23d64465f7d997441 Mon Sep 17 00:00:00 2001 From: xengeo Date: Tue, 14 Nov 2023 11:52:18 +0000 Subject: [PATCH 01/60] Initial commit - add test data --- pom.xml | 2 +- .../V3__update_existing_create_new_tables.sql | 38 +++++++++++++++++++ .../db/migration/V4__insert_test_data.sql | 7 ++++ .../db/migration/V5__insert_into_posts.sql | 2 + .../db/migration/V6__insert_into_comments.sql | 2 + .../db/migration/V7__insert_into_likes.sql | 2 + .../db/migration/V8__insert_into_friends.sql | 2 + .../db/migration/V9__add_new_user.sql | 2 + 8 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/db/migration/V3__update_existing_create_new_tables.sql create mode 100644 src/main/resources/db/migration/V4__insert_test_data.sql create mode 100644 src/main/resources/db/migration/V5__insert_into_posts.sql create mode 100644 src/main/resources/db/migration/V6__insert_into_comments.sql create mode 100644 src/main/resources/db/migration/V7__insert_into_likes.sql create mode 100644 src/main/resources/db/migration/V8__insert_into_friends.sql create mode 100644 src/main/resources/db/migration/V9__add_new_user.sql diff --git a/pom.xml b/pom.xml index dc2e28b62..261744b50 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ org.projectlombok lombok - 1.18.24 + 1.18.30 provided diff --git a/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql b/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql new file mode 100644 index 000000000..6032cbbfc --- /dev/null +++ b/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql @@ -0,0 +1,38 @@ +ALTER TABLE posts +ADD COLUMN timestamp timestamp, +ADD COLUMN user_id integer, +ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id); + + +ALTER TABLE users +ADD image_url varchar(500); + + +CREATE TABLE likes ( + id bigserial PRIMARY KEY, + like_status boolean DEFAULT false, + user_id integer, + post_id integer, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (post_id) REFERENCES posts(id), + UNIQUE (user_id, post_id) +); + + +CREATE TABLE comments ( + id bigserial PRIMARY KEY, + comment varchar(281) NOT NULL, + post_id integer, + user_id integer, + FOREIGN KEY (post_id) REFERENCES posts(id), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +CREATE TABLE friends ( + id bigserial PRIMARY KEY, + requester_id integer, + receiver_id integer, + FOREIGN KEY (requester_id) REFERENCES users(id), + FOREIGN KEY (receiver_id) REFERENCES users(id), + status varchar(50) check (status in ('ACCEPTED', 'PENDING', 'DENIED')) +); \ No newline at end of file diff --git a/src/main/resources/db/migration/V4__insert_test_data.sql b/src/main/resources/db/migration/V4__insert_test_data.sql new file mode 100644 index 000000000..26c1fd78d --- /dev/null +++ b/src/main/resources/db/migration/V4__insert_test_data.sql @@ -0,0 +1,7 @@ +insert into users (username, password, enabled, image_url) +values ('zak', '12345', TRUE, '/users/images/image.png'), + ('david', '123456', TRUE, '/users/images/another_image.png'); + + + + diff --git a/src/main/resources/db/migration/V5__insert_into_posts.sql b/src/main/resources/db/migration/V5__insert_into_posts.sql new file mode 100644 index 000000000..5df524f2a --- /dev/null +++ b/src/main/resources/db/migration/V5__insert_into_posts.sql @@ -0,0 +1,2 @@ +insert into posts (content, timestamp, user_id) +values ('first post', '2023-11-14 11:13:24', 2); \ No newline at end of file diff --git a/src/main/resources/db/migration/V6__insert_into_comments.sql b/src/main/resources/db/migration/V6__insert_into_comments.sql new file mode 100644 index 000000000..2ef617deb --- /dev/null +++ b/src/main/resources/db/migration/V6__insert_into_comments.sql @@ -0,0 +1,2 @@ +insert into comments (comment, post_id, user_id) +values ('great post', 1, 3); \ No newline at end of file diff --git a/src/main/resources/db/migration/V7__insert_into_likes.sql b/src/main/resources/db/migration/V7__insert_into_likes.sql new file mode 100644 index 000000000..ff91314cd --- /dev/null +++ b/src/main/resources/db/migration/V7__insert_into_likes.sql @@ -0,0 +1,2 @@ +insert into likes (like_status, user_id, post_id) +values (true, 2, 1), (true, 3, 1); \ No newline at end of file diff --git a/src/main/resources/db/migration/V8__insert_into_friends.sql b/src/main/resources/db/migration/V8__insert_into_friends.sql new file mode 100644 index 000000000..27f5ea1f2 --- /dev/null +++ b/src/main/resources/db/migration/V8__insert_into_friends.sql @@ -0,0 +1,2 @@ +insert into friends (requester_id, receiver_id, status) +values (2, 3, 'PENDING'); \ No newline at end of file diff --git a/src/main/resources/db/migration/V9__add_new_user.sql b/src/main/resources/db/migration/V9__add_new_user.sql new file mode 100644 index 000000000..1b26a72e1 --- /dev/null +++ b/src/main/resources/db/migration/V9__add_new_user.sql @@ -0,0 +1,2 @@ +insert into users (username, password, enabled, image_url) +values ('som', '123457', TRUE, '/users/images/image_three.png'); From 07f7ab4ccae5fbf33cdee2df0b988248368022ad Mon Sep 17 00:00:00 2001 From: Natalie Clark Date: Tue, 14 Nov 2023 11:59:22 +0000 Subject: [PATCH 02/60] Update lombok version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index dc2e28b62..261744b50 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ org.projectlombok lombok - 1.18.24 + 1.18.30 provided From 94c3e5fbbba858647305d732594fb8561cf60578 Mon Sep 17 00:00:00 2001 From: somunachima Date: Tue, 14 Nov 2023 15:44:52 +0000 Subject: [PATCH 03/60] add route for users show page and html file for users show --- .../acebook/controller/UsersController.java | 15 +++++++++++++++ src/main/resources/templates/users/show.html | 11 +++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/resources/templates/users/show.html diff --git a/src/main/java/com/makersacademy/acebook/controller/UsersController.java b/src/main/java/com/makersacademy/acebook/controller/UsersController.java index 3c46bf0a1..f25d283e8 100644 --- a/src/main/java/com/makersacademy/acebook/controller/UsersController.java +++ b/src/main/java/com/makersacademy/acebook/controller/UsersController.java @@ -9,9 +9,13 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; +import java.util.Optional; + @Controller public class UsersController { @@ -33,4 +37,15 @@ public RedirectView signup(@ModelAttribute User user) { authoritiesRepository.save(authority); return new RedirectView("/login"); } + + @GetMapping("/users/{id}") + public ModelAndView show(@PathVariable Long id) { + Optional optionalUser = userRepository.findById(id); + User user = optionalUser.orElse(null); + ModelAndView modelAndView = new ModelAndView("/users/show"); + modelAndView.addObject("user", user); + System.out.println("This is an OBJECT!!!!!!"); + System.out.println(user); + return modelAndView; + } } diff --git a/src/main/resources/templates/users/show.html b/src/main/resources/templates/users/show.html new file mode 100644 index 000000000..cc807a4ce --- /dev/null +++ b/src/main/resources/templates/users/show.html @@ -0,0 +1,11 @@ + + + + + Your Profile + + +

Welcome to your profile!

+

+ + \ No newline at end of file From 81cbd3c922230aad8dd2504554fc18e551d953f7 Mon Sep 17 00:00:00 2001 From: somunachima Date: Tue, 14 Nov 2023 16:15:45 +0000 Subject: [PATCH 04/60] write tests for users controller --- .../controller/UsersControllerTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java diff --git a/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java new file mode 100644 index 000000000..a5efcf018 --- /dev/null +++ b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java @@ -0,0 +1,53 @@ +package com.makersacademy.aceboook.controller; + +import com.github.javafaker.Faker; +import com.makersacademy.acebook.Application; +import jdk.internal.org.xml.sax.Locator; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class UsersControllerTest { + + WebDriver driver; + WebDriver page; + Faker faker; + + @Before + public void setup() { + System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); + driver = new ChromeDriver(); + faker = new Faker(); + } + + @After + public void tearDown() { + driver.close(); + } + +// @Test +// public void usersCanReadUsername() { +// driver.get("localhost:8080/users/3"); +// String username = driver.getTitle(); +// Assert.assertEquals("tom", username); +// } + +// @Test +// public void successfulSignUpRedirectsToSignIn() { +// driver.get("http://localhost:8080/users/new"); +// driver.findElement(By.id("username")).sendKeys(faker.name().firstName()); +// driver.findElement(By.id("password")).sendKeys("password"); +// driver.findElement(By.id("submit")).click(); +// String title = driver.getTitle(); +// Assert.assertEquals("Please sign in", title); +// } +} From b4418a9654ef5d0ceeca66ee5a4d28beef58df37 Mon Sep 17 00:00:00 2001 From: Natalie Clark Date: Tue, 14 Nov 2023 17:02:12 +0000 Subject: [PATCH 05/60] Update post model fields and add sign up button --- .../acebook/SecurityConfiguration.java | 2 +- .../acebook/controller/HomeController.java | 6 +++ .../makersacademy/acebook/model/Comment.java | 4 ++ .../com/makersacademy/acebook/model/Post.java | 18 ++++++++- .../V10__update_userid_to_bigint.sql | 3 ++ .../V3__update_existing_create_new_tables.sql | 38 +++++++++++++++++++ .../db/migration/V4__insert_test_data.sql | 7 ++++ .../db/migration/V5__insert_into_posts.sql | 2 + .../db/migration/V6__insert_into_comments.sql | 2 + .../db/migration/V7__insert_into_likes.sql | 2 + .../db/migration/V8__insert_into_friends.sql | 2 + .../db/migration/V9__add_new_user.sql | 2 + src/main/resources/templates/login.html | 32 ++++++++++++++++ .../makersacademy/acebook/model/PostTest.java | 11 +++++- 14 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/makersacademy/acebook/model/Comment.java create mode 100644 src/main/resources/db/migration/V10__update_userid_to_bigint.sql create mode 100644 src/main/resources/db/migration/V3__update_existing_create_new_tables.sql create mode 100644 src/main/resources/db/migration/V4__insert_test_data.sql create mode 100644 src/main/resources/db/migration/V5__insert_into_posts.sql create mode 100644 src/main/resources/db/migration/V6__insert_into_comments.sql create mode 100644 src/main/resources/db/migration/V7__insert_into_likes.sql create mode 100644 src/main/resources/db/migration/V8__insert_into_friends.sql create mode 100644 src/main/resources/db/migration/V9__add_new_user.sql create mode 100644 src/main/resources/templates/login.html diff --git a/src/main/java/com/makersacademy/acebook/SecurityConfiguration.java b/src/main/java/com/makersacademy/acebook/SecurityConfiguration.java index a6829646e..b8fbb1563 100644 --- a/src/main/java/com/makersacademy/acebook/SecurityConfiguration.java +++ b/src/main/java/com/makersacademy/acebook/SecurityConfiguration.java @@ -28,7 +28,7 @@ protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/posts").hasRole("USER") .antMatchers("/users").permitAll() - .and().formLogin(); + .and().formLogin().loginPage("/login"); } @Bean diff --git a/src/main/java/com/makersacademy/acebook/controller/HomeController.java b/src/main/java/com/makersacademy/acebook/controller/HomeController.java index 2036ec7e0..4c04bbf80 100644 --- a/src/main/java/com/makersacademy/acebook/controller/HomeController.java +++ b/src/main/java/com/makersacademy/acebook/controller/HomeController.java @@ -10,4 +10,10 @@ public class HomeController { public RedirectView index() { return new RedirectView("/posts"); } + + @RequestMapping("/login") + public String login() { + return "login"; + } } + diff --git a/src/main/java/com/makersacademy/acebook/model/Comment.java b/src/main/java/com/makersacademy/acebook/model/Comment.java new file mode 100644 index 000000000..a9d2e5921 --- /dev/null +++ b/src/main/java/com/makersacademy/acebook/model/Comment.java @@ -0,0 +1,4 @@ +package com.makersacademy.acebook.model; + +public class Comment { +} diff --git a/src/main/java/com/makersacademy/acebook/model/Post.java b/src/main/java/com/makersacademy/acebook/model/Post.java index 0098de1b3..bbee89e7b 100644 --- a/src/main/java/com/makersacademy/acebook/model/Post.java +++ b/src/main/java/com/makersacademy/acebook/model/Post.java @@ -8,6 +8,8 @@ import lombok.Data; +import java.sql.Timestamp; + @Data @Entity @Table(name = "POSTS") @@ -18,12 +20,24 @@ public class Post { private Long id; private String content; + private Timestamp timestamp; + + private Long userId; + public Post() {} - public Post(String content) { + public Post(String content, Timestamp timestamp, Long userId) { this.content = content; + this.timestamp = timestamp; + this.userId = userId; } - public String getContent() { return this.content; } + public String getContent() {return this.content;} public void setContent(String content) { this.content = content; } + public Timestamp getTimestamp() {return this.timestamp;} + public void setTimestamp(Timestamp timestamp) { this.timestamp = timestamp; } + + public Long getUserId() {return this.userId;} + public void setUserId(Long userId) { this.userId = userId; } + } diff --git a/src/main/resources/db/migration/V10__update_userid_to_bigint.sql b/src/main/resources/db/migration/V10__update_userid_to_bigint.sql new file mode 100644 index 000000000..83c91ad42 --- /dev/null +++ b/src/main/resources/db/migration/V10__update_userid_to_bigint.sql @@ -0,0 +1,3 @@ +ALTER TABLE posts +ALTER COLUMN user_id +TYPE bigint; diff --git a/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql b/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql new file mode 100644 index 000000000..6032cbbfc --- /dev/null +++ b/src/main/resources/db/migration/V3__update_existing_create_new_tables.sql @@ -0,0 +1,38 @@ +ALTER TABLE posts +ADD COLUMN timestamp timestamp, +ADD COLUMN user_id integer, +ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id); + + +ALTER TABLE users +ADD image_url varchar(500); + + +CREATE TABLE likes ( + id bigserial PRIMARY KEY, + like_status boolean DEFAULT false, + user_id integer, + post_id integer, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (post_id) REFERENCES posts(id), + UNIQUE (user_id, post_id) +); + + +CREATE TABLE comments ( + id bigserial PRIMARY KEY, + comment varchar(281) NOT NULL, + post_id integer, + user_id integer, + FOREIGN KEY (post_id) REFERENCES posts(id), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +CREATE TABLE friends ( + id bigserial PRIMARY KEY, + requester_id integer, + receiver_id integer, + FOREIGN KEY (requester_id) REFERENCES users(id), + FOREIGN KEY (receiver_id) REFERENCES users(id), + status varchar(50) check (status in ('ACCEPTED', 'PENDING', 'DENIED')) +); \ No newline at end of file diff --git a/src/main/resources/db/migration/V4__insert_test_data.sql b/src/main/resources/db/migration/V4__insert_test_data.sql new file mode 100644 index 000000000..26c1fd78d --- /dev/null +++ b/src/main/resources/db/migration/V4__insert_test_data.sql @@ -0,0 +1,7 @@ +insert into users (username, password, enabled, image_url) +values ('zak', '12345', TRUE, '/users/images/image.png'), + ('david', '123456', TRUE, '/users/images/another_image.png'); + + + + diff --git a/src/main/resources/db/migration/V5__insert_into_posts.sql b/src/main/resources/db/migration/V5__insert_into_posts.sql new file mode 100644 index 000000000..5df524f2a --- /dev/null +++ b/src/main/resources/db/migration/V5__insert_into_posts.sql @@ -0,0 +1,2 @@ +insert into posts (content, timestamp, user_id) +values ('first post', '2023-11-14 11:13:24', 2); \ No newline at end of file diff --git a/src/main/resources/db/migration/V6__insert_into_comments.sql b/src/main/resources/db/migration/V6__insert_into_comments.sql new file mode 100644 index 000000000..2ef617deb --- /dev/null +++ b/src/main/resources/db/migration/V6__insert_into_comments.sql @@ -0,0 +1,2 @@ +insert into comments (comment, post_id, user_id) +values ('great post', 1, 3); \ No newline at end of file diff --git a/src/main/resources/db/migration/V7__insert_into_likes.sql b/src/main/resources/db/migration/V7__insert_into_likes.sql new file mode 100644 index 000000000..ff91314cd --- /dev/null +++ b/src/main/resources/db/migration/V7__insert_into_likes.sql @@ -0,0 +1,2 @@ +insert into likes (like_status, user_id, post_id) +values (true, 2, 1), (true, 3, 1); \ No newline at end of file diff --git a/src/main/resources/db/migration/V8__insert_into_friends.sql b/src/main/resources/db/migration/V8__insert_into_friends.sql new file mode 100644 index 000000000..27f5ea1f2 --- /dev/null +++ b/src/main/resources/db/migration/V8__insert_into_friends.sql @@ -0,0 +1,2 @@ +insert into friends (requester_id, receiver_id, status) +values (2, 3, 'PENDING'); \ No newline at end of file diff --git a/src/main/resources/db/migration/V9__add_new_user.sql b/src/main/resources/db/migration/V9__add_new_user.sql new file mode 100644 index 000000000..1b26a72e1 --- /dev/null +++ b/src/main/resources/db/migration/V9__add_new_user.sql @@ -0,0 +1,2 @@ +insert into users (username, password, enabled, image_url) +values ('som', '123457', TRUE, '/users/images/image_three.png'); diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 000000000..4d1d4e92d --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,32 @@ + + + Messages : Create + + +
+
+
+ Please Login +
+ Invalid username and password. +
+
+ You have been logged out. +
+ + + + +
+ +
+
+
+
+
+ +
+
+
+ + \ No newline at end of file diff --git a/src/test/java/com/makersacademy/acebook/model/PostTest.java b/src/test/java/com/makersacademy/acebook/model/PostTest.java index 732aafc6e..6bacdb339 100644 --- a/src/test/java/com/makersacademy/acebook/model/PostTest.java +++ b/src/test/java/com/makersacademy/acebook/model/PostTest.java @@ -5,13 +5,22 @@ import org.junit.Test; +import java.sql.Timestamp; + public class PostTest { - private Post post = new Post("hello"); + Timestamp timestamp = Timestamp.valueOf("2007-09-23 10:10:10.0");; + + private Post post = new Post("hello", timestamp, 1L); @Test public void postHasContent() { assertThat(post.getContent(), containsString("hello")); } +// @Test +// public void postHasContent() { +// assertThat(post.getContent(), containsString("hello")); +// } +// } From 51dd9c648e3bed2600c9e906ccebc29238789560 Mon Sep 17 00:00:00 2001 From: xengeo Date: Wed, 15 Nov 2023 10:21:26 +0000 Subject: [PATCH 06/60] update column types to bigint and complete comment model implementation --- .../makersacademy/acebook/model/Comment.java | 48 +++++++++++++++++++ ...11__update_userid_and_postid_to_bigint.sql | 4 ++ 2 files changed, 52 insertions(+) create mode 100644 src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql diff --git a/src/main/java/com/makersacademy/acebook/model/Comment.java b/src/main/java/com/makersacademy/acebook/model/Comment.java index a9d2e5921..a61f7bd26 100644 --- a/src/main/java/com/makersacademy/acebook/model/Comment.java +++ b/src/main/java/com/makersacademy/acebook/model/Comment.java @@ -1,4 +1,52 @@ package com.makersacademy.acebook.model; + +import lombok.Data; + +import javax.persistence.*; + +@Data +@Entity +@Table(name = "COMMENTS") public class Comment { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String comment; + private Long postId; + private Long userId; + + public Comment() {}; + + public Comment(String comment, Long postId, Long userId) { + this.comment = comment; + this.postId = postId; + this.userId = userId; + } + + public String getComment() { + return this.comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + public Long getPostId() { + return this.postId; + } + + public void setPostId(Long postId) { + this.postId = postId; + } + + public Long getUserId() { + return this.userId; + } + + public void setUserId(Long UserId) { + this.userId = UserId; + } } diff --git a/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql b/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql new file mode 100644 index 000000000..36f980687 --- /dev/null +++ b/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql @@ -0,0 +1,4 @@ +ALTER TABLE posts +ALTER COLUMN user_id TYPE bigint +ALTER COLUMN post_id TYPE bigint; + From 6f562d997846720713aee17d74803e6a809fdd39 Mon Sep 17 00:00:00 2001 From: xengeo Date: Wed, 15 Nov 2023 10:32:13 +0000 Subject: [PATCH 07/60] update V11 migration file for column type change --- .../migration/V11__update_userid_and_postid_to_bigint.sql | 8 ++++++-- .../aceboook/controller/UsersControllerTest.java | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql b/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql index 36f980687..ccf94621b 100644 --- a/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql +++ b/src/main/resources/db/migration/V11__update_userid_and_postid_to_bigint.sql @@ -1,4 +1,8 @@ -ALTER TABLE posts -ALTER COLUMN user_id TYPE bigint +ALTER TABLE comments ALTER COLUMN post_id TYPE bigint; +ALTER TABLE comments +ALTER COLUMN user_id TYPE bigint; + + + diff --git a/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java index a5efcf018..f7c44f4c9 100644 --- a/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java +++ b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java @@ -2,7 +2,7 @@ import com.github.javafaker.Faker; import com.makersacademy.acebook.Application; -import jdk.internal.org.xml.sax.Locator; +//import jdk.internal.org.xml.sax.Locator; import org.junit.After; import org.junit.Assert; import org.junit.Before; From 533486ae7ed7fd10b11f0a0ad6ae3a88747faeca Mon Sep 17 00:00:00 2001 From: David Sholoye Date: Thu, 16 Nov 2023 12:05:32 +0000 Subject: [PATCH 08/60] Add header and links to profile, friends and timeline. And signout --- .../acebook/controller/UsersController.java | 1 + src/main/resources/templates/posts/index.html | 19 +++++++++ src/main/resources/templates/users/show.html | 39 +++++++++++++++---- .../controller/UsersControllerTest.java | 16 ++++---- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/makersacademy/acebook/controller/UsersController.java b/src/main/java/com/makersacademy/acebook/controller/UsersController.java index f25d283e8..c0d259fa2 100644 --- a/src/main/java/com/makersacademy/acebook/controller/UsersController.java +++ b/src/main/java/com/makersacademy/acebook/controller/UsersController.java @@ -48,4 +48,5 @@ public ModelAndView show(@PathVariable Long id) { System.out.println(user); return modelAndView; } + } diff --git a/src/main/resources/templates/posts/index.html b/src/main/resources/templates/posts/index.html index 4eb260155..c9baba052 100644 --- a/src/main/resources/templates/posts/index.html +++ b/src/main/resources/templates/posts/index.html @@ -8,6 +8,25 @@ +
+ + Profile Image + + +
+

Posts

diff --git a/src/main/resources/templates/users/show.html b/src/main/resources/templates/users/show.html index cc807a4ce..185148260 100644 --- a/src/main/resources/templates/users/show.html +++ b/src/main/resources/templates/users/show.html @@ -1,11 +1,36 @@ - - - - Your Profile + + + + + -

Welcome to your profile!

-

+ +
+
+ + Profile Image + +
+ +
+ +

+ - \ No newline at end of file + + diff --git a/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java index a5efcf018..0f8b6cd87 100644 --- a/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java +++ b/src/test/java/com/makersacademy/aceboook/controller/UsersControllerTest.java @@ -2,7 +2,6 @@ import com.github.javafaker.Faker; import com.makersacademy.acebook.Application; -import jdk.internal.org.xml.sax.Locator; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -10,6 +9,7 @@ import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -34,12 +34,14 @@ public void tearDown() { driver.close(); } -// @Test -// public void usersCanReadUsername() { -// driver.get("localhost:8080/users/3"); -// String username = driver.getTitle(); -// Assert.assertEquals("tom", username); -// } + @Test + public void usersCanReadUsername() { + driver.get("localhost:8080/users/3"); + WebElement pElement = driver.findElement(By.tagName("p")); + String pText = pElement.getText(); + Assert.assertEquals("zak ", pText); + + } // @Test // public void successfulSignUpRedirectsToSignIn() { From 95c4d7de68638fb2f8144402cea3a753805e9b48 Mon Sep 17 00:00:00 2001 From: xengeo Date: Thu, 16 Nov 2023 15:26:35 +0000 Subject: [PATCH 09/60] tested sign up features, checked for empty credentials --- .../acebook/controller/UsersController.java | 22 +++++++++- src/main/resources/templates/login.html | 4 +- src/main/resources/templates/users/new.html | 5 ++- src/test/java/SignUpTest.java | 40 ++++++++++++++++++- .../makersacademy/acebook/model/PostTest.java | 10 ++--- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/makersacademy/acebook/controller/UsersController.java b/src/main/java/com/makersacademy/acebook/controller/UsersController.java index f25d283e8..588f2f4fe 100644 --- a/src/main/java/com/makersacademy/acebook/controller/UsersController.java +++ b/src/main/java/com/makersacademy/acebook/controller/UsersController.java @@ -12,10 +12,14 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.view.RedirectView; +import java.util.ArrayList; import java.util.Optional; +import static java.lang.Boolean.valueOf; + @Controller public class UsersController { @@ -31,7 +35,23 @@ public String signup(Model model) { } @PostMapping("/users") - public RedirectView signup(@ModelAttribute User user) { + public RedirectView signup(@ModelAttribute User user, RedirectAttributes attributes) { + + String usernameErrorMsg = "Please enter a username"; + String passwordErrorMsg = "Please enter a password"; + + if (user.getUsername().trim().isEmpty() && user.getPassword().trim().isEmpty()){ + attributes.addAttribute("usernameError", usernameErrorMsg); + attributes.addAttribute("passwordError", passwordErrorMsg); + return new RedirectView("/users/new"); + } else if (user.getPassword().trim().isEmpty()) { + attributes.addAttribute("passwordError", passwordErrorMsg); + return new RedirectView("/users/new"); + } else if (user.getUsername().trim().isEmpty()) { + attributes.addAttribute("usernameError", usernameErrorMsg); + return new RedirectView("/users/new"); + } + userRepository.save(user); Authority authority = new Authority(user.getUsername(), "ROLE_USER"); authoritiesRepository.save(authority); diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index 4d1d4e92d..34cf6f3e0 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -1,6 +1,6 @@ - Messages : Create + Please log in
@@ -24,7 +24,7 @@
- +
diff --git a/src/main/resources/templates/users/new.html b/src/main/resources/templates/users/new.html index 2d763f396..e8e8b8d2d 100644 --- a/src/main/resources/templates/users/new.html +++ b/src/main/resources/templates/users/new.html @@ -1,12 +1,13 @@ - - + Signup
+

+

Username:

Password:

diff --git a/src/test/java/SignUpTest.java b/src/test/java/SignUpTest.java index b0e16955b..fec9926fa 100644 --- a/src/test/java/SignUpTest.java +++ b/src/test/java/SignUpTest.java @@ -7,7 +7,10 @@ import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -38,6 +41,41 @@ public void successfulSignUpRedirectsToSignIn() { driver.findElement(By.id("password")).sendKeys("password"); driver.findElement(By.id("submit")).click(); String title = driver.getTitle(); - Assert.assertEquals("Please sign in", title); + Assert.assertEquals("Please log in", title); + } + + + @Test + public void signUpButtonLinksToSignUpPage() { + driver.get("http://localhost:8080/login"); + driver.findElement(By.id("sign-up-btn")).click(); + String title = driver.getTitle(); + Assert.assertEquals("Signup", title); + } + + @Test + public void testBlankUsernameReturnsError() { + driver.get("http://localhost:8080/users/new"); + driver.findElement(By.id("username")).sendKeys(" "); + driver.findElement(By.id("password")).sendKeys("password"); + driver.findElement(By.id("submit")).click(); + // Explicitly wait for the error message to be visible + WebDriverWait wait = new WebDriverWait(driver, 10); + WebElement usernameError = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("usernameError"))); + String errorString = usernameError.getText(); + Assert.assertEquals("Please enter a username", errorString); + } + + @Test + public void testBlankPasswordReturnsError() { + driver.get("http://localhost:8080/users/new"); + driver.findElement(By.id("username")).sendKeys("username_1"); + driver.findElement(By.id("password")).sendKeys(" "); + driver.findElement(By.id("submit")).click(); + // Explicitly wait for the error message to be visible + WebDriverWait wait = new WebDriverWait(driver, 10); + WebElement passwordError = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("passwordError"))); + String errorString = passwordError.getText(); + Assert.assertEquals("Please enter a password", errorString); } } diff --git a/src/test/java/com/makersacademy/acebook/model/PostTest.java b/src/test/java/com/makersacademy/acebook/model/PostTest.java index 6bacdb339..be918f1d3 100644 --- a/src/test/java/com/makersacademy/acebook/model/PostTest.java +++ b/src/test/java/com/makersacademy/acebook/model/PostTest.java @@ -18,9 +18,9 @@ public void postHasContent() { assertThat(post.getContent(), containsString("hello")); } -// @Test -// public void postHasContent() { -// assertThat(post.getContent(), containsString("hello")); -// } -// + @Test + public void postHasU() { + assertThat(post.getContent(), containsString("hello")); + } + } From a1227eafc1712aeae33f637ace731442533b218d Mon Sep 17 00:00:00 2001 From: David Sholoye Date: Thu, 16 Nov 2023 16:16:14 +0000 Subject: [PATCH 10/60] Friend request system with errors --- .../acebook/controller/FriendController.java | 25 +++++++++++++ .../acebook/controller/PostsController.java | 10 ++++-- .../acebook/controller/UsersController.java | 2 -- .../makersacademy/acebook/model/Friend.java | 36 +++++++++++++++++++ .../acebook/repository/FriendRepository.java | 7 ++++ src/main/resources/templates/posts/index.html | 6 ++-- src/main/resources/templates/users/show.html | 12 +++++-- 7 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/makersacademy/acebook/controller/FriendController.java create mode 100644 src/main/java/com/makersacademy/acebook/model/Friend.java create mode 100644 src/main/java/com/makersacademy/acebook/repository/FriendRepository.java diff --git a/src/main/java/com/makersacademy/acebook/controller/FriendController.java b/src/main/java/com/makersacademy/acebook/controller/FriendController.java new file mode 100644 index 000000000..816238975 --- /dev/null +++ b/src/main/java/com/makersacademy/acebook/controller/FriendController.java @@ -0,0 +1,25 @@ +package com.makersacademy.acebook.controller; + +import com.makersacademy.acebook.model.Friend; +import com.makersacademy.acebook.repository.FriendRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +public class FriendController { + + @Autowired + FriendRepository friendRepository; + + @PostMapping("/handleFriendRequest/{receiver_id}") + public RedirectView create(@ModelAttribute Friend friend, Long receiver_id) { + friendRepository.save(friend); + RedirectView redirectView = new RedirectView("users/show"); + redirectView.addStaticAttribute("receiver_id", receiver_id); + return redirectView; + } +} diff --git a/src/main/java/com/makersacademy/acebook/controller/PostsController.java b/src/main/java/com/makersacademy/acebook/controller/PostsController.java index 57a7e5f4d..91aa5f26c 100644 --- a/src/main/java/com/makersacademy/acebook/controller/PostsController.java +++ b/src/main/java/com/makersacademy/acebook/controller/PostsController.java @@ -1,24 +1,28 @@ package com.makersacademy.acebook.controller; import com.makersacademy.acebook.model.Post; +import com.makersacademy.acebook.model.User; import com.makersacademy.acebook.repository.PostRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; import java.util.List; +import java.util.Optional; @Controller public class PostsController { @Autowired - PostRepository repository; + PostRepository postRepository; + @GetMapping("/posts") public String index(Model model) { - Iterable posts = repository.findAll(); + Iterable posts = postRepository.findAll(); model.addAttribute("posts", posts); model.addAttribute("post", new Post()); return "posts/index"; @@ -26,7 +30,7 @@ public String index(Model model) { @PostMapping("/posts") public RedirectView create(@ModelAttribute Post post) { - repository.save(post); + postRepository.save(post); return new RedirectView("/posts"); } } diff --git a/src/main/java/com/makersacademy/acebook/controller/UsersController.java b/src/main/java/com/makersacademy/acebook/controller/UsersController.java index c0d259fa2..393b03b04 100644 --- a/src/main/java/com/makersacademy/acebook/controller/UsersController.java +++ b/src/main/java/com/makersacademy/acebook/controller/UsersController.java @@ -44,8 +44,6 @@ public ModelAndView show(@PathVariable Long id) { User user = optionalUser.orElse(null); ModelAndView modelAndView = new ModelAndView("/users/show"); modelAndView.addObject("user", user); - System.out.println("This is an OBJECT!!!!!!"); - System.out.println(user); return modelAndView; } diff --git a/src/main/java/com/makersacademy/acebook/model/Friend.java b/src/main/java/com/makersacademy/acebook/model/Friend.java new file mode 100644 index 000000000..b956c69b7 --- /dev/null +++ b/src/main/java/com/makersacademy/acebook/model/Friend.java @@ -0,0 +1,36 @@ +package com.makersacademy.acebook.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.GenerationType; + +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +@Data +@Entity +@Table(name = "FRIENDS") +public class Friend { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Getter + private Long requester_id; + @Getter + private Long receiver_id; + + public Friend() {} + + public Friend(Long requester_id, Long receiver_id) { + this.requester_id = requester_id; + this.receiver_id = receiver_id; + } + + public void setRequesterId(Long requester_id) { this.requester_id = requester_id; } + public void setReceiver_id(Long receiver_id) { this.receiver_id = receiver_id; } + +} diff --git a/src/main/java/com/makersacademy/acebook/repository/FriendRepository.java b/src/main/java/com/makersacademy/acebook/repository/FriendRepository.java new file mode 100644 index 000000000..f9cd5efd7 --- /dev/null +++ b/src/main/java/com/makersacademy/acebook/repository/FriendRepository.java @@ -0,0 +1,7 @@ +package com.makersacademy.acebook.repository; + +import com.makersacademy.acebook.model.Friend; +import org.springframework.data.repository.CrudRepository; + +public interface FriendRepository extends CrudRepository { +} diff --git a/src/main/resources/templates/posts/index.html b/src/main/resources/templates/posts/index.html index c9baba052..1ade2e661 100644 --- a/src/main/resources/templates/posts/index.html +++ b/src/main/resources/templates/posts/index.html @@ -9,9 +9,11 @@
- - Profile Image + + Profile Image +

test this

+