Рефакторинг

Добавил новое вложение - карта
This commit is contained in:
Mark Struchkov 2019-05-24 15:47:05 +03:00
parent fd8c8402d1
commit dfc4f36e55
29 changed files with 254 additions and 256 deletions

View File

@ -1,5 +1,6 @@
package org.sadtech.bot.core.domain;
import org.sadtech.bot.core.domain.content.GeoCoordinate;
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
import java.util.Objects;
@ -8,8 +9,7 @@ public class BoxAnswer {
private String message;
private KeyBoard keyboard;
private Float lat;
private Float aLong;
private GeoCoordinate coordinates;
private Integer stickerId;
private BoxAnswer() {
@ -20,8 +20,7 @@ public class BoxAnswer {
if (target != null) {
this.message = target.getMessage();
this.keyboard = target.getKeyboard();
this.lat = target.getLat();
this.aLong = target.getaLong();
this.coordinates = target.getCoordinates();
this.stickerId = target.getStickerId();
}
}
@ -43,20 +42,8 @@ public class BoxAnswer {
this.keyboard = keyboard;
}
public Float getLat() {
return lat;
}
public void setLat(Float lat) {
this.lat = lat;
}
public Float getaLong() {
return aLong;
}
public void setaLong(Float aLong) {
this.aLong = aLong;
public GeoCoordinate getCoordinates() {
return coordinates;
}
public Integer getStickerId() {
@ -90,13 +77,8 @@ public class BoxAnswer {
return this;
}
public Builder lat(Float lat) {
BoxAnswer.this.lat = lat;
return this;
}
public Builder aLong(Float aLong) {
BoxAnswer.this.aLong = aLong;
public Builder coordinate(Float lat, Float aLong) {
BoxAnswer.this.coordinates = new GeoCoordinate(lat, aLong);
return this;
}
@ -113,19 +95,26 @@ public class BoxAnswer {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!(o instanceof BoxAnswer)) return false;
BoxAnswer boxAnswer = (BoxAnswer) o;
return Objects.equals(message, boxAnswer.message) &&
Objects.equals(keyboard, boxAnswer.keyboard) &&
Objects.equals(lat, boxAnswer.lat) &&
Objects.equals(aLong, boxAnswer.aLong) &&
Objects.equals(coordinates, boxAnswer.coordinates) &&
Objects.equals(stickerId, boxAnswer.stickerId);
}
@Override
public int hashCode() {
return Objects.hash(message, keyboard, lat, aLong, stickerId);
return Objects.hash(message, keyboard, coordinates, stickerId);
}
@Override
public String toString() {
return "BoxAnswer{" +
"message='" + message + '\'' +
", keyboard=" + keyboard +
", coordinates=" + coordinates +
", stickerId=" + stickerId +
'}';
}
}

View File

@ -1,11 +1,10 @@
package org.sadtech.bot.core.domain;
import org.sadtech.bot.core.domain.content.Content;
import org.sadtech.bot.core.exception.AppBotException;
public class EmptyContent extends Content {
public EmptyContent() {
}
@Override
public String getMessage() {
return "";
@ -13,6 +12,6 @@ public class EmptyContent extends Content {
@Override
public void setMessage(String message) {
throw new AppBotException(0, "EmptyContent no setMessage");
}
}

View File

@ -1,91 +0,0 @@
package org.sadtech.bot.core.domain;
import java.util.Objects;
public class Person {
private Integer id;
private String firstName;
private String lastName;
private Integer sex;
private String city;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) &&
Objects.equals(firstName, person.firstName) &&
Objects.equals(lastName, person.lastName) &&
Objects.equals(sex, person.sex) &&
Objects.equals(city, person.city) &&
Objects.equals(email, person.email);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, sex, city, email);
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", sex=" + sex +
", city='" + city + '\'' +
", email='" + email + '\'' +
'}';
}
}

View File

@ -7,4 +7,5 @@ public abstract class Attachment {
public AttachmentType getType() {
return type;
}
}

View File

@ -1,5 +1,7 @@
package org.sadtech.bot.core.domain.attachment;
public enum AttachmentType {
AUDIO_MESSAGE
AUDIO_MESSAGE, GEO
}

View File

@ -0,0 +1,81 @@
package org.sadtech.bot.core.domain.attachment;
import org.sadtech.bot.core.domain.content.GeoCoordinate;
import java.util.Objects;
public class Geo extends Attachment {
private GeoCoordinate geoCoordinate;
private String country;
private String city;
private Geo() {
type = AttachmentType.GEO;
}
public GeoCoordinate getGeoCoordinate() {
return geoCoordinate;
}
public String getCountry() {
return country;
}
public String getCity() {
return city;
}
public static Builder builder() {
return new Geo().new Builder();
}
public class Builder {
private Builder() {
}
public Builder coordinate(Float lat, Float aLong) {
Geo.this.geoCoordinate = new GeoCoordinate(lat, aLong);
return this;
}
public Builder country(String countryName) {
Geo.this.country = countryName;
return this;
}
public Builder city(String cityName) {
Geo.this.city = cityName;
return this;
}
public Geo build() {
return Geo.this;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Geo)) return false;
Geo geo = (Geo) o;
return Objects.equals(geoCoordinate, geo.geoCoordinate) &&
Objects.equals(country, geo.country) &&
Objects.equals(city, geo.city);
}
@Override
public int hashCode() {
return Objects.hash(geoCoordinate, country, city);
}
@Override
public String toString() {
return "Geo{" +
"geoCoordinate=" + geoCoordinate +
", country='" + country + '\'' +
", city='" + city + '\'' +
'}';
}
}

View File

@ -1,11 +1,9 @@
package org.sadtech.bot.core.domain;
package org.sadtech.bot.core.domain.content;
import java.util.Objects;
public abstract class Content {
public static final EmptyContent EMPTY_CONTENT = new EmptyContent();
private Integer personId;
private String message;

View File

@ -0,0 +1,28 @@
package org.sadtech.bot.core.domain.content;
public class GeoCoordinate {
private Float latitude;
private Float longitude;
public GeoCoordinate(Float latitude, Float longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public Float getLatitude() {
return latitude;
}
public void setLatitude(Float latitude) {
this.latitude = latitude;
}
public Float getLongitude() {
return longitude;
}
public void setLongitude(Float longitude) {
this.longitude = longitude;
}
}

View File

@ -1,4 +1,4 @@
package org.sadtech.bot.core.domain;
package org.sadtech.bot.core.domain.content;
import org.sadtech.bot.core.domain.attachment.Attachment;

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class KeyBoard {
@ -45,4 +46,26 @@ public class KeyBoard {
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof KeyBoard)) return false;
KeyBoard keyBoard = (KeyBoard) o;
return oneTime == keyBoard.oneTime &&
Objects.equals(keyBoardLines, keyBoard.keyBoardLines);
}
@Override
public int hashCode() {
return Objects.hash(keyBoardLines, oneTime);
}
@Override
public String toString() {
return "KeyBoard{" +
"keyBoardLines=" + keyBoardLines +
", oneTime=" + oneTime +
'}';
}
}

View File

@ -1,7 +1,5 @@
package org.sadtech.bot.core.domain.keyboard;
import com.google.gson.JsonObject;
import java.util.Objects;
public class KeyBoardButton {
@ -10,7 +8,7 @@ public class KeyBoardButton {
private String label;
private ButtonColor color = ButtonColor.DEFAULT;
public KeyBoardButton() {
private KeyBoardButton() {
}
@ -26,13 +24,6 @@ public class KeyBoardButton {
return color;
}
private JsonObject generateAction() {
JsonObject action = new JsonObject();
action.addProperty("payload", payload);
action.addProperty("label", label);
return action;
}
public static Builder builder() {
return new KeyBoardButton().new Builder();
}
@ -68,14 +59,23 @@ public class KeyBoardButton {
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof KeyBoardButton)) return false;
KeyBoardButton that = (KeyBoardButton) o;
return Objects.equals(payload, that.payload) &&
Objects.equals(label, that.label) &&
color == that.color;
KeyBoardButton button = (KeyBoardButton) o;
return Objects.equals(payload, button.payload) &&
Objects.equals(label, button.label) &&
color == button.color;
}
@Override
public int hashCode() {
return Objects.hash(payload, label, color);
}
@Override
public String toString() {
return "KeyBoardButton{" +
"payload='" + payload + '\'' +
", label='" + label + '\'' +
", color=" + color +
'}';
}
}

View File

@ -2,19 +2,16 @@ package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class KeyBoardLine {
private List<KeyBoardButton> keyBoardButtons = new ArrayList<>();
public KeyBoardLine() {
private KeyBoardLine() {
}
public KeyBoardLine(List<KeyBoardButton> keyBoardButtons) {
this.keyBoardButtons = keyBoardButtons;
}
public List<KeyBoardButton> getKeyBoardButtons() {
return keyBoardButtons;
}
@ -38,4 +35,24 @@ public class KeyBoardLine {
return KeyBoardLine.this;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof KeyBoardLine)) return false;
KeyBoardLine that = (KeyBoardLine) o;
return Objects.equals(keyBoardButtons, that.keyBoardButtons);
}
@Override
public int hashCode() {
return Objects.hash(keyBoardButtons);
}
@Override
public String toString() {
return "KeyBoardLine{" +
"keyBoardButtons=" + keyBoardButtons +
'}';
}
}

