dockerfile, other bs
This commit is contained in:
@@ -13,10 +13,12 @@ import io.github.lumijiez.example.models.Product;
|
||||
import io.github.lumijiez.core.logging.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ThreadedWriter writer = new ThreadedWriter("test.txt");
|
||||
ProductDao productDao = new ProductDao();
|
||||
|
||||
ServerConfig config = new ServerConfig.Builder()
|
||||
@@ -31,6 +33,12 @@ public class Main {
|
||||
chain.next(req, res);
|
||||
});
|
||||
|
||||
server.POST("/write/:text", (req, res) -> {
|
||||
Logger.info("PATH", req.getPathParam("text"));
|
||||
writer.writeToFile(req.getPathParam("text"));
|
||||
res.sendResponse(HttpStatus.OK, "OK");
|
||||
});
|
||||
|
||||
server.GET("/test/:lel/", (req, res) -> {
|
||||
Logger.info("PATH", req.getPathParam("lel"));
|
||||
Logger.info("QUERY", req.getQueryParam("lol"));
|
||||
@@ -78,9 +86,10 @@ public class Main {
|
||||
res.sendJson(HttpStatus.OK, product);
|
||||
});
|
||||
|
||||
server.GET("/products", (req, res) -> {
|
||||
Product product = productDao.getProductById(5);
|
||||
res.sendResponse(HttpStatus.OK, product.toString());
|
||||
server.GET("/products/:page/", (req, res) -> {
|
||||
Logger.info("PATH", req.getPathParam("page"));
|
||||
List<Product> products = productDao.getProductsByPage(Integer.parseInt(req.getPathParam("page")), 5);
|
||||
res.sendResponse(HttpStatus.OK, products.toString());
|
||||
});
|
||||
|
||||
WebSocketServer wsServer = new WebSocketServer(8081);
|
||||
|
||||
50
src/main/java/io/github/lumijiez/example/ThreadedWriter.java
Normal file
50
src/main/java/io/github/lumijiez/example/ThreadedWriter.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package io.github.lumijiez.example;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class ThreadedWriter {
|
||||
private final String filePath;
|
||||
private final Lock lock;
|
||||
|
||||
public ThreadedWriter(String fileName) {
|
||||
String resourcesPath = System.getProperty("user.dir") + "/resources";
|
||||
File resourcesDir = new File(resourcesPath);
|
||||
if (!resourcesDir.exists()) {
|
||||
resourcesDir.mkdirs();
|
||||
}
|
||||
this.filePath = resourcesPath + "/" + fileName;
|
||||
this.lock = new ReentrantLock();
|
||||
createFileIfNotExist();
|
||||
}
|
||||
|
||||
private void createFileIfNotExist() {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
Files.createDirectories(Paths.get(file.getParent()));
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to create file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToFile(String content) {
|
||||
lock.lock();
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
|
||||
writer.write(content);
|
||||
writer.newLine();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error writing to file: " + e.getMessage());
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import io.github.lumijiez.example.models.Product;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -58,7 +59,6 @@ public class ProductDao {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void deleteProduct(int id) {
|
||||
Transaction transaction = null;
|
||||
try (Session session = new Configuration().configure().buildSessionFactory().openSession()) {
|
||||
@@ -76,4 +76,18 @@ public class ProductDao {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Product> getProductsByPage(int pageNumber, int pageSize) {
|
||||
try (Session session = new Configuration().configure().buildSessionFactory().openSession()) {
|
||||
Query<Product> query = session.createQuery("from Product", Product.class);
|
||||
|
||||
query.setFirstResult((pageNumber - 1) * pageSize);
|
||||
query.setMaxResults(pageSize);
|
||||
|
||||
return query.getResultList();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user