Архитектурные решения

* Добавлен новый слой логики приложения. Модуль обращается к этому слою, и только этот слой может обращаться к БД
* Начал связывать таблицы
* Переместил классы
This commit is contained in:
Mark Struchkov 2019-01-08 23:39:00 +03:00
parent d6b902f2b9
commit 0090ab9034
19 changed files with 185 additions and 97 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +0,0 @@
package org.sadtech.consultant.database.entity;
public enum SourceMessage {
VK, FACEBOOK;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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: Написать удаление профиля из бд
}
}

View File

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

View File

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

View File

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