Add serverside CHECK_NICK-command #231
+15
@@ -6,15 +6,30 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandH
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Handles {@link CheckUsernameRequest}s to check whether a username is available. */
|
||||
public class CheckUsernameHandler implements CommandHandler<CheckUsernameRequest> {
|
||||
private final ResponseDispatcher responseDispatcher;
|
||||
private final UserRegistry userRegistry;
|
||||
|
||||
/**
|
||||
* Creates a new handler for checking username availability.
|
||||
*
|
||||
* @param responseDispatcher the dispatcher used to send the response
|
||||
* @param userRegistry the registry used to look up existing users
|
||||
*/
|
||||
public CheckUsernameHandler(ResponseDispatcher responseDispatcher, UserRegistry userRegistry) {
|
||||
this.responseDispatcher = responseDispatcher;
|
||||
this.userRegistry = userRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the username availability check for the given request.
|
||||
*
|
||||
* <p>If no user exists for the requested username, the username is reported as {@link
|
||||
* UsernameAvailability#FREE}; otherwise, it is reported as {@link UsernameAvailability#TAKEN}.
|
||||
*
|
||||
* @param request the request to execute
|
||||
*/
|
||||
@Override
|
||||
public void execute(CheckUsernameRequest request) {
|
||||
Optional<User> user = userRegistry.getByUsername(request.getUsername());
|
||||
|
||||
+7
@@ -4,7 +4,14 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandPar
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.RequestParameterAccessor;
|
||||
|
||||
/** Parses a primitive request into a {@link CheckUsernameRequest}. */
|
||||
public class CheckUsernameParser implements CommandParser<CheckUsernameRequest> {
|
||||
/**
|
||||
* Extracts the required {@code USERNAME} parameter from the incoming request.
|
||||
*
|
||||
* @param primitiveRequest the request to parse
|
||||
* @return {@link CheckUsernameRequest} containing the username
|
||||
*/
|
||||
@Override
|
||||
public CheckUsernameRequest parse(PrimitiveRequest primitiveRequest) {
|
||||
RequestParameterAccessor accessor = new RequestParameterAccessor(primitiveRequest.parameters());
|
||||
|
||||
+13
@@ -3,14 +3,27 @@ package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||
|
||||
/** Request implementation used to check whether a username is available or already taken */
|
||||
public class CheckUsernameRequest extends Request {
|
||||
private final String username;
|
||||
|
||||
/**
|
||||
* Constructs a new CheckUsernameRequest with the given context and username to check
|
||||
*
|
||||
* @param context the {@link RequestContext} containing information for responding to the
|
||||
* request
|
||||
* @param username the username to check for availability
|
||||
*/
|
||||
public CheckUsernameRequest(RequestContext context, String username) {
|
||||
super(context);
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the provided username in the request
|
||||
*
|
||||
* @return username to check
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
+7
@@ -4,7 +4,14 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.SuccessResponse;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBodyBuilder;
|
||||
|
||||
/** Response indicating the availability status of a username check. */
|
||||
public class CheckUsernameResponse extends SuccessResponse {
|
||||
/**
|
||||
* Creates a new response to respond to the username availability check to
|
||||
*
|
||||
* @param context the {@link RequestContext} associated with the request
|
||||
* @param availability the availability status of the requested username
|
||||
*/
|
||||
public CheckUsernameResponse(RequestContext context, UsernameAvailability availability) {
|
||||
super(context, new ResponseBodyBuilder().param("STATUS", availability).build());
|
||||
}
|
||||
|
||||
+4
@@ -1,6 +1,10 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.check_nick;
|
||||
|
||||
/** Represents the availability status of a username */
|
||||
enum UsernameAvailability {
|
||||
/** Username is available */
|
||||
FREE,
|
||||
|
||||
/** Username is already in use */
|
||||
TAKEN
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user