Рефакторинг. Удалил ненужный код

This commit is contained in:
Mark Struchkov 2019-05-24 15:47:33 +03:00
parent d40a33bf80
commit 74d0299342
9 changed files with 103 additions and 117 deletions

View File

@ -24,7 +24,8 @@
<properties>
<bot.core.ver>0.6.1-SNAPSHOT</bot.core.ver>
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<vksdk.ver>1.0.2</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver>
</properties>

View File

@ -1,8 +1,8 @@
package org.sadtech.vkbot.core.distribution;
package org.sadtech.vkbot.core.convert;
@FunctionalInterface
public interface Convert<T, C> {
C converting(T target);
}

View File

@ -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<Message, Mail> {
@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;
}
}

View File

@ -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<S, C> {

View File

@ -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<Message, Message> {
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<Message, Message>
}
@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());
}
}
}
}

View File

@ -20,6 +20,7 @@ public class EventDistributor extends AbstractBasketSubscribe<JsonObject, JsonOb
while (true) {
if (rawEventService.getJsonObjects().peek() != null) {
JsonObject event = rawEventService.getJsonObjects().poll();
log.info("Добавлено новое событие");
super.update(event);
}
}

View File

@ -3,25 +3,15 @@ package org.sadtech.vkbot.core.distribution;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.messages.MessageAttachment;
import com.vk.api.sdk.objects.messages.MessageAttachmentType;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.domain.attachment.Attachment;
import org.sadtech.bot.core.domain.attachment.AudioMessage;
import org.sadtech.bot.core.domain.content.Mail;
import org.sadtech.bot.core.service.MailService;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import org.sadtech.vkbot.core.convert.Convert;
import org.sadtech.vkbot.core.convert.MessageMailConvert;
public class MailSubscriber extends AbstractBasketSubscribe<JsonObject, Message> {
private static final Logger log = Logger.getLogger(MailSubscriber.class);
private final MailService mailService;
private final Convert<Message, Mail> mailConvert = new MessageMailConvert();
public MailSubscriber(MailService mailService) {
this.mailService = mailService;
@ -39,26 +29,7 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject, Message>
@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<Attachment> 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));
}
}

View File

@ -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 {

View File

@ -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;
}
}