Архитектурные решения
* Добавлен новый слой логики приложения. Модуль обращается к этому слою, и только этот слой может обращаться к БД * Начал связывать таблицы * Переместил классы
This commit is contained in:
parent
30f4c09bbd
commit
5177fee391
@ -6,16 +6,18 @@ import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class Messages {
|
||||
public class Message {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long idMess;
|
||||
private Long idUser;
|
||||
private String text;
|
||||
private Long date;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private SourceMessage source;
|
||||
private String sourceMessage;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "idUser", nullable = false)
|
||||
private Person user;
|
||||
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import lombok.Data;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@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.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@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.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@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;
|
||||
|
||||
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.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
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")
|
||||
Collection<Messages> getMessagesByRange(@Param("date") Long date);
|
||||
@Query("SELECT u FROM Message u WHERE u.date > :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;
|
||||
|
||||
import org.sadtech.consultant.database.entity.Messages;
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
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.service.MessageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -14,12 +14,12 @@ public class MessageServiceImpl implements MessageService {
|
||||
@Autowired
|
||||
private MessageRepository repository;
|
||||
|
||||
public void addMessage(Messages message) {
|
||||
public void addMessage(Message message) {
|
||||
repository.saveAndFlush(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Messages> getMessageRange(Long date) {
|
||||
return (List<Messages>) repository.getMessagesByRange(date);
|
||||
public List<Message> getMessageRange(Long 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;
|
||||
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.sadtech.consultant.database.entity.Messages;
|
||||
import org.sadtech.consultant.database.service.MessageService;
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -13,27 +12,26 @@ import java.util.List;
|
||||
@Component
|
||||
public class MessageHandler {
|
||||
|
||||
private MessageService service;
|
||||
private MessageLogicService messageLogicService;
|
||||
private PersonLogicService userLogicService;
|
||||
private long data;
|
||||
|
||||
public MessageHandler(MessageService service) {
|
||||
this.service = service;
|
||||
public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) {
|
||||
this.messageLogicService = messageLogicService;
|
||||
this.userLogicService = userLogicService;
|
||||
data = new Date().getTime() / 1000;
|
||||
}
|
||||
|
||||
@Async
|
||||
public void processing() {
|
||||
while (true) {
|
||||
List<Messages> messages = service.getMessageRange(data);
|
||||
for (Messages message : messages) {
|
||||
List<Message> messages = messageLogicService.getMessageRange(data);
|
||||
for (Message message : messages) {
|
||||
System.out.println(message);
|
||||
|
||||
|
||||
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();
|
||||
Handled dispetcherHandler = context.getBean(Handled.class);
|
||||
dispetcherHandler.sortAndSend();
|
||||
MessageHandler messageHandler = context.getBean(MessageHandler.class);
|
||||
messageHandler.processing();
|
||||
// MessageHandler messageHandler = context.getBean(MessageHandler.class);
|
||||
// messageHandler.processing();
|
||||
|
||||
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.JsonObject;
|
||||
import com.vk.api.sdk.objects.messages.Message;
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.sadtech.consultant.database.entity.Messages;
|
||||
import org.sadtech.consultant.database.entity.SourceMessage;
|
||||
import org.sadtech.consultant.database.service.MessageService;
|
||||
import org.sadtech.vkbot.VkApi;
|
||||
import org.sadtech.consultant.database.entity.Person;
|
||||
import org.sadtech.consultant.processing.MessageLogicService;
|
||||
import org.sadtech.consultant.processing.PersonLogicService;
|
||||
import org.sadtech.consultant.database.entity.Message;
|
||||
import org.sadtech.vkbot.SourceMessage;
|
||||
import org.sadtech.vkbot.listener.Observable;
|
||||
import org.sadtech.vkbot.listener.Observer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Log4j
|
||||
@Component
|
||||
|
||||
public class MessageHandlerVk implements Observer {
|
||||
|
||||
private MessageService service;
|
||||
private MessageLogicService messageLogicService;
|
||||
private PersonLogicService userLogicService;
|
||||
|
||||
public MessageHandlerVk(Observable dispetcherHandler, MessageService messageService) {
|
||||
this.service = messageService;
|
||||
public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService userLogicService, MessageLogicService messageLogicService) {
|
||||
this.userLogicService = userLogicService;
|
||||
this.messageLogicService = messageLogicService;
|
||||
dispetcherHandler.registerObserver(this);
|
||||
}
|
||||
|
||||
@ -28,19 +29,28 @@ public class MessageHandlerVk implements Observer {
|
||||
public void update(JsonObject object) {
|
||||
if (object.get("type").toString().equals("\"message_new\"")) {
|
||||
Gson gson = new Gson();
|
||||
Message message = gson.fromJson(object.getAsJsonObject("object"), Message.class);
|
||||
send(message);
|
||||
com.vk.api.sdk.objects.messages.Message message = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class);
|
||||
sendProcessing(message);
|
||||
}
|
||||
}
|
||||
|
||||
private void send(Message userMessage) {
|
||||
private void sendProcessing(com.vk.api.sdk.objects.messages.Message userMessage) {
|
||||
log.info(userMessage.getBody());
|
||||
Messages message = new Messages();
|
||||
message.setIdUser(Long.valueOf(userMessage.getUserId()));
|
||||
Message message = new Message();
|
||||
// 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.setDate(Long.valueOf(userMessage.getDate()));
|
||||
message.setSource(SourceMessage.VK);
|
||||
VkApi.sendMessage(userMessage.getUserId(), "Здравствуйте, " + VkApi.getUserName(userMessage.getUserId()) + "!\nВаше сообщение получено!\n");
|
||||
service.addMessage(message);
|
||||
message.setSourceMessage(SourceMessage.VK.name());
|
||||
log.info(message);
|
||||
messageLogicService.addMessage(message);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user