From da75242279e322ba7709c4a73f3212be6a7a86f1 Mon Sep 17 00:00:00 2001 From: ScarletRedMan Date: Fri, 24 May 2024 12:05:36 +0700 Subject: [PATCH] implemented client account repository --- .../api/repository/AccountRepository.java | 2 +- .../repository/AccountRepositoryImpl.java | 37 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/client-api/src/main/java/ru/dragonestia/picker/api/repository/AccountRepository.java b/client-api/src/main/java/ru/dragonestia/picker/api/repository/AccountRepository.java index b94d403..57b64c6 100644 --- a/client-api/src/main/java/ru/dragonestia/picker/api/repository/AccountRepository.java +++ b/client-api/src/main/java/ru/dragonestia/picker/api/repository/AccountRepository.java @@ -13,7 +13,7 @@ public interface AccountRepository { Account getAccount(AccountId id); - List getAccounts(Collection ids); + List getAccounts(Collection ids); void createAccount(AccountId id, String password, List permissions); diff --git a/client-impl/src/main/java/ru/dragonestia/picker/api/impl/repository/AccountRepositoryImpl.java b/client-impl/src/main/java/ru/dragonestia/picker/api/impl/repository/AccountRepositoryImpl.java index 53a4b5d..ec53379 100644 --- a/client-impl/src/main/java/ru/dragonestia/picker/api/impl/repository/AccountRepositoryImpl.java +++ b/client-impl/src/main/java/ru/dragonestia/picker/api/impl/repository/AccountRepositoryImpl.java @@ -1,54 +1,69 @@ package ru.dragonestia.picker.api.impl.repository; 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.AccountId; import ru.dragonestia.picker.api.model.account.Permission; import ru.dragonestia.picker.api.repository.AccountRepository; +import ru.dragonestia.picker.api.repository.response.ResponseObject; import java.util.Collection; import java.util.List; public class AccountRepositoryImpl implements AccountRepository { - private final RestTemplate restTemplate; + private final RestTemplate rest; - public AccountRepositoryImpl(RestTemplate restTemplate) { - this.restTemplate = restTemplate; + public AccountRepositoryImpl(RestTemplate rest) { + this.rest = rest; } @Override public List allAccountsIds() { - throw new UnsupportedOperationException("Not implemented"); + List id = rest.queryWithRequest("/accounts", HttpMethod.GET); + return id.stream().map(AccountId::of).toList(); } @Override public Account getAccount(AccountId id) { - throw new UnsupportedOperationException("Not implemented"); + ResponseObject.RAccount account = rest.queryWithRequest("/accounts/target/" + id, HttpMethod.GET); + return account.convert(); } @Override - public List getAccounts(Collection ids) { - throw new UnsupportedOperationException("Not implemented"); + public List getAccounts(Collection id) { + List 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 public void createAccount(AccountId id, String password, List 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 public void deleteAccount(AccountId id) { - throw new UnsupportedOperationException("Not implemented"); + rest.query("/accounts/target/" + id.getValue(), HttpMethod.DELETE); } @Override public void setPermissions(AccountId id, List 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 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); + }); } }