From 74d02993420a64e535d2ac40a297554d9e45d096 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Fri, 24 May 2019 15:47:33 +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.=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 +- .../{distribution => convert}/Convert.java | 4 +- .../core/convert/MessageMailConvert.java | 50 +++++++++++++ .../distribution/AbstractBasketSubscribe.java | 2 + .../core/distribution/AccountSubscribe.java | 43 ++++++++++- .../core/distribution/EventDistributor.java | 1 + .../core/distribution/MailSubscriber.java | 39 ++-------- .../vkbot/core/sender/MailSenderVk.java | 4 +- .../vkbot/core/service/PersonServiceImpl.java | 74 ------------------- 9 files changed, 103 insertions(+), 117 deletions(-) rename src/main/java/org/sadtech/vkbot/core/{distribution => convert}/Convert.java (50%) create mode 100644 src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java diff --git a/pom.xml b/pom.xml index b0b0bd0..9b8e494 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,8 @@ 0.6.1-SNAPSHOT - 0.5.13-SNAPSHOT + + 1.0.2 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java similarity index 50% rename from src/main/java/org/sadtech/vkbot/core/distribution/Convert.java rename to src/main/java/org/sadtech/vkbot/core/convert/Convert.java index 6f2e201..792bd25 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java @@ -1,8 +1,8 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vkbot.core.convert; -@FunctionalInterface public interface Convert { C converting(T target); + } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java new file mode 100644 index 0000000..a09b446 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -0,0 +1,50 @@ +package org.sadtech.vkbot.core.convert; + +import com.vk.api.sdk.objects.messages.Message; +import com.vk.api.sdk.objects.messages.MessageAttachment; +import org.sadtech.bot.core.domain.attachment.Attachment; +import org.sadtech.bot.core.domain.attachment.AudioMessage; +import org.sadtech.bot.core.domain.attachment.Geo; +import org.sadtech.bot.core.domain.content.Mail; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.TimeZone; +import java.util.stream.Collectors; + +public class MessageMailConvert implements Convert { + + @Override + public Mail converting(Message message) { + Mail mail = new Mail(); + mail.setMessage(message.getText()); + mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); + mail.setId(message.getId()); + mail.setPersonId(message.getPeerId()); + mail.setAttachments(message.getAttachments() + .stream() + .map(this::convertAttachment) + .collect(Collectors.toList())); + mail.getAttachments().add(convertGeo(message.getGeo())); + return mail; + } + + private Geo convertGeo(com.vk.api.sdk.objects.base.Geo geoVk) { + return Geo.builder() + .coordinate(geoVk.getCoordinates().getLatitude(), + geoVk.getCoordinates().getLongitude()) + .build(); + } + + private Attachment convertAttachment(MessageAttachment vkAttachment) { + Attachment attachment = null; + switch (vkAttachment.getType()) { + case AUDIO_MESSAGE: + AudioMessage audioMessage = new AudioMessage(); + audioMessage.setLinkOdd(vkAttachment.getAudioMessage().getLinkOgg()); + attachment = audioMessage; + break; + } + return attachment; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java index f136494..7d3affe 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -1,5 +1,7 @@ package org.sadtech.vkbot.core.distribution; +import org.sadtech.vkbot.core.convert.Convert; + import java.util.Set; public abstract class AbstractBasketSubscribe { diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index aba0a81..d100355 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -1,14 +1,41 @@ package org.sadtech.vkbot.core.distribution; import com.vk.api.sdk.objects.messages.Message; +import org.sadtech.bot.core.domain.BoxAnswer; +import org.sadtech.bot.core.exception.PaymentException; import org.sadtech.bot.core.service.AccountService; +import org.sadtech.bot.core.service.sender.Sent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AccountSubscribe extends AbstractBasketSubscribe { - private final AccountService accountService; + private static final Logger log = LoggerFactory.getLogger(AccountSubscribe.class); - public AccountSubscribe(AccountService accountService) { + private final AccountService accountService; + private final Sent sent; + private BoxAnswer answerSuccessfulPayment; + private BoxAnswer answerFailPayment; + + public AccountSubscribe(AccountService accountService, Sent sent) { this.accountService = accountService; + this.sent = sent; + } + + public BoxAnswer getAnswerSuccessfulPayment() { + return answerSuccessfulPayment; + } + + public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) { + this.answerSuccessfulPayment = answerSuccessfulPayment; + } + + public BoxAnswer getAnswerFailPayment() { + return answerFailPayment; + } + + public void setAnswerFailPayment(BoxAnswer answerFailPayment) { + this.answerFailPayment = answerFailPayment; } @Override @@ -17,7 +44,15 @@ public class AccountSubscribe extends AbstractBasketSubscribe } @Override - public void processing(Message object) { - accountService.pay(0); + public void processing(Message message) { + if (message.getText() != null) { + try { + if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]))) { + sent.send(message.getPeerId(), answerSuccessfulPayment); + } + } catch (PaymentException e) { + sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); + } + } } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index f5e250f..731da2e 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -20,6 +20,7 @@ public class EventDistributor extends AbstractBasketSubscribe { - private static final Logger log = Logger.getLogger(MailSubscriber.class); - private final MailService mailService; + private final Convert mailConvert = new MessageMailConvert(); public MailSubscriber(MailService mailService) { this.mailService = mailService; @@ -39,26 +29,7 @@ public class MailSubscriber extends AbstractBasketSubscribe @Override public void processing(Message object) { - mailService.add(createMail(object)); - } - - private Mail createMail(Message message) { - Mail mail = new Mail(); - mail.setMessage(message.getText()); - mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); - mail.setId(message.getId()); - mail.setPersonId(message.getPeerId()); - - List attachments = new ArrayList<>(); - for (MessageAttachment attachment : message.getAttachments()) { - if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) { - AudioMessage audioMessage = new AudioMessage(); - audioMessage.setLinkOdd(attachment.getAudioMessage().getLinkOgg()); - attachments.add(audioMessage); - } - } - mail.setAttachments(attachments); - return mail; + mailService.add(mailConvert.converting(object)); } } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java index 09bb80b..0af6de0 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -54,8 +54,8 @@ public class MailSenderVk implements Sent { keyboard.setButtons(Collections.EMPTY_LIST); messages.keyboard(keyboard); } - if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) { - messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong()); + if (boxAnswer.getCoordinates() != null) { + messages.lat(boxAnswer.getCoordinates().getLat()).lng(boxAnswer.getCoordinates().getaLong()); } if (boxAnswer.getStickerId() != null) { try { diff --git a/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java deleted file mode 100644 index c14abb0..0000000 --- a/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.sadtech.vkbot.core.service; - -import com.vk.api.sdk.objects.users.User; -import com.vk.api.sdk.objects.users.UserMin; -import org.apache.log4j.Logger; -import org.sadtech.bot.core.domain.Person; -import org.sadtech.bot.core.repository.PersonRepository; -import org.sadtech.bot.core.repository.impl.PersonRepositoryMap; -import org.sadtech.bot.core.service.PersonService; -import org.sadtech.vkbot.core.VkApi; -import org.sadtech.vkbot.core.VkConnect; - -public class PersonServiceImpl implements PersonService { - - private static final Logger log = Logger.getLogger(PersonServiceImpl.class); - - private final PersonRepository personRepository; - private final VkApi vkApi; - - public PersonServiceImpl(VkConnect vkConnect) { - this.personRepository = new PersonRepositoryMap(); - vkApi = new VkApi(vkConnect); - } - - public PersonServiceImpl(PersonRepository personRepository, VkConnect vkConnect) { - this.personRepository = personRepository; - vkApi = new VkApi(vkConnect); - } - - public void add(User user) { - Person person = new Person(); - person.setId(user.getId()); - person.setLastName(user.getLastName()); - person.setFirstName(user.getFirstName()); - personRepository.add(person); - log.info("Пользователь добавлен в репозиторий"); - } - - @Override - public void add(Person person) { - personRepository.add(person); - log.info("Пользователь добавлен в репозиторий"); - } - - public void add(UserMin userMin) { - Person person = new Person(); - person.setId(userMin.getId()); - person.setFirstName(userMin.getFirstName()); - person.setLastName(userMin.getLastName()); - personRepository.add(person); - } - - @Override - public Person get(Integer id) { - return personRepository.get(id); - } - - @Override - public boolean checkPerson(Integer idPerson) { - log.info("Проверка наличия пользователя в репозитории"); - return get(idPerson) != null; - } - - @Override - public Person createPerson(Integer userId) { - Person person = new Person(); - UserMin userMin = vkApi.getUserMini(userId); - person.setId(userMin.getId()); - person.setLastName(userMin.getLastName()); - person.setFirstName(userMin.getFirstName()); - return person; - } - -}