View File

@ -67,12 +67,25 @@ public class Account {
return Objects.equals(id, account.id) &&
Objects.equals(totalSum, account.totalSum) &&
Objects.equals(belongsPersonId, account.belongsPersonId) &&
Objects.equals(extinguishedPersonId, account.extinguishedPersonId) &&
Objects.equals(description, account.description) &&
accountStatus == account.accountStatus;
}
@Override
public int hashCode() {
return Objects.hash(id, totalSum, belongsPersonId, description, accountStatus);
return Objects.hash(id, totalSum, belongsPersonId, extinguishedPersonId, description, accountStatus);
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", totalSum=" + totalSum +
", belongsPersonId=" + belongsPersonId +
", extinguishedPersonId=" + extinguishedPersonId +
", description='" + description + '\'' +
", accountStatus=" + accountStatus +
'}';
}
}

View File

@ -4,23 +4,18 @@ import java.time.LocalDateTime;
public class AppBotException extends RuntimeException {
private final String type = "ERROR";
private static final String TYPE = "ERROR";
private final LocalDateTime timeError = LocalDateTime.now();
protected Integer code;
protected String description;
protected final Integer code;
protected final String description;
public AppBotException(String message, Integer code) {
super(message);
public AppBotException(Integer code, String message) {
this.description = message;
this.code = code;
}
public AppBotException(Integer code) {
this.code = code;
}
public String getType() {
return type;
return TYPE;
}
public LocalDateTime getTimeError() {

View File

@ -2,9 +2,8 @@ package org.sadtech.bot.core.exception;
public class MailSendException extends AppBotException {
public final static Integer CODE = 1;
public MailSendException() {
super(CODE);
super(1, "Ошибка отправки сообщения");
}
}

View File

@ -2,12 +2,8 @@ package org.sadtech.bot.core.exception;
public class PaymentException extends AppBotException {
public PaymentException(String message, Integer code) {
super(message, code);
}
public PaymentException(Integer code) {
super(code);
public PaymentException(Integer code, String message) {
super(code, message);
}
}

View File

@ -1,9 +0,0 @@
package org.sadtech.bot.core.filter;
import org.sadtech.bot.core.domain.Content;
public interface Filter<T extends Content> {
void doFilter(T content);
}

View File

@ -1,6 +1,6 @@
package org.sadtech.bot.core.repository;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.domain.content.Mail;
import java.time.LocalDateTime;
import java.util.List;

View File

@ -1,11 +0,0 @@
package org.sadtech.bot.core.repository;
import org.sadtech.bot.core.domain.Person;
public interface PersonRepository {
void add(Person person);
Person get(Integer id);
}

View File

@ -6,7 +6,7 @@ import org.sadtech.bot.core.repository.AccountRepository;
import java.util.HashMap;
import java.util.Map;
public class AccountRepositoryImpl implements AccountRepository {
public class AccountRepositoryMap implements AccountRepository {
private final Map<Integer, Account> saveMap = new HashMap<>();
private Integer id = 0;

View File

@ -1,6 +1,6 @@
package org.sadtech.bot.core.repository.impl;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.domain.content.Mail;
import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.bot.core.repository.MailRepository;

View File

@ -1,35 +0,0 @@
package org.sadtech.bot.core.repository.impl;
import org.sadtech.bot.core.domain.Person;
import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.bot.core.repository.PersonRepository;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class PersonRepositoryMap implements EventRepository<Person>, PersonRepository {
private final Map<Integer, Person> personMap = new HashMap<>();
@Override
public void add(Person person) {
personMap.put(person.getId(), person);
}
@Override
public void cleanAll() {
personMap.clear();
}
@Override
public Queue<Person> getEventQueue() {
return new ConcurrentLinkedQueue<>(personMap.values());
}
@Override
public Person get(Integer id) {
return personMap.get(id);
}
}

View File

@ -1,6 +1,6 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.domain.content.Mail;
public interface MailService extends EventService<Mail> {

View File

@ -1,15 +0,0 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.Person;
public interface PersonService {
void add(Person person);
Person get(Integer id);
boolean checkPerson(Integer personId);
Person createPerson(Integer userId);
}

View File

@ -0,0 +1,9 @@
package org.sadtech.bot.core.service.filter;
import org.sadtech.bot.core.domain.content.Content;
public interface Filter<T extends Content> {
void processing(T content);
}

View File

@ -30,7 +30,7 @@ public class AccountServiceImpl implements AccountService {
} else {
account.setAccountStatus(AccountStatus.EXCEPTION);
accountRepository.edit(accountId, account);
throw new PaymentException("Неверная сумма", 2);
throw new PaymentException(2, "Неверная сумма");
}
return true;
}

View File

@ -1,8 +1,7 @@
package org.sadtech.bot.core.service.impl;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.domain.content.Mail;
import org.sadtech.bot.core.repository.MailRepository;
import org.sadtech.bot.core.repository.impl.MailRepositoryList;
import org.sadtech.bot.core.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -19,10 +18,6 @@ public class MailServiceImpl implements MailService {
private final MailRepository mailRepository;
public MailServiceImpl() {
this.mailRepository = new MailRepositoryList();
}
public MailServiceImpl(MailRepository mailRepository) {
this.mailRepository = mailRepository;
}
@ -30,13 +25,12 @@ public class MailServiceImpl implements MailService {
@Override
public void add(Mail mail) {
mailRepository.add(mail);
log.info("Сообщение добавлено в репозиторий");
log.info(mail.toString());
log.info("Сообщение добавлено в репозиторий | {}", mail);
}
@Override
public List<Mail> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены сообщения " + timeFrom + " - " + timeTo);
log.info("Запрошены сообщения {} - {} ", timeFrom, timeTo);
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
@ -65,7 +59,7 @@ public class MailServiceImpl implements MailService {
@Override
public List<Mail> getEvent(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
log.info("Запрошены сообщения {} - {} ", timeFrom, timeTo);
return mailRepository.getMailByTime(timeFrom, timeTo);
}

View File

@ -23,6 +23,7 @@ public class EmailSent implements Sent {
@Override
public void send(Integer personId, String htmlText) {
Session session = Session.getDefaultInstance(emailConfig.getProps(), new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailConfig.getUsername(), emailConfig.getPassword());
}
@ -42,6 +43,6 @@ public class EmailSent implements Sent {
@Override
public void send(Integer personId, BoxAnswer boxAnswer) {
throw new MailSendException();
}
}

View File

@ -0,0 +1,14 @@
package org.sadtech.bot.core.utils;
import org.sadtech.bot.core.domain.EmptyContent;
public class Contents {
private Contents() {
throw new IllegalStateException("Utility class");
}
public static final EmptyContent EMPTY_CONTENT = new EmptyContent();
}