added find all public
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
@@ -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();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|
||||||
|
|||||||
@@ -4,26 +4,16 @@
|
|||||||
...
|
...
|
||||||
</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">
|
<li th:classappend="${module == 'index' ? 'active' : ''}">
|
||||||
<span class="icon-bar"></span>
|
<a href="#" th:href="@{/}">Index</a>
|
||||||
<span class="icon-bar"></span>
|
</li>
|
||||||
<span class="icon-bar"></span>
|
<li th:classappend="${module == 'paste' ? 'active' : ''}">
|
||||||
</button>
|
<a href="#" th:href="@{/paste/uBFC4RG02y}">Paste</a>
|
||||||
<a class="navbar-brand" href="#">My project</a>
|
</li>
|
||||||
</div>
|
</ul>
|
||||||
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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>
|
||||||
Reference in New Issue
Block a user