implemented client-side account management

This commit is contained in:
Andrey Terentev 2024-04-25 16:43:32 +07:00 committed by Andrey Terentev
parent 02d71c7138
commit b61bd9c366
2 changed files with 39 additions and 0 deletions

View File

@ -1,11 +1,21 @@
package ru.dragonestia.picker.api.repository; package ru.dragonestia.picker.api.repository;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import ru.dragonestia.picker.api.model.account.IAccount;
import ru.dragonestia.picker.api.model.account.ResponseAccount; import ru.dragonestia.picker.api.model.account.ResponseAccount;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface AccountRepository { public interface AccountRepository {
Optional<ResponseAccount> findAccountByUsername(@NotNull String username); Optional<ResponseAccount> findAccountByUsername(@NotNull String username);
@NotNull List<ResponseAccount> allAccounts();
void createAccount(@NotNull String accountId, @NotNull String password);
void removeAccount(@NotNull IAccount account);
void setPermissions(@NotNull IAccount account, @NotNull List<String> permissions);
} }

View File

@ -6,9 +6,12 @@ import ru.dragonestia.picker.api.exception.AccountDoesNotExistsException;
import ru.dragonestia.picker.api.impl.RoomPickerClient; import ru.dragonestia.picker.api.impl.RoomPickerClient;
import ru.dragonestia.picker.api.impl.util.RestTemplate; import ru.dragonestia.picker.api.impl.util.RestTemplate;
import ru.dragonestia.picker.api.impl.util.type.HttpMethod; import ru.dragonestia.picker.api.impl.util.type.HttpMethod;
import ru.dragonestia.picker.api.model.account.IAccount;
import ru.dragonestia.picker.api.model.account.ResponseAccount; import ru.dragonestia.picker.api.model.account.ResponseAccount;
import ru.dragonestia.picker.api.repository.AccountRepository; import ru.dragonestia.picker.api.repository.AccountRepository;
import ru.dragonestia.picker.api.repository.response.AllAccountsResponse;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class AccountRepositoryImpl implements AccountRepository { public class AccountRepositoryImpl implements AccountRepository {
@ -29,4 +32,30 @@ public class AccountRepositoryImpl implements AccountRepository {
return Optional.empty(); return Optional.empty();
} }
} }
@Override
public @NotNull List<ResponseAccount> allAccounts() {
var response = rest.query("/accounts", HttpMethod.GET, AllAccountsResponse.class);
return response.accounts();
}
@Override
public void createAccount(@NotNull String accountId, @NotNull String password) {
rest.query("/accounts", HttpMethod.POST, params -> {
params.put("username", accountId);
params.put("password", password);
});
}
@Override
public void removeAccount(@NotNull IAccount account) {
rest.query("/accounts/" + account.getUsername(), HttpMethod.DELETE);
}
@Override
public void setPermissions(@NotNull IAccount account, @NotNull List<String> permissions) {
rest.query("/accounts/" + account.getUsername(), HttpMethod.PUT, params -> {
params.put("permissions", String.join(",", permissions));
});
}
} }