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

posts appear by newest first #6

Merged
merged 1 commit into from
Nov 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
Expand All @@ -18,14 +20,16 @@ public class PostsController {

@GetMapping("/posts")
public String index(Model model) {
Iterable<Post> posts = repository.findAll();
Iterable<Post> posts = repository.findAllByOrderByTimestampDesc();
model.addAttribute("posts", posts);
model.addAttribute("post", new Post());
return "posts/index";
}

@PostMapping("/posts")
public RedirectView create(@ModelAttribute Post post) {
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
post.setTimestamp(Timestamp.valueOf(timeStamp));
repository.save(post);
return new RedirectView("/posts");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long> {
import java.util.List;

public interface PostRepository extends CrudRepository<Post, Long> {
public List<Post> findAllByOrderByTimestampDesc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
update
posts
set
timestamp = '2023-11-14 11:13:24',
user_id = 3
where
content = 'Second post';


update
posts
set
timestamp = '2023-11-15 11:13:24',
user_id = 10
where
content = 'fourth post';


update
posts
set
timestamp = '2023-11-16 11:13:24',
user_id = 11
where
content = 'third post';


update
posts
set
timestamp = '2023-11-10 11:13:24',
user_id = 12
where
content = 'sixth post';


update
posts
set
timestamp = '2023-11-09 11:13:24',
user_id = 14
where
content = 'fifth post';

2 changes: 1 addition & 1 deletion src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
<div class="form-actions">
<button type="submit" class="btn">Log in</button>
<button type="submit" id="submit" class="btn">Log in</button>
</div>
</fieldset>
</form>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/posts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1>Posts</h1>

<form action="#" th:action="@{/posts}" th:object="${post}" method="post">
<p>Content: <input type="text" th:field="*{content}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
<p><input type="submit" id="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<ul th:each="post: ${posts}">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.makersacademy.aceboook.controller;

import com.github.javafaker.Faker;
import com.makersacademy.acebook.Application;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class PostControllerTest {


WebDriver driver;
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 testNewPostIsAtTheTopOfList() {
driver.get("http://localhost:8080/login");
driver.findElement(By.id("username")).sendKeys("user_1");
driver.findElement(By.id("password")).sendKeys("12345");
driver.findElement(By.id("submit")).click();
driver.findElement(By.id("content")).sendKeys("New Post");
driver.findElement(By.id("submit")).click();

WebElement ul = driver.findElement(By.tagName("ul"));
List<WebElement> postList = ul.findElements(By.tagName("li"));

String newPost = postList.get(0).getText();

Assert.assertEquals("New Post", newPost);


}
}