Skip to content

Commit

Permalink
refactor: change SJMData entity key to UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyAreaBean committed Apr 26, 2024
1 parent 692d5c7 commit e0a2af5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
20 changes: 8 additions & 12 deletions src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,15 @@ public void collectionCreate(BukkitCommandActor actor, @Named("collection") Stri
public void collectionRemove(BukkitCommandActor actor, @Named("collection") String collectionName) {
actor.reply(Constants.PREFIX.append(text("Removing collection %s...".formatted(collectionName), NamedTextColor.YELLOW)));

SimpleJoinMessage.getInstance().getDatabase()
.deleteCollection(collectionName)
.thenAsync((throwable) -> {

if (throwable != null) {
actor.reply(Constants.PREFIX.append(text("The collection %s does not exist.".formatted(collectionName),
NamedTextColor.RED)));
return;
}
if (!SimpleJoinMessage.getInstance().getDatabase().getCollections().contains(collectionName)) {
actor.reply(Constants.PREFIX.append(text("The collection %s does not exist.".formatted(collectionName),
NamedTextColor.RED)));
return;
}

SimpleJoinMessage.getInstance().getDatabase().updateCollectionNames();
actor.reply(Constants.PREFIX.append(text("Collection %s has been successfully removed.".formatted(collectionName), NamedTextColor.GREEN)));
});
SimpleJoinMessage.getInstance().getDatabase().deleteCollection(collectionName);
SimpleJoinMessage.getInstance().getDatabase().updateCollectionNames();
actor.reply(Constants.PREFIX.append(text("Collection %s has been successfully removed.".formatted(collectionName), NamedTextColor.GREEN)));
}

@Subcommand({"collection update"})
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/cc/happyareabean/sjm/database/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import static cc.happyareabean.sjm.utils.Util.BASE64_ENCODER;

@Getter
public class DatabaseManager {

private final Repository<String, SJMData> data;
private final Repository<UUID, SJMData> data;
private final List<String> collections = new ArrayList<>();

public DatabaseManager() {
Expand Down Expand Up @@ -136,9 +137,19 @@ public CompletableFuture<Void> updateCollection(String name, String content) {
});
}

public Promise.AsyncEmptyResult deleteCollection(String name) {
return SimpleJoinMessage.getInstance().getDatabase().getData()
.deleteAsync(name);
public void deleteCollection(String name) {
SimpleJoinMessage.getInstance().getDatabase().getData()
.findOneAsync(NQuery.predicate("$.collection_name == " + name))
.thenAsync(optionalSJMData -> {

if (optionalSJMData.isEmpty()) {
return;
}

SJMData sjmData = optionalSJMData.get();

data.delete(sjmData);
});
}

public CompletableFuture<Optional<SJMData>> getRawContentFuture(String collectionName) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/cc/happyareabean/sjm/database/SJMData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@Data
@NTable(name = "sjm_data")
@NoArgsConstructor
@Builder
public class SJMData extends NEntity<String> {
public class SJMData extends NEntity<UUID> {

@JsonProperty("content")
private String content;
Expand All @@ -24,7 +26,7 @@ public class SJMData extends NEntity<String> {
public String collectionName;

public SJMData(String collectionName, String content) {
super(collectionName);
super(UUID.randomUUID());
this.collectionName = collectionName;
this.content = content;
}
Expand Down

0 comments on commit e0a2af5

Please sign in to comment.