From 8e5edc1e9e7f1e483b7e7ab1c66bc06f1de751e7 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 26 May 2019 22:15:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sadtech/bot/core/domain/BoxAnswer.java | 2 +- .../domain/{ => content}/EmptyContent.java | 3 +- .../sadtech/bot/core/domain/content/Mail.java | 2 +- .../{ => content}/attachment/Attachment.java | 2 +- .../attachment/AttachmentType.java | 2 +- .../attachment/AudioMessage.java | 2 +- .../domain/{ => content}/attachment/Geo.java | 4 +-- .../{ => attachment}/GeoCoordinate.java | 2 +- .../bot/core/domain/money/AccountStatus.java | 2 +- .../bot/core/exception/AccessException.java | 7 +++++ .../bot/core/exception/NotFoundException.java | 7 +++++ .../repository/impl/AccountRepositoryMap.java | 31 ++++++++++++++----- .../bot/core/service/EventService.java | 4 ++- .../bot/core/service/{filter => }/Filter.java | 3 +- .../bot/core/service/RawEventService.java | 3 -- .../service/impl/RawEventServiceImpl.java | 5 --- .../org/sadtech/bot/core/utils/Contents.java | 2 +- 17 files changed, 53 insertions(+), 30 deletions(-) rename src/main/java/org/sadtech/bot/core/domain/{ => content}/EmptyContent.java (77%) rename src/main/java/org/sadtech/bot/core/domain/{ => content}/attachment/Attachment.java (70%) rename src/main/java/org/sadtech/bot/core/domain/{ => content}/attachment/AttachmentType.java (50%) rename src/main/java/org/sadtech/bot/core/domain/{ => content}/attachment/AudioMessage.java (93%) rename src/main/java/org/sadtech/bot/core/domain/{ => content}/attachment/Geo.java (94%) rename src/main/java/org/sadtech/bot/core/domain/content/{ => attachment}/GeoCoordinate.java (90%) create mode 100644 src/main/java/org/sadtech/bot/core/exception/AccessException.java create mode 100644 src/main/java/org/sadtech/bot/core/exception/NotFoundException.java rename src/main/java/org/sadtech/bot/core/service/{filter => }/Filter.java (69%) diff --git a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java index b84a414..1345258 100644 --- a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java +++ b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java @@ -1,6 +1,6 @@ package org.sadtech.bot.core.domain; -import org.sadtech.bot.core.domain.content.GeoCoordinate; +import org.sadtech.bot.core.domain.content.attachment.GeoCoordinate; import org.sadtech.bot.core.domain.keyboard.KeyBoard; import java.util.Objects; diff --git a/src/main/java/org/sadtech/bot/core/domain/EmptyContent.java b/src/main/java/org/sadtech/bot/core/domain/content/EmptyContent.java similarity index 77% rename from src/main/java/org/sadtech/bot/core/domain/EmptyContent.java rename to src/main/java/org/sadtech/bot/core/domain/content/EmptyContent.java index 328eb32..2400a1e 100644 --- a/src/main/java/org/sadtech/bot/core/domain/EmptyContent.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/EmptyContent.java @@ -1,6 +1,5 @@ -package org.sadtech.bot.core.domain; +package org.sadtech.bot.core.domain.content; -import org.sadtech.bot.core.domain.content.Content; import org.sadtech.bot.core.exception.AppBotException; public class EmptyContent extends Content { diff --git a/src/main/java/org/sadtech/bot/core/domain/content/Mail.java b/src/main/java/org/sadtech/bot/core/domain/content/Mail.java index 3d0c3fe..0e4e720 100644 --- a/src/main/java/org/sadtech/bot/core/domain/content/Mail.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/Mail.java @@ -1,6 +1,6 @@ package org.sadtech.bot.core.domain.content; -import org.sadtech.bot.core.domain.attachment.Attachment; +import org.sadtech.bot.core.domain.content.attachment.Attachment; import java.time.LocalDateTime; import java.util.List; diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/Attachment.java b/src/main/java/org/sadtech/bot/core/domain/content/attachment/Attachment.java similarity index 70% rename from src/main/java/org/sadtech/bot/core/domain/attachment/Attachment.java rename to src/main/java/org/sadtech/bot/core/domain/content/attachment/Attachment.java index ecf3782..fef3973 100644 --- a/src/main/java/org/sadtech/bot/core/domain/attachment/Attachment.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/attachment/Attachment.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.domain.attachment; +package org.sadtech.bot.core.domain.content.attachment; public abstract class Attachment { diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java b/src/main/java/org/sadtech/bot/core/domain/content/attachment/AttachmentType.java similarity index 50% rename from src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java rename to src/main/java/org/sadtech/bot/core/domain/content/attachment/AttachmentType.java index 2487c99..16fb351 100644 --- a/src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/attachment/AttachmentType.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.domain.attachment; +package org.sadtech.bot.core.domain.content.attachment; public enum AttachmentType { diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java b/src/main/java/org/sadtech/bot/core/domain/content/attachment/AudioMessage.java similarity index 93% rename from src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java rename to src/main/java/org/sadtech/bot/core/domain/content/attachment/AudioMessage.java index ef6583a..15f61eb 100644 --- a/src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/attachment/AudioMessage.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.domain.attachment; +package org.sadtech.bot.core.domain.content.attachment; import java.net.URL; import java.util.Objects; diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/Geo.java b/src/main/java/org/sadtech/bot/core/domain/content/attachment/Geo.java similarity index 94% rename from src/main/java/org/sadtech/bot/core/domain/attachment/Geo.java rename to src/main/java/org/sadtech/bot/core/domain/content/attachment/Geo.java index e6bc1bb..520ea14 100644 --- a/src/main/java/org/sadtech/bot/core/domain/attachment/Geo.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/attachment/Geo.java @@ -1,6 +1,4 @@ -package org.sadtech.bot.core.domain.attachment; - -import org.sadtech.bot.core.domain.content.GeoCoordinate; +package org.sadtech.bot.core.domain.content.attachment; import java.util.Objects; diff --git a/src/main/java/org/sadtech/bot/core/domain/content/GeoCoordinate.java b/src/main/java/org/sadtech/bot/core/domain/content/attachment/GeoCoordinate.java similarity index 90% rename from src/main/java/org/sadtech/bot/core/domain/content/GeoCoordinate.java rename to src/main/java/org/sadtech/bot/core/domain/content/attachment/GeoCoordinate.java index 09998c0..69dfa8b 100644 --- a/src/main/java/org/sadtech/bot/core/domain/content/GeoCoordinate.java +++ b/src/main/java/org/sadtech/bot/core/domain/content/attachment/GeoCoordinate.java @@ -1,4 +1,4 @@ -package org.sadtech.bot.core.domain.content; +package org.sadtech.bot.core.domain.content.attachment; public class GeoCoordinate { diff --git a/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java b/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java index bbd8622..5ea1cec 100644 --- a/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java +++ b/src/main/java/org/sadtech/bot/core/domain/money/AccountStatus.java @@ -2,6 +2,6 @@ package org.sadtech.bot.core.domain.money; public enum AccountStatus { - EXPOSED, CLOSED, CANCELLED, EXCEPTION; + EXPOSED, CLOSED, CANCELLED, EXCEPTION } diff --git a/src/main/java/org/sadtech/bot/core/exception/AccessException.java b/src/main/java/org/sadtech/bot/core/exception/AccessException.java new file mode 100644 index 0000000..5c722ad --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/exception/AccessException.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.exception; + +public class AccessException extends AppBotException { + public AccessException(Integer code, String message) { + super(code, message); + } +} diff --git a/src/main/java/org/sadtech/bot/core/exception/NotFoundException.java b/src/main/java/org/sadtech/bot/core/exception/NotFoundException.java new file mode 100644 index 0000000..d3c6351 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/exception/NotFoundException.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.exception; + +public class NotFoundException extends AppBotException { + public NotFoundException(Integer code, String message) { + super(code, message); + } +} diff --git a/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryMap.java b/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryMap.java index 66f8ed0..a5fa3de 100644 --- a/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryMap.java +++ b/src/main/java/org/sadtech/bot/core/repository/impl/AccountRepositoryMap.java @@ -1,31 +1,48 @@ package org.sadtech.bot.core.repository.impl; import org.sadtech.bot.core.domain.money.Account; +import org.sadtech.bot.core.exception.AccessException; +import org.sadtech.bot.core.exception.NotFoundException; +import org.sadtech.bot.core.exception.PaymentException; import org.sadtech.bot.core.repository.AccountRepository; import java.util.HashMap; import java.util.Map; +import java.util.Optional; public class AccountRepositoryMap implements AccountRepository { private final Map saveMap = new HashMap<>(); - private Integer id = 0; + private Integer id = 1; @Override public Integer add(Account account) { - account.setId(id); - saveMap.put(id, account); - return id++; + if (check(account.getId())) { + account.setId(id); + saveMap.put(id, account); + return id++; + } else { + throw new AccessException(312, "Счет " + account.getId() + " уже существует"); + } + } @Override public void edit(Integer accountId, Account account) { - account.setId(accountId); - saveMap.put(accountId, account); + if (check(id)) { + account.setId(accountId); + saveMap.put(accountId, account); + } else { + throw new NotFoundException(491, "Счет " + accountId + " не найден"); + } } @Override public Account findById(Integer accountId) { - return saveMap.get(accountId); + return Optional.ofNullable(saveMap.get(accountId)).orElseThrow(() -> new PaymentException(43, "Счет " + accountId + " не найден")); + } + + private boolean check(Integer id) { + return saveMap.containsKey(id); } } diff --git a/src/main/java/org/sadtech/bot/core/service/EventService.java b/src/main/java/org/sadtech/bot/core/service/EventService.java index fcb7a1e..9527c4e 100644 --- a/src/main/java/org/sadtech/bot/core/service/EventService.java +++ b/src/main/java/org/sadtech/bot/core/service/EventService.java @@ -1,9 +1,11 @@ package org.sadtech.bot.core.service; +import org.sadtech.bot.core.domain.content.Content; + import java.time.LocalDateTime; import java.util.List; -public interface EventService { +public interface EventService { void add(T event); diff --git a/src/main/java/org/sadtech/bot/core/service/filter/Filter.java b/src/main/java/org/sadtech/bot/core/service/Filter.java similarity index 69% rename from src/main/java/org/sadtech/bot/core/service/filter/Filter.java rename to src/main/java/org/sadtech/bot/core/service/Filter.java index 1556412..4da035f 100644 --- a/src/main/java/org/sadtech/bot/core/service/filter/Filter.java +++ b/src/main/java/org/sadtech/bot/core/service/Filter.java @@ -1,7 +1,8 @@ -package org.sadtech.bot.core.service.filter; +package org.sadtech.bot.core.service; import org.sadtech.bot.core.domain.content.Content; +@FunctionalInterface public interface Filter { void processing(T content); diff --git a/src/main/java/org/sadtech/bot/core/service/RawEventService.java b/src/main/java/org/sadtech/bot/core/service/RawEventService.java index 46e43f3..168d2f9 100644 --- a/src/main/java/org/sadtech/bot/core/service/RawEventService.java +++ b/src/main/java/org/sadtech/bot/core/service/RawEventService.java @@ -1,7 +1,6 @@ package org.sadtech.bot.core.service; import com.google.gson.JsonObject; -import org.sadtech.bot.core.repository.EventRepository; import java.util.Queue; @@ -13,6 +12,4 @@ public interface RawEventService { Queue getJsonObjects(); - EventRepository getEventRepository(); - } \ No newline at end of file diff --git a/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java b/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java index c592287..a61880c 100644 --- a/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java +++ b/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java @@ -35,9 +35,4 @@ public class RawEventServiceImpl implements RawEventService { return eventRepository.getEventQueue(); } - @Override - public EventRepository getEventRepository() { - return eventRepository; - } - } diff --git a/src/main/java/org/sadtech/bot/core/utils/Contents.java b/src/main/java/org/sadtech/bot/core/utils/Contents.java index c3d0c66..9b417b1 100644 --- a/src/main/java/org/sadtech/bot/core/utils/Contents.java +++ b/src/main/java/org/sadtech/bot/core/utils/Contents.java @@ -1,6 +1,6 @@ package org.sadtech.bot.core.utils; -import org.sadtech.bot.core.domain.EmptyContent; +import org.sadtech.bot.core.domain.content.EmptyContent; public class Contents {