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")
public ResponseEntity<List<Paste>> findAll() {
return ResponseEntity.ok(service.findAll());
return ResponseEntity.ok(service.findAllPublic());
}
@PostMapping(value = "/create")

View File

@@ -4,7 +4,10 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.*;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
@Entity
@@ -26,4 +29,20 @@ public class Paste {
private Boolean isPrivate;
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> findAllPublic();
Paste save(PasteCreateRequest pasteCreateRequest);
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.service.PasteService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.data.domain.Example;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@@ -15,13 +17,15 @@ import java.util.stream.Stream;
@Service
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 PasteRepository repository;
private final JdbcTemplate jdbcTemplate;
public PasteServiceImpl(PasteRepository repository) {
public PasteServiceImpl(PasteRepository repository, JdbcTemplate jdbcTemplate) {
this.repository = repository;
this.jdbcTemplate = jdbcTemplate;
}
private String generateId(int length) {
@@ -49,6 +53,13 @@ public class PasteServiceImpl implements PasteService {
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
public Paste save(PasteCreateRequest pasteCreateRequest) {

View File

@@ -4,26 +4,16 @@
...
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header">
<div class="container">
<div class="navbar-header">
<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' : ''}">
<a href="#" th:href="@{/}">Index</a>
</li>
<li th:classappend="${module == 'paste' ? 'active' : ''}">
<a href="#" th:href="@{/paste/1wMoxOx8it}">Paste</a>
</li>
</ul>
</div>
<div class="navbar" th:fragment="header">
<div class="navbar">
<ul class="nav">
<li th:classappend="${module == 'index' ? 'active' : ''}">
<a href="#" th:href="@{/}">Index</a>
</li>
<li th:classappend="${module == 'paste' ? 'active' : ''}">
<a href="#" th:href="@{/paste/uBFC4RG02y}">Paste</a>
</li>
</ul>
</div>
</div>
</body>

View File

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

View File

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