Архитектурные решения
* Добавлен новый слой логики приложения. Модуль обращается к этому слою, и только этот слой может обращаться к БД * Начал связывать таблицы * Переместил классы
This commit is contained in:
parent
30f4c09bbd
commit
5177fee391
@ -6,16 +6,18 @@ import javax.persistence.*;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
public class Messages {
|
public class Message {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long idMess;
|
private Long idMess;
|
||||||
private Long idUser;
|
|
||||||
private String text;
|
private String text;
|
||||||
private Long date;
|
private Long date;
|
||||||
@Enumerated(EnumType.STRING)
|
private String sourceMessage;
|
||||||
private SourceMessage source;
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "idUser", nullable = false)
|
||||||
|
private Person user;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.sadtech.consultant.database.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(length = 20)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(length = 20)
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
@Column(length = 20)
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
@CollectionTable(name="PersonSocialNetworks")
|
||||||
|
@MapKeyColumn(name="KeysSocialNetworks", length = 20)
|
||||||
|
private Map<String, Integer> socialNetworks = new HashMap<>();
|
||||||
|
|
||||||
|
}
|
@ -4,6 +4,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package org.sadtech.consultant.database.entity;
|
|
||||||
|
|
||||||
public enum SourceMessage {
|
|
||||||
VK, FACEBOOK;
|
|
||||||
}
|
|
@ -4,6 +4,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
package org.sadtech.consultant.database.entity;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Data
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
private Long id;
|
|
||||||
private String name;
|
|
||||||
private String token;
|
|
||||||
private String lastName;
|
|
||||||
private String city;
|
|
||||||
|
|
||||||
@ElementCollection
|
|
||||||
@CollectionTable(name="SocialNetworks", joinColumns=@JoinColumn(name="id"))
|
|
||||||
private List<String> socialNetworks;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,16 +1,15 @@
|
|||||||
package org.sadtech.consultant.database.repository;
|
package org.sadtech.consultant.database.repository;
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.Messages;
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface MessageRepository extends JpaRepository<Messages, Long> {
|
public interface MessageRepository extends JpaRepository<Message, Long> {
|
||||||
|
|
||||||
@Query("SELECT u FROM Messages u WHERE u.date > :date")
|
@Query("SELECT u FROM Message u WHERE u.date > :date")
|
||||||
Collection<Messages> getMessagesByRange(@Param("date") Long date);
|
Collection<Message> getMessagesByRange(@Param("date") Long date);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package org.sadtech.consultant.database.repository;
|
||||||
|
|
||||||
|
import org.sadtech.consultant.database.entity.Person;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
public interface PersonRepository extends JpaRepository<Person, Long> {
|
||||||
|
|
||||||
|
@Query("SELECT u FROM Person u INNER JOIN u.socialNetworks p WHERE KEY(p) = :type2 AND VALUE(p) = :socialId")
|
||||||
|
Person getUserBySocialNetworksId(@Param("type2") String type, @Param("socialId") Integer socialNetworksId);
|
||||||
|
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
package org.sadtech.consultant.database.repository;
|
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.User;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface UserRepositoriy extends JpaRepository<User, Long> {
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +1,12 @@
|
|||||||
package org.sadtech.consultant.database.service;
|
package org.sadtech.consultant.database.service;
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.Messages;
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface MessageService {
|
public interface MessageService {
|
||||||
|
|
||||||
void addMessage(Messages message);
|
void addMessage(Message message);
|
||||||
|
|
||||||
List<Messages> getMessageRange(Long date);
|
List<Message> getMessageRange(Long date);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,14 @@
|
|||||||
|
package org.sadtech.consultant.database.service;
|
||||||
|
|
||||||
|
import org.sadtech.consultant.database.entity.Person;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface PersonService {
|
||||||
|
|
||||||
|
void addUser(Person user);
|
||||||
|
void removeUser(Long id);
|
||||||
|
Person getUserByID(Long id);
|
||||||
|
Map<String, Integer> getSocialNetwork(Long id);
|
||||||
|
Person getUserBySocialNetworksId(String type, Integer socialNetworksId);
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package org.sadtech.consultant.database.service;
|
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.User;
|
|
||||||
|
|
||||||
public interface UserService {
|
|
||||||
|
|
||||||
void addUser(User user);
|
|
||||||
void removeUser(Long id);
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
package org.sadtech.consultant.database.service.impl;
|
package org.sadtech.consultant.database.service.impl;
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.Messages;
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
import org.sadtech.consultant.database.repository.MessageRepository;
|
import org.sadtech.consultant.database.repository.MessageRepository;
|
||||||
import org.sadtech.consultant.database.service.MessageService;
|
import org.sadtech.consultant.database.service.MessageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -14,12 +14,12 @@ public class MessageServiceImpl implements MessageService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MessageRepository repository;
|
private MessageRepository repository;
|
||||||
|
|
||||||
public void addMessage(Messages message) {
|
public void addMessage(Message message) {
|
||||||
repository.saveAndFlush(message);
|
repository.saveAndFlush(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Messages> getMessageRange(Long date) {
|
public List<Message> getMessageRange(Long date) {
|
||||||
return (List<Messages>) repository.getMessagesByRange(date);
|
return (List<Message>) repository.getMessagesByRange(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package org.sadtech.consultant.database.service.impl;
|
||||||
|
|
||||||
|
import org.sadtech.consultant.database.entity.Person;
|
||||||
|
import org.sadtech.consultant.database.repository.PersonRepository;
|
||||||
|
import org.sadtech.consultant.database.service.PersonService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PersonServiceImpl implements PersonService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PersonRepository repositoriy;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addUser(Person user) {
|
||||||
|
repositoriy.saveAndFlush(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeUser(Long id) {
|
||||||
|
repositoriy.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Person getUserByID(Long id) {
|
||||||
|
return repositoriy.getOne(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Integer> getSocialNetwork(Long id) {
|
||||||
|
return repositoriy.getOne(id).getSocialNetworks();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Person getUserBySocialNetworksId(String type, Integer socialNetworksId) {
|
||||||
|
return repositoriy.getUserBySocialNetworksId(type, socialNetworksId);
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
package org.sadtech.consultant.database.service.impl;
|
|
||||||
|
|
||||||
import org.sadtech.consultant.database.entity.User;
|
|
||||||
import org.sadtech.consultant.database.repository.UserRepositoriy;
|
|
||||||
import org.sadtech.consultant.database.service.UserService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UserServiceImpl implements UserService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserRepositoriy repositoriy;
|
|
||||||
|
|
||||||
public void addUser(User user) {
|
|
||||||
//@TODO: Написть добавление профиля в бд
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeUser(Long id) {
|
|
||||||
//@TODO: Написать удаление профиля из бд
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,7 @@
|
|||||||
package org.sadtech.consultant.processing;
|
package org.sadtech.consultant.processing;
|
||||||
|
|
||||||
import lombok.extern.log4j.Log4j;
|
import lombok.extern.log4j.Log4j;
|
||||||
import org.sadtech.consultant.database.entity.Messages;
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
import org.sadtech.consultant.database.service.MessageService;
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@ -13,27 +12,26 @@ import java.util.List;
|
|||||||
@Component
|
@Component
|
||||||
public class MessageHandler {
|
public class MessageHandler {
|
||||||
|
|
||||||
private MessageService service;
|
private MessageLogicService messageLogicService;
|
||||||
|
private PersonLogicService userLogicService;
|
||||||
private long data;
|
private long data;
|
||||||
|
|
||||||
public MessageHandler(MessageService service) {
|
public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) {
|
||||||
this.service = service;
|
this.messageLogicService = messageLogicService;
|
||||||
|
this.userLogicService = userLogicService;
|
||||||
data = new Date().getTime() / 1000;
|
data = new Date().getTime() / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public void processing() {
|
public void processing() {
|
||||||
while (true) {
|
while (true) {
|
||||||
List<Messages> messages = service.getMessageRange(data);
|
List<Message> messages = messageLogicService.getMessageRange(data);
|
||||||
for (Messages message : messages) {
|
for (Message message : messages) {
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
|
|
||||||
|
|
||||||
data = message.getDate();
|
data = message.getDate();
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
Thread.sleep(10000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package org.sadtech.consultant.processing;
|
||||||
|
|
||||||
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
|
import org.sadtech.consultant.database.service.MessageService;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MessageLogicService {
|
||||||
|
|
||||||
|
private MessageService messageService;
|
||||||
|
|
||||||
|
public MessageLogicService(MessageService messageService) {
|
||||||
|
this.messageService = messageService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMessage(Message message) {
|
||||||
|
messageService.addMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Message> getMessageRange(long data) {
|
||||||
|
return messageService.getMessageRange(data);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package org.sadtech.consultant.processing;
|
||||||
|
|
||||||
|
import org.sadtech.consultant.database.entity.Person;
|
||||||
|
import org.sadtech.consultant.database.service.PersonService;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PersonLogicService {
|
||||||
|
|
||||||
|
private PersonService personService;
|
||||||
|
|
||||||
|
public PersonLogicService(PersonService userService) {
|
||||||
|
this.personService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addUser(Person user) {
|
||||||
|
personService.addUser(user);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getUserById(Long id) {
|
||||||
|
return personService.getUserByID(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getUserBySocialId(String type, Integer id) {
|
||||||
|
return personService.getUserBySocialNetworksId(type, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,8 +23,8 @@ public class Main {
|
|||||||
eventListener.listen();
|
eventListener.listen();
|
||||||
Handled dispetcherHandler = context.getBean(Handled.class);
|
Handled dispetcherHandler = context.getBean(Handled.class);
|
||||||
dispetcherHandler.sortAndSend();
|
dispetcherHandler.sortAndSend();
|
||||||
MessageHandler messageHandler = context.getBean(MessageHandler.class);
|
// MessageHandler messageHandler = context.getBean(MessageHandler.class);
|
||||||
messageHandler.processing();
|
// messageHandler.processing();
|
||||||
|
|
||||||
log.info("\n\n=== Конец программы ===\n\n");
|
log.info("\n\n=== Конец программы ===\n\n");
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package org.sadtech.vkbot;
|
||||||
|
|
||||||
|
public enum SourceMessage {
|
||||||
|
VK, FB
|
||||||
|
}
|
@ -2,25 +2,26 @@ package org.sadtech.vkbot.handlers.impl;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.vk.api.sdk.objects.messages.Message;
|
|
||||||
import lombok.extern.log4j.Log4j;
|
import lombok.extern.log4j.Log4j;
|
||||||
import org.sadtech.consultant.database.entity.Messages;
|
import org.sadtech.consultant.database.entity.Person;
|
||||||
import org.sadtech.consultant.database.entity.SourceMessage;
|
import org.sadtech.consultant.processing.MessageLogicService;
|
||||||
import org.sadtech.consultant.database.service.MessageService;
|
import org.sadtech.consultant.processing.PersonLogicService;
|
||||||
import org.sadtech.vkbot.VkApi;
|
import org.sadtech.consultant.database.entity.Message;
|
||||||
|
import org.sadtech.vkbot.SourceMessage;
|
||||||
import org.sadtech.vkbot.listener.Observable;
|
import org.sadtech.vkbot.listener.Observable;
|
||||||
import org.sadtech.vkbot.listener.Observer;
|
import org.sadtech.vkbot.listener.Observer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Log4j
|
@Log4j
|
||||||
@Component
|
@Component
|
||||||
|
|
||||||
public class MessageHandlerVk implements Observer {
|
public class MessageHandlerVk implements Observer {
|
||||||
|
|
||||||
private MessageService service;
|
private MessageLogicService messageLogicService;
|
||||||
|
private PersonLogicService userLogicService;
|
||||||
|
|
||||||
public MessageHandlerVk(Observable dispetcherHandler, MessageService messageService) {
|
public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService userLogicService, MessageLogicService messageLogicService) {
|
||||||
this.service = messageService;
|
this.userLogicService = userLogicService;
|
||||||
|
this.messageLogicService = messageLogicService;
|
||||||
dispetcherHandler.registerObserver(this);
|
dispetcherHandler.registerObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,19 +29,28 @@ public class MessageHandlerVk implements Observer {
|
|||||||
public void update(JsonObject object) {
|
public void update(JsonObject object) {
|
||||||
if (object.get("type").toString().equals("\"message_new\"")) {
|
if (object.get("type").toString().equals("\"message_new\"")) {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Message message = gson.fromJson(object.getAsJsonObject("object"), Message.class);
|
com.vk.api.sdk.objects.messages.Message message = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class);
|
||||||
send(message);
|
sendProcessing(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(Message userMessage) {
|
private void sendProcessing(com.vk.api.sdk.objects.messages.Message userMessage) {
|
||||||
log.info(userMessage.getBody());
|
log.info(userMessage.getBody());
|
||||||
Messages message = new Messages();
|
Message message = new Message();
|
||||||
message.setIdUser(Long.valueOf(userMessage.getUserId()));
|
// Person user = userLogicService.getUserBySocialId(SourceMessage.VK.name(), Long.valueOf(userMessage.getUserId()));
|
||||||
|
Person user;
|
||||||
|
user = new Person();
|
||||||
|
user.getSocialNetworks().put(SourceMessage.VK.name(), userMessage.getUserId());
|
||||||
|
user.setName("Name");
|
||||||
|
user.setLastName("LastName");
|
||||||
|
user.setCity("City");
|
||||||
|
userLogicService.addUser(user);
|
||||||
|
log.info(user);
|
||||||
|
message.setUser(user);
|
||||||
message.setText(userMessage.getBody());
|
message.setText(userMessage.getBody());
|
||||||
message.setDate(Long.valueOf(userMessage.getDate()));
|
message.setDate(Long.valueOf(userMessage.getDate()));
|
||||||
message.setSource(SourceMessage.VK);
|
message.setSourceMessage(SourceMessage.VK.name());
|
||||||
VkApi.sendMessage(userMessage.getUserId(), "Здравствуйте, " + VkApi.getUserName(userMessage.getUserId()) + "!\nВаше сообщение получено!\n");
|
log.info(message);
|
||||||
service.addMessage(message);
|
messageLogicService.addMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user