added find all public

This commit is contained in:
luxic
2025-09-04 11:05:15 +07:00
parent 91394a3590
commit 653cd71c95
7 changed files with 49 additions and 24 deletions

View File

@@ -20,7 +20,7 @@ public class PasteController {
@GetMapping(value = "/findAll") @GetMapping(value = "/findAll")
public ResponseEntity<List<Paste>> findAll() { public ResponseEntity<List<Paste>> findAll() {
return ResponseEntity.ok(service.findAll()); return ResponseEntity.ok(service.findAllPublic());
} }
@PostMapping(value = "/create") @PostMapping(value = "/create")

View File

@@ -4,7 +4,10 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.*; import lombok.*;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Entity @Entity
@@ -26,4 +29,20 @@ public class Paste {
private Boolean isPrivate; private Boolean isPrivate;
private Boolean isDeleted; private Boolean isDeleted;
public static RowMapper<Paste> rowMapper() {
return (rs, rn) -> {
return Paste.builder()
.id(rs.getString("id"))
.content(rs.getString("content"))
.title(rs.getString("title"))
.expireAt(rs.getTimestamp("expire_at") == null ? null : rs.getTimestamp("expire_at").toLocalDateTime())
.created(rs.getTimestamp("created") == null ? null : rs.getTimestamp("created").toLocalDateTime())
.isPrivate(rs.getBoolean("is_private"))
.title(rs.getString("title"))
.updated(rs.getTimestamp("updated") == null ? null : rs.getTimestamp("updated").toLocalDateTime())
.isDeleted(rs.getBoolean("is_deleted"))
.build();
};
}
} }

View File

@@ -11,6 +11,8 @@ public interface PasteService {
List<Paste> findAll(); List<Paste> findAll();
List<Paste> findAllPublic();
Paste save(PasteCreateRequest pasteCreateRequest); Paste save(PasteCreateRequest pasteCreateRequest);
void delete(String id); void delete(String id);

View File

@@ -5,6 +5,8 @@ import id.my.luxic.pastebin.repository.PasteRepository;
import id.my.luxic.pastebin.request.PasteCreateRequest; import id.my.luxic.pastebin.request.PasteCreateRequest;
import id.my.luxic.pastebin.service.PasteService; import id.my.luxic.pastebin.service.PasteService;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.springframework.data.domain.Example;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -15,13 +17,15 @@ import java.util.stream.Stream;
@Service @Service
public class PasteServiceImpl implements PasteService { public class PasteServiceImpl implements PasteService {
private final String BASE64_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; private final String BASE64_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
private final int BASE64_LENGTH = 64; private final int BASE64_LENGTH = 64;
private final PasteRepository repository; private final PasteRepository repository;
private final JdbcTemplate jdbcTemplate;
public PasteServiceImpl(PasteRepository repository) { public PasteServiceImpl(PasteRepository repository, JdbcTemplate jdbcTemplate) {
this.repository = repository; this.repository = repository;
this.jdbcTemplate = jdbcTemplate;
} }
private String generateId(int length) { private String generateId(int length) {
@@ -49,6 +53,13 @@ public class PasteServiceImpl implements PasteService {
return repository.findAll(); return repository.findAll();
} }
@Override
public List<Paste> findAllPublic() {
// why did I use jdbc template ;-;
String sql = "select * from paste where (is_deleted is null or is_deleted = false) and is_private = false order by created desc";
return jdbcTemplate.query(sql, Paste.rowMapper());
}
@Override @Override
public Paste save(PasteCreateRequest pasteCreateRequest) { public Paste save(PasteCreateRequest pasteCreateRequest) {

View File

@@ -4,27 +4,17 @@
... ...
</head> </head>
<body> <body>
<div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header"> <div class="navbar" th:fragment="header">
<div class="container"> <div class="navbar">
<div class="navbar-header"> <ul class="nav">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">My project</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li th:classappend="${module == 'index' ? 'active' : ''}"> <li th:classappend="${module == 'index' ? 'active' : ''}">
<a href="#" th:href="@{/}">Index</a> <a href="#" th:href="@{/}">Index</a>
</li> </li>
<li th:classappend="${module == 'paste' ? 'active' : ''}"> <li th:classappend="${module == 'paste' ? 'active' : ''}">
<a href="#" th:href="@{/paste/1wMoxOx8it}">Paste</a> <a href="#" th:href="@{/paste/uBFC4RG02y}">Paste</a>
</li> </li>
</ul> </ul>
</div> </div>
</div>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -2,8 +2,10 @@
<html xmlns:th="http://www.thymeleaf.org" lang="en"> <html xmlns:th="http://www.thymeleaf.org" lang="en">
<head> <head>
<title>Thymeleaf Example</title> <title>Thymeleaf Example</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head> </head>
<body> <body>
<div th:replace="fragments/header :: header">
<h1>Welcome to Thymeleaf!</h1> <h1>Welcome to Thymeleaf!</h1>
<p th:text="${message}">This is a message</p> <p th:text="${message}">This is a message</p>
</body> </body>

View File

@@ -2,12 +2,13 @@
<html xmlns:th="http://www.thymeleaf.org" lang="en"> <html xmlns:th="http://www.thymeleaf.org" lang="en">
<head> <head>
<title>Thymeleaf Example</title> <title>Thymeleaf Example</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head> </head>
<body> <body>
<div th:replace="fragments/header :: header"> <div th:replace="fragments/header :: header">
</div> </div>
<h1>Welcome to Thymeleaf!</h1> <h1>Welcome to Thymeleaf!</h1>
<p th:text="${paste.content}">This is a message</p> <textarea th:text="${paste.content}"></textarea>
</body> </body>
</html> </html>