Skip to content

Commit 21d9a42

Browse files
committed
Make the FormField into a record
1 parent 61d7e36 commit 21d9a42

File tree

5 files changed

+11
-92
lines changed

5 files changed

+11
-92
lines changed

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/FormInteractionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private static MessageEmbed createSubmissionEmbed(FormData form, List<ModalMappi
268268
ModalMapping mapping = values.get(i);
269269
FormField field = form.getFields().get(i);
270270
String value = mapping.getAsString();
271-
builder.addField(field.getLabel(), value == null ? "*Empty*" : "```\n" + value + "\n```", false);
271+
builder.addField(field.label(), value == null ? "*Empty*" : "```\n" + value + "\n```", false);
272272
}
273273

274274
return builder.build();

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AddFieldFormSubcommand.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public AddFieldFormSubcommand(FormsRepository formsRepo) {
4040
.addOption(OptionType.BOOLEAN, "required",
4141
"Whether or not the user has to input data in this field. Default: false")
4242
.addOption(OptionType.STRING, "style", "Input style. Default: SHORT", false, true)
43-
.addOption(OptionType.STRING, "value", "Initial field value")
44-
.addOption(OptionType.INTEGER, "index", "Index to insert the field at"));
43+
.addOption(OptionType.STRING, "value", "Initial field value"));
4544
}
4645

4746
@Override
@@ -60,12 +59,6 @@ public void execute(SlashCommandInteractionEvent event) {
6059
return;
6160
}
6261

63-
int index = event.getOption("index", -1, OptionMapping::getAsInt);
64-
if (index < -1 || index >= form.getFields().size()) {
65-
event.getHook().sendMessage("Field index out of bounds").queue();
66-
return;
67-
}
68-
6962
formsRepo.addField(form, createFormFieldFromEvent(event));
7063
event.getHook().sendMessage("Added a new field to the form.").queue();
7164
}

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/RemoveFieldFormSubcommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void execute(SlashCommandInteractionEvent event) {
6161

6262
formsRepo.removeField(form, index);
6363

64-
event.getHook().sendMessage("Removed field `" + form.getFields().get(index).getLabel() + "` from the form.")
64+
event.getHook().sendMessage("Removed field `" + form.getFields().get(index).label() + "` from the form.")
6565
.queue();
6666
}
6767

@@ -79,7 +79,7 @@ public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCo
7979
List<Choice> choices = new ArrayList<>();
8080
List<FormField> fields = form.get().getFields();
8181
for (int i = 0; i < fields.size(); i++) {
82-
choices.add(new Choice(fields.get(i).getLabel(), i));
82+
choices.add(new Choice(fields.get(i).label(), i));
8383
}
8484
event.replyChoices(choices).queue();
8585
return;

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/dao/FormsRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void addField(FormData form, FormField field) {
4343
jdbcTemplate.update(
4444
"INSERT INTO FORM_FIELDS (FORM_ID, LABEL, MIN, MAX, PLACEHOLDER, REQUIRED, \"style\", INITIAL) "
4545
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
46-
form.getId(), field.getLabel(), field.getMin(), field.getMax(), field.getPlaceholder(),
47-
field.isRequired(), field.getStyle().name(), field.getValue());
46+
form.getId(), field.label(), field.min(), field.max(), field.placeholder(), field.required(),
47+
field.style().name(), field.value());
4848
}
4949

5050
/**
@@ -241,7 +241,7 @@ public void addSubmission(User user, FormData form, Message message) {
241241
public void removeField(FormData form, int index) {
242242
List<FormField> fields = form.getFields();
243243
if (index < 0 || index >= fields.size()) return;
244-
jdbcTemplate.update("delete from `form_fields` where `id` = ?", fields.get(index).getId());
244+
jdbcTemplate.update("delete from `form_fields` where `id` = ?", fields.get(index).id());
245245
}
246246

247247
/**

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/model/FormField.java

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,8 @@
66
/**
77
* Represents a form field.
88
*/
9-
public class FormField {
10-
private final long id;
11-
private final String label;
12-
private final int max;
13-
private final int min;
14-
private final String placeholder;
15-
private final boolean required;
16-
private final TextInputStyle style;
17-
private final String value;
18-
19-
/**
20-
* Main constructor.
21-
*
22-
* @param label text field lable.
23-
* @param max maximum characters allowed.
24-
* @param min minimum characters allowed.
25-
* @param placeholder text field placeholder.
26-
* @param required whether or not this field is required.
27-
* @param style text field style. One of {@link TextInputStyle} values.
28-
* Case insensitive.
29-
* @param value initial value of this text field.
30-
* @param id unique ID of this field.
31-
*/
32-
public FormField(String label, int max, int min, String placeholder, boolean required, TextInputStyle style,
33-
String value, long id) {
34-
this.id = id;
35-
this.label = label;
36-
this.max = max;
37-
this.min = min;
38-
this.placeholder = placeholder;
39-
this.required = required;
40-
this.style = style;
41-
this.value = value;
42-
}
9+
public record FormField(String label, int max, int min, String placeholder, boolean required,
10+
TextInputStyle style, String value, long id) {
4311

4412
/**
4513
* Create a standalone text input from this field.
@@ -48,50 +16,8 @@ public FormField(String label, int max, int min, String placeholder, boolean req
4816
* @return text input ready to use in a modal.
4917
*/
5018
public TextInput createTextInput(String id) {
51-
return TextInput.create(id, getLabel(), getStyle()).setRequiredRange(getMin(), getMax())
52-
.setPlaceholder(getPlaceholder()).setRequired(isRequired()).setValue(getValue()).build();
53-
}
54-
55-
/**
56-
* Get this field's unique ID.
57-
*
58-
* @return unique ID of the field.
59-
*/
60-
public long getId() {
61-
return id;
62-
}
63-
64-
public String getLabel() {
65-
return label;
66-
}
67-
68-
public int getMax() {
69-
return max <= 0 ? 64 : max;
70-
}
71-
72-
public int getMin() {
73-
return Math.max(0, min);
74-
}
75-
76-
public String getPlaceholder() {
77-
return placeholder;
78-
}
79-
80-
/**
81-
* Get the style of this field's text input.
82-
*
83-
* @return one of {@link TextInputStyle} values.
84-
*/
85-
public TextInputStyle getStyle() {
86-
return style;
87-
}
88-
89-
public String getValue() {
90-
return value;
91-
}
92-
93-
public boolean isRequired() {
94-
return required;
19+
return TextInput.create(id, label(), style()).setRequiredRange(min(), max()).setPlaceholder(placeholder())
20+
.setRequired(required()).setValue(value()).build();
9521
}
9622

9723
@Override

0 commit comments

Comments
 (0)