Archived
1
0
Fork 0

Add channels list command

This commit is contained in:
Ethanell 2022-01-07 11:53:35 +01:00
parent 7cf3b41e6a
commit 62727bcb3c
3 changed files with 37 additions and 5 deletions

View file

@ -18,6 +18,7 @@ import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Client {
private final int port;
@ -92,6 +93,7 @@ public class Client {
case login -> commandLogin();
case message -> commandMessage(cmd);
case list -> commandList(cmd);
case listChannels -> commandListChannels(cmd);
case join -> commandJoin(cmd);
}
}
@ -99,6 +101,8 @@ public class Client {
private void commandLogin() throws IOException {
out.writeObject(new Command(CommandType.list, null));
out.flush();
out.writeObject(new Command(CommandType.listChannels, null));
out.flush();
out.writeObject(new Command(CommandType.join, List.of("general")));
out.flush();
}
@ -109,10 +113,11 @@ public class Client {
}
private void commandList(Command cmd) {
List<Object> users = cmd.getArgs();
for (Object u : users) {
System.out.println(u);
}
System.out.println("Users: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
}
private void commandListChannels(Command cmd) {
System.out.println("Channels: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
}
private void commandJoin(Command cmd) {

View file

@ -48,6 +48,7 @@ public class ConnectedClient implements Runnable {
case login -> commandLogin(command);
case message -> commandMessage(command);
case list -> commandList();
case listChannels -> commandListChannels();
case join -> commandJoin(command);
}
}
@ -89,7 +90,12 @@ public class ConnectedClient implements Runnable {
}
private void commandList() throws IOException {
out.writeObject(new Command(CommandType.list, Collections.singletonList(server.getUsers())));
out.writeObject(new Command(CommandType.list, Collections.singletonList(server.getUsers())));
out.flush();
}
private void commandListChannels() throws IOException {
out.writeObject(new Command(CommandType.listChannels, Collections.singletonList((List<Channel>)(List<?>) ChannelModel.getAll())));
out.flush();
}

View file

@ -6,6 +6,8 @@ import fr.univ.lyon1.common.User;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ChannelModel extends Channel implements Model {
@ -63,6 +65,25 @@ public class ChannelModel extends Channel implements Model {
return null;
}
public static List<ChannelModel> getAll() {
List<ChannelModel> channels = new ArrayList<>();
try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME);
if (ps.execute()) {
ResultSet rs = ps.getResultSet();
while (rs.next())
channels.add(new ChannelModel(
UUID.fromString(rs.getString("UUID")),
rs.getString("NAME"))
);
}
} catch (SQLException err) {
err.printStackTrace();
return null;
}
return channels;
}
private boolean exist() {
try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?");