44import java .sql .PreparedStatement ;
55import java .sql .ResultSet ;
66import java .sql .SQLException ;
7- import java .util .ArrayList ;
87import java .util .Collections ;
98import java .util .HashMap ;
109import java .util .List ;
1817import org .springframework .jdbc .core .RowMapper ;
1918import org .springframework .stereotype .Repository ;
2019
21- import com .google .gson .Gson ;
22- import com .google .gson .JsonElement ;
23- import com .google .gson .JsonParser ;
24-
2520import lombok .NonNull ;
2621import lombok .RequiredArgsConstructor ;
2722import net .discordjug .javabot .systems .staff_commands .forms .model .FormData ;
3025import net .dv8tion .jda .api .entities .Message ;
3126import net .dv8tion .jda .api .entities .User ;
3227import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
28+ import net .dv8tion .jda .api .interactions .components .text .TextInputStyle ;
3329
3430/**
3531 * Dao class that represents the FORMS database.
3632 */
3733@ RequiredArgsConstructor
3834@ Repository
3935public class FormsRepository {
40- private final Gson gson ;
4136 private final JdbcTemplate jdbcTemplate ;
4237
4338 /**
4439 * Add a field to a form.
4540 *
4641 * @param form form to add field to
4742 * @param field field to add
48- * @param index insertion index, or -1 to append
4943 */
50- public void addField (FormData form , FormField field , int index ) {
51- Objects .requireNonNull (field );
52- List <FormField > fields = new ArrayList <>(form .getFields ());
53- if (index != -1 ) {
54- fields .add (index , field );
55- } else {
56- fields .add (field );
57- }
58- updateFormData (form , fields );
44+ public void addField (FormData form , FormField field ) {
45+ jdbcTemplate .update (
46+ "INSERT INTO FORM_FIELDS (FORM_ID, LABEL, MIN, MAX, PLACEHOLDER, REQUIRED, \" style\" , INITIAL) "
47+ + "VALUES(?, ?, ?, ?, ?, ?, ?, ?)" ,
48+ form .getId (), field .getLabel (), field .getMin (), field .getMax (), field .getPlaceholder (),
49+ field .isRequired (), field .getStyle ().name (), field .getValue ());
5950 }
6051
6152 /**
@@ -133,7 +124,7 @@ public void detachForm(FormData form) {
133124 * @return A list of forms
134125 */
135126 public List <FormData > getAllForms () {
136- return jdbcTemplate .query ("select * from `forms`" , (rs , rowNum ) -> read (rs ));
127+ return jdbcTemplate .query ("select * from `forms`" , (rs , rowNum ) -> read (rs , readFormFields ( rowNum ) ));
137128 }
138129
139130 /**
@@ -147,7 +138,7 @@ public List<FormData> getAllForms(boolean closed) {
147138 PreparedStatement statement = con .prepareStatement ("select * from `forms` where `closed` = ?" );
148139 statement .setBoolean (1 , closed );
149140 return statement ;
150- }, (rs , rowNum ) -> read (rs ));
141+ }, (rs , rowNum ) -> read (rs , readFormFields ( rowNum ) ));
151142 }
152143
153144 /**
@@ -176,7 +167,7 @@ public Map<FormUser, Integer> getAllSubmissions(FormData form) {
176167 public Optional <FormData > getForm (long formId ) {
177168 try {
178169 return Optional .of (jdbcTemplate .queryForObject ("select * from `forms` where `form_id` = ?" ,
179- (RowMapper <FormData >) (rs , rowNum ) -> read (rs ), formId ));
170+ (RowMapper <FormData >) (rs , rowNum ) -> read (rs , readFormFields ( formId ) ), formId ));
180171 } catch (EmptyResultDataAccessException e ) {
181172 return Optional .empty ();
182173 }
@@ -224,16 +215,14 @@ public void insertForm(@NonNull FormData data) {
224215 @ Override
225216 public PreparedStatement createPreparedStatement (Connection con ) throws SQLException {
226217 PreparedStatement statement = con .prepareStatement (
227- "merge into `forms` (form_id, form_data, title, submit_message, submit_channel, message_id, message_channel, expiration, onetime) values (?, ?, ?, ?, ?, ?, ? ,?, ?)" );
228- statement .setLong (1 , data .getId ());
229- statement .setString (2 , gson .toJson (data .getFields ()));
230- statement .setString (3 , data .getTitle ());
231- statement .setString (4 , data .getSubmitMessage ());
232- statement .setString (5 , data .getSubmitChannel ());
233- statement .setString (6 , data .getMessageId ());
234- statement .setString (7 , data .getMessageChannel ());
235- statement .setLong (8 , data .getExpiration ());
236- statement .setBoolean (9 , data .isOnetime ());
218+ "insert into `forms` (title, submit_message, submit_channel, message_id, message_channel, expiration, onetime) values (?, ?, ?, ?, ?, ?, ?)" );
219+ statement .setString (1 , data .getTitle ());
220+ statement .setString (2 , data .getSubmitMessage ());
221+ statement .setString (3 , data .getSubmitChannel ());
222+ statement .setString (4 , data .getMessageId ());
223+ statement .setString (5 , data .getMessageChannel ());
224+ statement .setLong (6 , data .getExpiration ());
225+ statement .setBoolean (7 , data .isOnetime ());
237226 return statement ;
238227 }
239228 });
@@ -266,10 +255,9 @@ public void logSubmission(User user, FormData form) {
266255 * @param index index of the field to remove
267256 */
268257 public void removeField (FormData form , int index ) {
269- List <FormField > fields = new ArrayList <>( form .getFields () );
258+ List <FormField > fields = form .getFields ();
270259 if (index < 0 || index >= fields .size ()) return ;
271- fields .remove (index );
272- updateFormData (form , fields );
260+ jdbcTemplate .update ("delete from `form_fields` where `id` = ?" , fields .get (index ).getId ());
273261 }
274262
275263 /**
@@ -302,26 +290,20 @@ public void updateForm(FormData newData) {
302290 });
303291 }
304292
305- private FormData read (ResultSet rs ) throws SQLException {
306- List <FormField > fields = new ArrayList <>();
307- for (JsonElement element : JsonParser .parseString (rs .getString ("form_data" )).getAsJsonArray ()) {
308- fields .add (gson .fromJson (element , FormField .class ));
309- }
293+ private List <FormField > readFormFields (long formId ) {
294+ return jdbcTemplate .query ("select * from `form_fields` where `form_id` = ?" , (rs , rowNum ) -> readField (rs ),
295+ formId );
296+ }
297+
298+ private static FormData read (ResultSet rs , List <FormField > fields ) throws SQLException {
310299 return new FormData (rs .getLong ("form_id" ), fields , rs .getString ("title" ), rs .getString ("submit_channel" ),
311300 rs .getString ("submit_message" ), rs .getString ("message_id" ), rs .getString ("message_channel" ),
312301 rs .getLong ("expiration" ), rs .getBoolean ("closed" ), rs .getBoolean ("onetime" ));
313302 }
314303
315- private void updateFormData (FormData form , List <FormField > fields ) {
316- Objects .requireNonNull (form );
317- Objects .requireNonNull (fields );
318- String json = gson .toJson (fields );
319- jdbcTemplate .update (con -> {
320- PreparedStatement statement = con
321- .prepareStatement ("update `forms` set `form_data` = ? where `form_id` = ?" );
322- statement .setString (1 , json );
323- statement .setLong (2 , form .getId ());
324- return statement ;
325- });
304+ private static FormField readField (ResultSet rs ) throws SQLException {
305+ return new FormField (rs .getString ("label" ), rs .getInt ("max" ), rs .getInt ("min" ), rs .getString ("placeholder" ),
306+ rs .getBoolean ("required" ), TextInputStyle .valueOf (rs .getString ("style" ).toUpperCase ()),
307+ rs .getString ("initial" ), rs .getInt ("id" ));
326308 }
327309}
0 commit comments