test jenkins
This commit is contained in:
11
Jenkinsfile
vendored
Normal file
11
Jenkinsfile
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
pipeline {
|
||||
agent any
|
||||
stages {
|
||||
stage('Build') {
|
||||
steps {
|
||||
sh 'mvn -B -DskipTests clean package'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package id.my.luxic.pastebin.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class DatasourceConfig {
|
||||
|
||||
@Value("${spring.datasource.url}")
|
||||
private String url;
|
||||
|
||||
@Value("${spring.datasource.username}")
|
||||
private String username;
|
||||
|
||||
@Value("${spring.datasource.password}")
|
||||
private String password;
|
||||
|
||||
@Bean
|
||||
DataSource dataSource() {
|
||||
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
|
||||
dataSourceBuilder.url(url);
|
||||
dataSourceBuilder.username(username);
|
||||
dataSourceBuilder.password(password);
|
||||
return dataSourceBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package id.my.luxic.pastebin.controller;
|
||||
|
||||
import id.my.luxic.pastebin.model.Paste;
|
||||
import id.my.luxic.pastebin.service.PasteService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
private final PasteService service;
|
||||
|
||||
public HomeController(PasteService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String home(Model model) {
|
||||
model.addAttribute("message", "Hello, Thymeleaf!");
|
||||
return "index";
|
||||
}
|
||||
|
||||
@GetMapping("/paste/{id}")
|
||||
public String paste(Model model, @PathVariable String id) {
|
||||
Paste paste = service.findById(id);
|
||||
model.addAttribute("paste", paste);
|
||||
return "paste";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package id.my.luxic.pastebin.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import id.my.luxic.pastebin.model.Paste;
|
||||
import id.my.luxic.pastebin.request.PasteCreateRequest;
|
||||
import id.my.luxic.pastebin.service.PasteService;
|
||||
@@ -28,6 +29,11 @@ public class PasteController {
|
||||
return ResponseEntity.ok(service.save(pasteCreateRequest));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/createJdbc")
|
||||
public ResponseEntity<Paste> createJdbc(@RequestBody PasteCreateRequest pasteCreateRequest) {
|
||||
return ResponseEntity.ok(service.saveJdbc(pasteCreateRequest));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Paste> findById(@PathVariable String id) {
|
||||
return ResponseEntity.ok(service.findById(id));
|
||||
|
||||
@@ -6,8 +6,6 @@ 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
|
||||
@@ -30,8 +28,7 @@ public class Paste {
|
||||
private Boolean isDeleted;
|
||||
|
||||
public static RowMapper<Paste> rowMapper() {
|
||||
return (rs, rn) -> {
|
||||
return Paste.builder()
|
||||
return (rs, rn) -> Paste.builder()
|
||||
.id(rs.getString("id"))
|
||||
.content(rs.getString("content"))
|
||||
.title(rs.getString("title"))
|
||||
@@ -42,7 +39,6 @@ public class Paste {
|
||||
.updated(rs.getTimestamp("updated") == null ? null : rs.getTimestamp("updated").toLocalDateTime())
|
||||
.isDeleted(rs.getBoolean("is_deleted"))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,5 +3,9 @@ package id.my.luxic.pastebin.repository;
|
||||
import id.my.luxic.pastebin.model.Paste;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PasteRepository extends JpaRepository<Paste, String> {
|
||||
|
||||
public List<Paste> findAllPasteByIsDeleted(Boolean isDeleted);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ public interface PasteService {
|
||||
|
||||
Paste save(PasteCreateRequest pasteCreateRequest);
|
||||
|
||||
Paste saveJdbc(PasteCreateRequest pasteCreateRequest);
|
||||
|
||||
void delete(String id);
|
||||
|
||||
}
|
||||
|
||||
@@ -5,10 +5,13 @@ 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 javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collector;
|
||||
@@ -22,10 +25,13 @@ public class PasteServiceImpl implements PasteService {
|
||||
|
||||
private final PasteRepository repository;
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final DataSource datasource;
|
||||
|
||||
public PasteServiceImpl(PasteRepository repository, JdbcTemplate jdbcTemplate) {
|
||||
public PasteServiceImpl(PasteRepository repository, JdbcTemplate jdbcTemplate, DataSource datasource) {
|
||||
this.repository = repository;
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.datasource = datasource;
|
||||
|
||||
}
|
||||
|
||||
private String generateId(int length) {
|
||||
@@ -55,7 +61,6 @@ public class PasteServiceImpl implements PasteService {
|
||||
|
||||
@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());
|
||||
}
|
||||
@@ -75,6 +80,44 @@ public class PasteServiceImpl implements PasteService {
|
||||
return repository.save(paste);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paste saveJdbc(PasteCreateRequest pasteCreateRequest) {
|
||||
|
||||
String sql = "insert into paste (id, title, content, is_private, expire_at, created) values (?, ?, ?, ?, ?, ?)";
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = datasource.getConnection();
|
||||
PreparedStatement statement = conn.prepareStatement(sql);
|
||||
|
||||
statement.setString(1, generateId(10));
|
||||
statement.setString(2, pasteCreateRequest.getTitle());
|
||||
statement.setString(3, pasteCreateRequest.getContent());
|
||||
statement.setBoolean(4, pasteCreateRequest.getIsPrivate());
|
||||
statement.setTimestamp(5, java.sql.Timestamp.valueOf(pasteCreateRequest.getExpiration().getTimestamp()));
|
||||
statement.setTimestamp(6, java.sql.Timestamp.valueOf(LocalDateTime.now()));
|
||||
statement.executeUpdate();
|
||||
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Paste.builder().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id) {
|
||||
repository.deleteById(id);
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
...
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
</html>
|
||||
@@ -1,12 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<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>
|
||||
</html>
|
||||
@@ -1,14 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<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>
|
||||
<textarea th:text="${paste.content}"></textarea>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user