implemented client account repository

This commit is contained in:
Andrey Terentev 2024-05-24 12:05:36 +07:00 committed by Andrey Terentev
parent 9064f7cab7
commit da75242279
2 changed files with 27 additions and 12 deletions

View File

@ -13,7 +13,7 @@ public interface AccountRepository {
Account getAccount(AccountId id); Account getAccount(AccountId id);
List<Account> getAccounts(Collection<Account> ids); List<Account> getAccounts(Collection<AccountId> ids);
void createAccount(AccountId id, String password, List<Permission> permissions); void createAccount(AccountId id, String password, List<Permission> permissions);

View File

@ -1,54 +1,69 @@
package ru.dragonestia.picker.api.impl.repository; package ru.dragonestia.picker.api.impl.repository;
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.model.account.Account; import ru.dragonestia.picker.api.model.account.Account;
import ru.dragonestia.picker.api.model.account.AccountId; import ru.dragonestia.picker.api.model.account.AccountId;
import ru.dragonestia.picker.api.model.account.Permission; import ru.dragonestia.picker.api.model.account.Permission;
import ru.dragonestia.picker.api.repository.AccountRepository; import ru.dragonestia.picker.api.repository.AccountRepository;
import ru.dragonestia.picker.api.repository.response.ResponseObject;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public class AccountRepositoryImpl implements AccountRepository { public class AccountRepositoryImpl implements AccountRepository {
private final RestTemplate restTemplate; private final RestTemplate rest;
public AccountRepositoryImpl(RestTemplate restTemplate) { public AccountRepositoryImpl(RestTemplate rest) {
this.restTemplate = restTemplate; this.rest = rest;
} }
@Override @Override
public List<AccountId> allAccountsIds() { public List<AccountId> allAccountsIds() {
throw new UnsupportedOperationException("Not implemented"); List<String> id = rest.queryWithRequest("/accounts", HttpMethod.GET);
return id.stream().map(AccountId::of).toList();
} }
@Override @Override
public Account getAccount(AccountId id) { public Account getAccount(AccountId id) {
throw new UnsupportedOperationException("Not implemented"); ResponseObject.RAccount account = rest.queryWithRequest("/accounts/target/" + id, HttpMethod.GET);
return account.convert();
} }
@Override @Override
public List<Account> getAccounts(Collection<Account> ids) { public List<Account> getAccounts(Collection<AccountId> id) {
throw new UnsupportedOperationException("Not implemented"); List<ResponseObject.RAccount> accounts = rest.queryWithRequest("/accounts/list", HttpMethod.GET, params -> {
params.put("id", String.join(",", id.stream().map(AccountId::getValue).toList()));
});
return accounts.stream().map(ResponseObject.RAccount::convert).toList();
} }
@Override @Override
public void createAccount(AccountId id, String password, List<Permission> permissions) { public void createAccount(AccountId id, String password, List<Permission> permissions) {
throw new UnsupportedOperationException("Not implemented"); rest.query("/accounts", HttpMethod.POST, params -> {
params.put("username", id.getValue());
params.put("password", password);
params.put("permissions", String.join(",", permissions.stream().map(Permission::toString).toList()));
});
} }
@Override @Override
public void deleteAccount(AccountId id) { public void deleteAccount(AccountId id) {
throw new UnsupportedOperationException("Not implemented"); rest.query("/accounts/target/" + id.getValue(), HttpMethod.DELETE);
} }
@Override @Override
public void setPermissions(AccountId id, List<Permission> permissions) { public void setPermissions(AccountId id, List<Permission> permissions) {
throw new UnsupportedOperationException("Not implemented"); rest.query("/accounts/target/" + id.getValue() + "/permissions", HttpMethod.PUT, params -> {
params.put("permissions", String.join(",", permissions.stream().map(Enum::name).toList()));
});
} }
@Override @Override
public void changePassword(AccountId id, String newPassword) { public void changePassword(AccountId id, String newPassword) {
throw new UnsupportedOperationException("Not implemented"); rest.query("/accounts/target/" + id.getValue() + "/password", HttpMethod.PUT, params -> {
params.put("newPassword", newPassword);
});
} }
} }