Importing data should be fine now

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-25 22:16:31 +01:00
parent e92fdd2813
commit 578b4c5011
4 changed files with 47 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import xyz.vallat.louis.managers.calendar.CalendarManager; import xyz.vallat.louis.managers.calendar.CalendarManager;
import xyz.vallat.louis.managers.database.EventManager; import xyz.vallat.louis.managers.database.EventManager;
import xyz.vallat.louis.managers.database.StudentManager;
import xyz.vallat.louis.managers.database.dao.Student; import xyz.vallat.louis.managers.database.dao.Student;
import java.util.HashMap; import java.util.HashMap;
@ -28,19 +29,32 @@ public class Inscription extends Command {
String[] args = event.getMessage().getContent().split(" "); String[] args = event.getMessage().getContent().split(" ");
if (args.length < 2 || !StringUtils.isNumeric(args[1])) if (args.length < 2 || !StringUtils.isNumeric(args[1]))
return event.getMessage().getChannel().flatMap(channel -> channel.createMessage("Error: " + getUsage())).then(); 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 -> { return event.getMessage().getChannel().flatMap(messageChannel -> {
Snowflake snowflake = event.getMessage().getAuthor().isEmpty() ? null : Snowflake snowflake = event.getMessage().getAuthor().isEmpty() ? null :
event.getMessage().getAuthor().get().getId(); 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<VEvent> events = CalendarManager.getEventsFromResource(Integer.parseInt(args[1])); List<VEvent> events = CalendarManager.getEventsFromResource(Integer.parseInt(args[1]));
Map<Student, List<VEvent>> studentListMap = new HashMap<>(); Map<Student, List<VEvent>> studentListMap = new HashMap<>();
studentListMap.put(student, events); studentListMap.put(student, events);
int importedEvents = EventManager.importEvents(studentListMap); int importedEvents = EventManager.importEvents(studentListMap);
if (importedEvents == 0) return messageChannel.createMessage( 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 ?"); "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 " + else return messageChannel.createMessage("Hey, " +
(student.getSnowflake() == null ? "" : "<@!" + student.getSnowflake().asString() + "> ") (student.getSnowflake() == null ? "" : "<@!" + student.getSnowflake() + ">, ")
+ " tout est bon ! Je surveille maintenant tes " + importedEvents + " prochains évènements !"); + "tout est bon pour moi ! Je surveille maintenant tes " + importedEvents + " prochains évènements !");
}).then().onErrorResume(throwable -> fatalError(event, throwable)); }).then().onErrorResume(throwable -> fatalError(event, throwable));
} }
private Mono<Void> 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();
}
} }

View File

@ -27,7 +27,7 @@ public final class EventManager {
for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) { for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) {
if (e.getValue().isEmpty()) continue; if (e.getValue().isEmpty()) continue;
e.getKey().setId(StudentManager 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()) for (VEvent event : e.getValue())
if (importEvent(e.getKey(), event, connection) != 0) imported_events++; if (importEvent(e.getKey(), event, connection) != 0) imported_events++;
} }
@ -51,8 +51,10 @@ public final class EventManager {
Instant end = event.getDateEnd().getValue().toInstant(); Instant end = event.getDateEnd().getValue().toInstant();
stmt.setInt(1, student.getId()); stmt.setInt(1, student.getId());
stmt.setString(2, event.getSummary().getValue()); stmt.setString(2, event.getSummary().getValue());
stmt.setTimestamp(3, start == null ? null : new Timestamp(start.toEpochMilli()), event.getDateStart().getValue().getRawComponents().); stmt.setTimestamp(3,
stmt.setTimestamp(4, end == null ? null : new Timestamp(end.toEpochMilli()), CalendarManager.TZ_UTC); 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.executeUpdate();
stmt.getGeneratedKeys().next(); stmt.getGeneratedKeys().next();
assert start != null; assert start != null;

View File

@ -2,6 +2,7 @@ package xyz.vallat.louis.managers.database;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.managers.database.dao.Student;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -42,4 +43,22 @@ public final class StudentManager {
return stmt.getGeneratedKeys().getInt(1); 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;
}
} }

View File

@ -5,16 +5,16 @@ import discord4j.common.util.Snowflake;
public class Student { public class Student {
private int id; private int id;
private final Snowflake snowflake; private final String snowflake;
private final int ade; private final int ade;
public Student(Snowflake snowflake, int ade) { public Student(String snowflake, int ade) {
this.snowflake = snowflake; this.snowflake = snowflake;
this.ade = ade; this.ade = ade;
this.id = 0; this.id = 0;
} }
public Snowflake getSnowflake() { public String getSnowflake() {
return snowflake; return snowflake;
} }
@ -26,7 +26,8 @@ public class Student {
return id; return id;
} }
public void setId(int id) { public Student setId(int id) {
this.id = id; this.id = id;
return this;
} }
} }