From 578b4c5011b49e42e0c7667b35f1ffe99906d229 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 25 Nov 2020 22:16:31 +0100 Subject: [PATCH] Importing data should be fine now Signed-off-by: Louis Vallat --- .../vallat/louis/commands/Inscription.java | 22 +++++++++++++++---- .../louis/managers/database/EventManager.java | 8 ++++--- .../managers/database/StudentManager.java | 19 ++++++++++++++++ .../louis/managers/database/dao/Student.java | 9 ++++---- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/xyz/vallat/louis/commands/Inscription.java b/app/src/main/java/xyz/vallat/louis/commands/Inscription.java index c913c9c..c549d03 100644 --- a/app/src/main/java/xyz/vallat/louis/commands/Inscription.java +++ b/app/src/main/java/xyz/vallat/louis/commands/Inscription.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.managers.calendar.CalendarManager; import xyz.vallat.louis.managers.database.EventManager; +import xyz.vallat.louis.managers.database.StudentManager; import xyz.vallat.louis.managers.database.dao.Student; import java.util.HashMap; @@ -28,19 +29,32 @@ public class Inscription extends Command { String[] args = event.getMessage().getContent().split(" "); if (args.length < 2 || !StringUtils.isNumeric(args[1])) return event.getMessage().getChannel().flatMap(channel -> channel.createMessage("Error: " + getUsage())).then(); + Student studentFromDatabase = StudentManager.getStudentFromDatabase(Integer.parseInt(args[1])); + if (studentFromDatabase != null) return alreadyImported(event, studentFromDatabase); return event.getMessage().getChannel().flatMap(messageChannel -> { Snowflake snowflake = event.getMessage().getAuthor().isEmpty() ? null : event.getMessage().getAuthor().get().getId(); - Student student = new Student(snowflake, Integer.parseInt(args[1])); + Student student = new Student(snowflake == null ? null : snowflake.asString(), Integer.parseInt(args[1])); List events = CalendarManager.getEventsFromResource(Integer.parseInt(args[1])); Map> studentListMap = new HashMap<>(); studentListMap.put(student, events); int importedEvents = EventManager.importEvents(studentListMap); if (importedEvents == 0) return messageChannel.createMessage( "On dirait qu'il y a eu une erreur lors de l'importation. Es-tu sûr que c'est bien ton identifiant ADE ?"); - else return messageChannel.createMessage("Hey " + - (student.getSnowflake() == null ? "" : "<@!" + student.getSnowflake().asString() + "> ") - + " tout est bon ! Je surveille maintenant tes " + importedEvents + " prochains évènements !"); + else return messageChannel.createMessage("Hey, " + + (student.getSnowflake() == null ? "" : "<@!" + student.getSnowflake() + ">, ") + + "tout est bon pour moi ! Je surveille maintenant tes " + importedEvents + " prochains évènements !"); }).then().onErrorResume(throwable -> fatalError(event, throwable)); } + + private Mono alreadyImported(MessageCreateEvent event, Student student) { + StringBuilder s = new StringBuilder("Hey"); + if (event.getMessage().getAuthor().isPresent()) + s.append(" <@!").append(event.getMessage().getAuthor().get().getId().asString()).append("> "); + s.append(" ! Cet identifiant est déjà dans ma base de données !"); + if (event.getMessage().getAuthor().isPresent() && + !student.getSnowflake().equals(event.getMessage().getAuthor().get().getId().asString())) + s.append(" <@!").append(student.getSnowflake()).append("> a déjà cet identifiant !"); + return event.getMessage().getChannel().flatMap(channel -> channel.createMessage(s.toString())).then(); + } } diff --git a/app/src/main/java/xyz/vallat/louis/managers/database/EventManager.java b/app/src/main/java/xyz/vallat/louis/managers/database/EventManager.java index c43898c..a0fbfea 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/database/EventManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/database/EventManager.java @@ -27,7 +27,7 @@ public final class EventManager { for (Map.Entry> e : events.entrySet()) { if (e.getValue().isEmpty()) continue; e.getKey().setId(StudentManager - .addStudent(e.getKey().getSnowflake().asString(), e.getKey().getAde(), connection)); + .addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection)); for (VEvent event : e.getValue()) if (importEvent(e.getKey(), event, connection) != 0) imported_events++; } @@ -51,8 +51,10 @@ public final class EventManager { Instant end = event.getDateEnd().getValue().toInstant(); stmt.setInt(1, student.getId()); stmt.setString(2, event.getSummary().getValue()); - stmt.setTimestamp(3, start == null ? null : new Timestamp(start.toEpochMilli()), event.getDateStart().getValue().getRawComponents().); - stmt.setTimestamp(4, end == null ? null : new Timestamp(end.toEpochMilli()), CalendarManager.TZ_UTC); + stmt.setTimestamp(3, + start == null ? null : new Timestamp(start.toEpochMilli()), CalendarManager.TZ_UTC); + stmt.setTimestamp(4, + end == null ? null : new Timestamp(end.toEpochMilli()), CalendarManager.TZ_UTC); stmt.executeUpdate(); stmt.getGeneratedKeys().next(); assert start != null; diff --git a/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java b/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java index 962df19..991185d 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/database/StudentManager.java @@ -2,6 +2,7 @@ package xyz.vallat.louis.managers.database; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.vallat.louis.managers.database.dao.Student; import java.sql.Connection; import java.sql.PreparedStatement; @@ -42,4 +43,22 @@ public final class StudentManager { return stmt.getGeneratedKeys().getInt(1); } } + + public static Student getStudentFromDatabase(int ade) { + try (Connection connection = DBManager.getConnection()) { + String sql = "SELECT id, snowflake FROM students WHERE ade_resource = ?;"; + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setInt(1, ade); + stmt.execute(); + if (stmt.getResultSet().next()) { + int id = stmt.getResultSet().getInt("id"); + String snowflake = stmt.getResultSet().getString("snowflake"); + return new Student(snowflake, ade).setId(id); + } + } + } catch (SQLException e) { + logger.error("An error occurred while getting students:", e); + } + return null; + } } diff --git a/app/src/main/java/xyz/vallat/louis/managers/database/dao/Student.java b/app/src/main/java/xyz/vallat/louis/managers/database/dao/Student.java index 71867e6..b058077 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/database/dao/Student.java +++ b/app/src/main/java/xyz/vallat/louis/managers/database/dao/Student.java @@ -5,16 +5,16 @@ import discord4j.common.util.Snowflake; public class Student { private int id; - private final Snowflake snowflake; + private final String snowflake; private final int ade; - public Student(Snowflake snowflake, int ade) { + public Student(String snowflake, int ade) { this.snowflake = snowflake; this.ade = ade; this.id = 0; } - public Snowflake getSnowflake() { + public String getSnowflake() { return snowflake; } @@ -26,7 +26,8 @@ public class Student { return id; } - public void setId(int id) { + public Student setId(int id) { this.id = id; + return this; } }