Added rethinkdb connector
- Refactored RethinkDBService - Added FileUploadRepository
This commit is contained in:
parent
ee8255df7b
commit
02cca3909b
27
src/main/java/de/w665/sharepulse/db/RethinkDBConnector.java
Normal file
27
src/main/java/de/w665/sharepulse/db/RethinkDBConnector.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package de.w665.sharepulse.db;
|
||||||
|
|
||||||
|
import com.rethinkdb.RethinkDB;
|
||||||
|
import com.rethinkdb.net.Connection;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RethinkDBConnector {
|
||||||
|
|
||||||
|
private final RethinkDBConfig config;
|
||||||
|
@Getter
|
||||||
|
private final RethinkDB r = RethinkDB.r;
|
||||||
|
@Getter
|
||||||
|
private Connection connection;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void connectToDatabae() {
|
||||||
|
connection = r.connection().hostname(config.getHost()).port(config.getPort()).connect();
|
||||||
|
log.info("Connected to RethinkDB at " + config.getHost() + ":" + config.getPort() + " on database " + config.getDatabase());
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,9 @@ import com.rethinkdb.net.Connection;
|
|||||||
import com.rethinkdb.net.Result;
|
import com.rethinkdb.net.Result;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.annotation.PreDestroy;
|
import jakarta.annotation.PreDestroy;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -16,18 +18,24 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class RethinkDBService {
|
public class RethinkDBService {
|
||||||
private static final RethinkDB r = RethinkDB.r;
|
|
||||||
private Connection connection;
|
|
||||||
private final RethinkDBConfig config;
|
private final RethinkDBConfig config;
|
||||||
|
|
||||||
public RethinkDBService(RethinkDBConfig config) {
|
private final RethinkDB r;
|
||||||
|
private final Connection connection;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public RethinkDBService(RethinkDBConfig config, RethinkDBConnector connector) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
|
|
||||||
|
// mapping to private vars for easier access
|
||||||
|
this.r = connector.getR();
|
||||||
|
this.connection = connector.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
connection = r.connection().hostname(config.getHost()).port(config.getPort()).connect();
|
|
||||||
log.info("Connected to RethinkDB at " + config.getHost() + ":" + config.getPort() + " on database " + config.getDatabase());
|
|
||||||
|
|
||||||
//rethinkdb check if database exists
|
//rethinkdb check if database exists
|
||||||
|
|
||||||
@ -45,7 +53,7 @@ public class RethinkDBService {
|
|||||||
log.info("Database 'sharepulse' already exists. No action needed.");
|
log.info("Database 'sharepulse' already exists. No action needed.");
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
// rethinkdb check if database exists
|
||||||
try {
|
try {
|
||||||
r.dbCreate(config.getDatabase()).run(connection).stream();
|
r.dbCreate(config.getDatabase()).run(connection).stream();
|
||||||
log.info("Database " + config.getDatabase() + " created");
|
log.info("Database " + config.getDatabase() + " created");
|
||||||
@ -53,9 +61,21 @@ public class RethinkDBService {
|
|||||||
log.info("Database " + config.getDatabase() + " already exists. Error: " + e.getClass().getSimpleName());
|
log.info("Database " + config.getDatabase() + " already exists. Error: " + e.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rethinkdb check if table file_uploads exists
|
||||||
|
try {
|
||||||
|
r.db(config.getDatabase()).tableCreate("file_uploads").run(connection).stream();
|
||||||
|
log.info("Table 'file_uploads' created successfully.");
|
||||||
|
} catch (ReqlOpFailedError e) {
|
||||||
|
log.info("Table 'file_uploads' already exists. No action needed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// rethinkdb check if table id_store exists
|
||||||
// Additional initialization, like ensuring the database and tables exist
|
try {
|
||||||
|
r.db(config.getDatabase()).tableCreate("id_store").run(connection).stream();
|
||||||
|
log.info("Table 'id_store' created successfully.");
|
||||||
|
} catch (ReqlOpFailedError e) {
|
||||||
|
log.info("Table 'id_store' already exists. No action needed.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
@ -64,9 +84,4 @@ public class RethinkDBService {
|
|||||||
connection.close();
|
connection.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example method to insert data into a specific table
|
|
||||||
public void insertData(String tableName, Object data) {
|
|
||||||
r.db(config.getDatabase()).table(tableName).insert(data).run(connection);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package de.w665.sharepulse.db.repo;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.rethinkdb.RethinkDB;
|
||||||
|
import com.rethinkdb.gen.ast.Filter;
|
||||||
|
import com.rethinkdb.net.Connection;
|
||||||
|
import de.w665.sharepulse.db.RethinkDBConnector;
|
||||||
|
import de.w665.sharepulse.db.RethinkDBService;
|
||||||
|
import de.w665.sharepulse.model.FileUpload;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileUploadRepository {
|
||||||
|
|
||||||
|
private final RethinkDBService rethinkDBService;
|
||||||
|
private final RethinkDB r;
|
||||||
|
private final Connection connection;
|
||||||
|
private final Gson gson;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public FileUploadRepository(RethinkDBService rethinkDBService, RethinkDBConnector connector) {
|
||||||
|
this.rethinkDBService = rethinkDBService;
|
||||||
|
this.r = connector.getR();
|
||||||
|
this.connection = connector.getConnection();
|
||||||
|
|
||||||
|
this.gson = new GsonBuilder()
|
||||||
|
.setDateFormat("dd-MM-yyyy HH:mm:ss") // date field formatting
|
||||||
|
.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertFileUpload(FileUpload fileUpload) {
|
||||||
|
// Serialize FileUpload object to JSON
|
||||||
|
String json = gson.toJson(fileUpload);
|
||||||
|
|
||||||
|
// Convert JSON string back to Map
|
||||||
|
Type type = new TypeToken<Map<String, Object>>(){}.getType();
|
||||||
|
Map<String, Object> map = gson.fromJson(json, type);
|
||||||
|
|
||||||
|
// Insert the Map into RethinkDB
|
||||||
|
r.db("sharepulse").table("file_uploads").insert(map).run(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileUpload retrieveFileUploadByFileId(String fileId) {
|
||||||
|
return r.db("sharepulse").table("file_uploads")
|
||||||
|
.filter(r.hashMap("fileId", fileId))
|
||||||
|
.run(connection, FileUpload.class)
|
||||||
|
.next();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user