From 0090ab90345daa8228c8cf848052283ef4dba54e Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Tue, 8 Jan 2019 23:39:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D0=BD=D1=8B=D0=B5=20=D1=80=D0=B5=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Добавлен новый слой логики приложения. Модуль обращается к этому слою, и только этот слой может обращаться к БД * Начал связывать таблицы * Переместил классы --- .../entity/{Messages.java => Message.java} | 10 +++-- .../consultant/database/entity/NextUnit.java | 1 + .../consultant/database/entity/Person.java | 33 +++++++++++++++ .../consultant/database/entity/SaveUnit.java | 1 + .../database/entity/SourceMessage.java | 5 --- .../consultant/database/entity/Unit.java | 1 + .../consultant/database/entity/User.java | 24 ----------- .../repository/MessageRepository.java | 9 ++-- .../database/repository/PersonRepository.java | 13 ++++++ .../database/repository/UserRepositoriy.java | 8 ---- .../database/service/MessageService.java | 6 +-- .../database/service/PersonService.java | 14 +++++++ .../database/service/UserService.java | 10 ----- .../service/impl/MessageServiceImpl.java | 8 ++-- .../service/impl/PersonServiceImpl.java | 41 +++++++++++++++++++ .../service/impl/UserServiceImpl.java | 22 ---------- .../consultant/processing/MessageHandler.java | 22 +++++----- .../processing/MessageLogicService.java | 25 +++++++++++ .../processing/PersonLogicService.java | 29 +++++++++++++ 19 files changed, 185 insertions(+), 97 deletions(-) rename src/main/java/org/sadtech/consultant/database/entity/{Messages.java => Message.java} (59%) create mode 100644 src/main/java/org/sadtech/consultant/database/entity/Person.java delete mode 100644 src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java delete mode 100644 src/main/java/org/sadtech/consultant/database/entity/User.java create mode 100644 src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java delete mode 100644 src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java create mode 100644 src/main/java/org/sadtech/consultant/database/service/PersonService.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/UserService.java create mode 100644 src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java create mode 100644 src/main/java/org/sadtech/consultant/processing/MessageLogicService.java create mode 100644 src/main/java/org/sadtech/consultant/processing/PersonLogicService.java diff --git a/src/main/java/org/sadtech/consultant/database/entity/Messages.java b/src/main/java/org/sadtech/consultant/database/entity/Message.java similarity index 59% rename from src/main/java/org/sadtech/consultant/database/entity/Messages.java rename to src/main/java/org/sadtech/consultant/database/entity/Message.java index f752173..b1d0ff3 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/Messages.java +++ b/src/main/java/org/sadtech/consultant/database/entity/Message.java @@ -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; } diff --git a/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java b/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java index 64281d3..5e896fe 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java +++ b/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data diff --git a/src/main/java/org/sadtech/consultant/database/entity/Person.java b/src/main/java/org/sadtech/consultant/database/entity/Person.java new file mode 100644 index 0000000..5a3e580 --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/entity/Person.java @@ -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 socialNetworks = new HashMap<>(); + +} diff --git a/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java b/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java index b5df08e..68c696c 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java +++ b/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data diff --git a/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java b/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java deleted file mode 100644 index df6497a..0000000 --- a/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.sadtech.consultant.database.entity; - -public enum SourceMessage { - VK, FACEBOOK; -} diff --git a/src/main/java/org/sadtech/consultant/database/entity/Unit.java b/src/main/java/org/sadtech/consultant/database/entity/Unit.java index a267686..247df8b 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/Unit.java +++ b/src/main/java/org/sadtech/consultant/database/entity/Unit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data diff --git a/src/main/java/org/sadtech/consultant/database/entity/User.java b/src/main/java/org/sadtech/consultant/database/entity/User.java deleted file mode 100644 index 17267fa..0000000 --- a/src/main/java/org/sadtech/consultant/database/entity/User.java +++ /dev/null @@ -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 socialNetworks; - - -} diff --git a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java b/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java index cedfccb..8dfc122 100644 --- a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java +++ b/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java @@ -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 { +public interface MessageRepository extends JpaRepository { - @Query("SELECT u FROM Messages u WHERE u.date > :date") - Collection getMessagesByRange(@Param("date") Long date); + @Query("SELECT u FROM Message u WHERE u.date > :date") + Collection getMessagesByRange(@Param("date") Long date); } diff --git a/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java b/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java new file mode 100644 index 0000000..a00fa83 --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java @@ -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 { + + @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); + +} diff --git a/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java b/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java deleted file mode 100644 index f92f0d1..0000000 --- a/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java +++ /dev/null @@ -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 { - -} diff --git a/src/main/java/org/sadtech/consultant/database/service/MessageService.java b/src/main/java/org/sadtech/consultant/database/service/MessageService.java index 9aa136e..53e2d37 100644 --- a/src/main/java/org/sadtech/consultant/database/service/MessageService.java +++ b/src/main/java/org/sadtech/consultant/database/service/MessageService.java @@ -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 getMessageRange(Long date); + List getMessageRange(Long date); } diff --git a/src/main/java/org/sadtech/consultant/database/service/PersonService.java b/src/main/java/org/sadtech/consultant/database/service/PersonService.java new file mode 100644 index 0000000..db476ea --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/service/PersonService.java @@ -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 getSocialNetwork(Long id); + Person getUserBySocialNetworksId(String type, Integer socialNetworksId); +} diff --git a/src/main/java/org/sadtech/consultant/database/service/UserService.java b/src/main/java/org/sadtech/consultant/database/service/UserService.java deleted file mode 100644 index 3e81573..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/UserService.java +++ /dev/null @@ -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); - -} diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java index 23f6d65..f5d967b 100644 --- a/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java +++ b/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java @@ -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 getMessageRange(Long date) { - return (List) repository.getMessagesByRange(date); + public List getMessageRange(Long date) { + return (List) repository.getMessagesByRange(date); } } diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java new file mode 100644 index 0000000..7acd025 --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java @@ -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 getSocialNetwork(Long id) { + return repositoriy.getOne(id).getSocialNetworks(); + } + + @Override + public Person getUserBySocialNetworksId(String type, Integer socialNetworksId) { + return repositoriy.getUserBySocialNetworksId(type, socialNetworksId); + } +} diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java deleted file mode 100644 index fe6c606..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java +++ /dev/null @@ -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: Написать удаление профиля из бд - } -} diff --git a/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java index fdd84a8..232b33e 100644 --- a/src/main/java/org/sadtech/consultant/processing/MessageHandler.java +++ b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java @@ -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 = service.getMessageRange(data); - for (Messages message : messages) { + List messages = messageLogicService.getMessageRange(data); + for (Message message : messages) { System.out.println(message); + + data = message.getDate(); } - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } } } diff --git a/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java b/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java new file mode 100644 index 0000000..1ed910a --- /dev/null +++ b/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java @@ -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 getMessageRange(long data) { + return messageService.getMessageRange(data); + } +} diff --git a/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java b/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java new file mode 100644 index 0000000..28c672c --- /dev/null +++ b/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java @@ -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); + } + +}