Команда получить продукт по id

This commit is contained in:
Ivan Zinchenko 2024-05-30 13:20:22 +03:00
parent 5b95fff14d
commit 2ffcca7eeb
Signed by: zinch
GPG Key ID: 6D45AA2C8FD6A37E

View File

@ -0,0 +1,29 @@
package me.zinch.Lab6.Client.commands;
import me.zinch.Lab6.Client.client.Client;
import me.zinch.Lab6.Client.console.Console;
import me.zinch.Lab6.Client.exceptions.CommandActionException;
import me.zinch.Lab6.Domain.dto.BodyfulMessage;
import me.zinch.Lab6.Domain.dto.BodylessMessage;
import me.zinch.Lab6.Domain.dto.MessageType;
import java.io.IOException;
import java.util.regex.Pattern;
public class GetProductById extends Command {
public GetProductById() {
super("get_product_by_id {element}", "", Pattern.compile("^get_product_by_id +."));
}
@Override
public String action(Client client) throws CommandActionException {
try {
Long id = Long.parseLong(Console.getLastCommand().split(" ")[1]);
var response = (BodylessMessage) client.sendMessage(new BodyfulMessage(MessageType.POST, Console.getLastCommand(), id));
if (response.getType() == MessageType.ERROR) throw new CommandActionException(response.getBody().toString());
return response.getBody().toString();
} catch (IOException | ClassNotFoundException e) {
throw new CommandActionException(e);
}
}
}