Работа с базой и перемещение файлов

This commit is contained in:
Mark Struchkov 2018-12-31 12:35:16 +03:00
parent 58d2e7d3bc
commit da8b5cba11
47 changed files with 200 additions and 209 deletions

View File

@ -1,8 +1,9 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@Data @Data
@ -10,9 +11,11 @@ import javax.persistence.Id;
public class Message { public class Message {
@Id @Id
@GeneratedValue
private Long idMess; private Long idMess;
private Long idUser; private Long idUser;
private String text; private String text;
private String date;
} }

View File

@ -1,13 +1,15 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id;
@Entity @Entity
@Data @Data
public class NextUnit { public class NextUnit {
@Id
private Long id; private Long id;
private Long idNext; private Long idNext;

View File

@ -1,4 +1,4 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;

View File

@ -1,9 +1,8 @@
package org.sadtech.consultant.entity; package org.sadtech.consultant.database.entity;
import lombok.Data; import lombok.Data;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import java.util.List; import java.util.List;
@ -17,7 +16,7 @@ public class User {
private String token; private String token;
private String lastName; private String lastName;
private String city; private String city;
private List<SocialNetworks> socialNetworks; // private List<SocialNetworks> socialNetworks;
} }

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.Message; import org.sadtech.consultant.database.entity.Message;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface MessageRepository extends JpaRepository<Message, Long> { public interface MessageRepository extends JpaRepository<Message, Long> {

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.NextUnit; import org.sadtech.consultant.database.entity.NextUnit;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface NextUnitRepositoriy extends JpaRepository<NextUnit, Long> { public interface NextUnitRepositoriy extends JpaRepository<NextUnit, Long> {

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.SaveUnit; import org.sadtech.consultant.database.entity.SaveUnit;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface SaveUnitRepositoriy extends JpaRepository<SaveUnit, Long> { public interface SaveUnitRepositoriy extends JpaRepository<SaveUnit, Long> {

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.SocialNetworks; import org.sadtech.consultant.database.entity.SocialNetworks;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface SocialNetworksRepositoriy extends JpaRepository<SocialNetworks, Long> { public interface SocialNetworksRepositoriy extends JpaRepository<SocialNetworks, Long> {

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.Unit; import org.sadtech.consultant.database.entity.Unit;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface UnitRepositoriy extends JpaRepository<Unit, Long> { public interface UnitRepositoriy extends JpaRepository<Unit, Long> {

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.repository; package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.entity.User; import org.sadtech.consultant.database.entity.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepositoriy extends JpaRepository<User, Long> { public interface UserRepositoriy extends JpaRepository<User, Long> {

View File

@ -0,0 +1,8 @@
package org.sadtech.consultant.database.service;
import org.sadtech.consultant.database.entity.Message;
public interface MessageService {
void addMessage(Message message);
}

View File

@ -0,0 +1,4 @@
package org.sadtech.consultant.database.service;
public interface NextUnitService {
}

View File

@ -0,0 +1,4 @@
package org.sadtech.consultant.database.service;
public interface SaveUnitService {
}

View File

@ -0,0 +1,4 @@
package org.sadtech.consultant.database.service;
public interface SocialNetworksService {
}

View File

@ -0,0 +1,10 @@
package org.sadtech.consultant.database.service;
import org.sadtech.consultant.database.entity.Unit;
public interface UnitService {
void addUnit(Unit unit);
void removeUnit(Long id);
}

View File

@ -0,0 +1,10 @@
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

@ -0,0 +1,18 @@
package org.sadtech.consultant.database.service.impl;
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;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageRepository repository;
public void addMessage(Message message) {
repository.saveAndFlush(message);
}
}

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.service.impl; package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.repository.NextUnitRepositoriy; import org.sadtech.consultant.database.repository.NextUnitRepositoriy;
import org.sadtech.consultant.service.NextUnitService; import org.sadtech.consultant.database.service.NextUnitService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.service.impl; package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.repository.SaveUnitRepositoriy; import org.sadtech.consultant.database.repository.SaveUnitRepositoriy;
import org.sadtech.consultant.service.SaveUnitService; import org.sadtech.consultant.database.service.SaveUnitService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.service.impl; package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.repository.SocialNetworksRepositoriy; import org.sadtech.consultant.database.repository.SocialNetworksRepositoriy;
import org.sadtech.consultant.service.SocialNetworksService; import org.sadtech.consultant.database.service.SocialNetworksService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,8 +1,8 @@
package org.sadtech.consultant.service.impl; package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.entity.Unit; import org.sadtech.consultant.database.entity.Unit;
import org.sadtech.consultant.repository.UnitRepositoriy; import org.sadtech.consultant.database.repository.UnitRepositoriy;
import org.sadtech.consultant.service.UnitService; import org.sadtech.consultant.database.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,8 +1,8 @@
package org.sadtech.consultant.service.impl; package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.entity.User; import org.sadtech.consultant.database.entity.User;
import org.sadtech.consultant.repository.UserRepositoriy; import org.sadtech.consultant.database.repository.UserRepositoriy;
import org.sadtech.consultant.service.UserService; import org.sadtech.consultant.database.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,8 +0,0 @@
package org.sadtech.consultant.service;
import org.sadtech.consultant.entity.Message;
public interface MessageService {
void addMessage(Message message);
}

View File

@ -1,4 +0,0 @@
package org.sadtech.consultant.service;
public interface NextUnitService {
}

View File

@ -1,4 +0,0 @@
package org.sadtech.consultant.service;
public interface SaveUnitService {
}

View File

@ -1,4 +0,0 @@
package org.sadtech.consultant.service;
public interface SocialNetworksService {
}

View File

@ -1,10 +0,0 @@
package org.sadtech.consultant.service;
import org.sadtech.consultant.entity.Unit;
public interface UnitService {
void addUnit(Unit unit);
void removeUnit(Long id);
}

View File

@ -1,10 +0,0 @@
package org.sadtech.consultant.service;
import org.sadtech.consultant.entity.User;
public interface UserService {
void addUser(User user);
void removeUser(Long id);
}

View File

@ -1,16 +0,0 @@
package org.sadtech.consultant.service.impl;
import org.sadtech.consultant.entity.Message;
import org.sadtech.consultant.repository.MessageRepository;
import org.sadtech.consultant.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageRepository repository;
public void addMessage(Message message) {
repository.saveAndFlush(message);
}
}

View File

@ -18,8 +18,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>6</source> <source>7</source>
<target>6</target> <target>7</target>
</configuration> </configuration>
</plugin> </plugin>
@ -45,7 +45,7 @@
<spring.data>2.1.3.RELEASE</spring.data> <spring.data>2.1.3.RELEASE</spring.data>
<javax.servlet>3.1.0</javax.servlet> <javax.servlet>3.1.0</javax.servlet>
<javax.persistance>1.0.2</javax.persistance> <javax.persistance>2.2</javax.persistance>
<hibernate.entitymanager>5.4.0.Final</hibernate.entitymanager> <hibernate.entitymanager>5.4.0.Final</hibernate.entitymanager>
<hibernate.core>5.2.10.Final</hibernate.core> <hibernate.core>5.2.10.Final</hibernate.core>
@ -81,6 +81,10 @@
<version>${log4j}</version> <version>${log4j}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
@ -111,6 +115,11 @@
<artifactId>spring-data-jpa</artifactId> <artifactId>spring-data-jpa</artifactId>
<version>${spring.data}</version> <version>${spring.data}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
@ -138,19 +147,21 @@
<dependency> <dependency>
<groupId>javax.persistence</groupId> <groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId> <artifactId>javax.persistence-api</artifactId>
<version>${javax.persistance}</version> <version>${javax.persistance}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector}</version> <version>${mysql.connector}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId> <artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.entitymanager}</version> <version>${hibernate.entitymanager}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
@ -158,6 +169,18 @@
<version>${hibernate.core}</version> <version>${hibernate.core}</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.sadtech</groupId>
<artifactId>consultant-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,21 +1,27 @@
package org.sadtech.vkbot.config; package org.sadtech.vkbot;
import lombok.extern.log4j.Log4j; import lombok.extern.log4j.Log4j;
import org.sadtech.vkbot.config.DataConfig;
import org.sadtech.vkbot.config.SpringConfigVk;
import org.sadtech.vkbot.listener.EventListenable; import org.sadtech.vkbot.listener.EventListenable;
import org.sadtech.vkbot.listener.Observable; import org.sadtech.vkbot.listener.handlers.Handled;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@Log4j @Log4j
public class TestMain { public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Main main = new Main();
main.run();
}
public void run() throws Exception {
log.info("\n\n\n\n=== Запуск прогарммы ===\n\n"); log.info("\n\n\n\n=== Запуск прогарммы ===\n\n");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class, DataConfig.class);
context.register(DataConfig.class);
EventListenable eventListener = context.getBean(EventListenable.class); EventListenable eventListener = context.getBean(EventListenable.class);
eventListener.listen(); eventListener.listen();
Observable dispetcherHandler = context.getBean(Observable.class); Handled dispetcherHandler = context.getBean(Handled.class);
dispetcherHandler.packaging(); dispetcherHandler.sortAndSend();
log.info("\n\n=== Конец программы ===\n\n"); log.info("\n\n=== Конец программы ===\n\n");
} }

View File

@ -4,8 +4,6 @@ import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -16,8 +14,7 @@ import java.util.Properties;
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaRepositories("org.sadtech.vkbot.repo")
@PropertySource("classpath:config.properties")
public class DataConfig { public class DataConfig {
@Value("${db.driver}") @Value("${db.driver}")
@ -38,7 +35,7 @@ public class DataConfig {
@Value("${db.hibernate.hbm2ddl.auto}") @Value("${db.hibernate.hbm2ddl.auto}")
private String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO; private String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO;
@Bean(name = "entityManagerFactory") @Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setDataSource(dataSource());

View File

@ -9,10 +9,7 @@ import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.httpclient.HttpTransportClient; import com.vk.api.sdk.httpclient.HttpTransportClient;
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@ -23,9 +20,9 @@ import java.util.concurrent.Executors;
@Configuration @Configuration
@EnableAsync @EnableAsync
@Import({DataConfig.class})
@PropertySource("classpath:config.properties") @PropertySource("classpath:config.properties")
@ComponentScan("org.sadtech.vkbot.listener") @ComponentScan("org.sadtech.vkbot.listener")
@EnableScheduling
public class SpringConfigVk { public class SpringConfigVk {
@Value("${vk.groupID}") @Value("${vk.groupID}")

View File

@ -1,16 +0,0 @@
package org.sadtech.vkbot.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class User {
@Id
private long id;
private String name;
}

View File

@ -6,5 +6,4 @@ public interface Observable {
// void removeObserver(Observer o); // void removeObserver(Observer o);
void notifyObservers(); void notifyObservers();
void packaging() throws Exception;
} }

View File

@ -5,5 +5,5 @@ import com.google.gson.JsonObject;
import java.util.List; import java.util.List;
public interface Observer { public interface Observer {
void update(List<JsonObject> jsonObjects); void update(JsonObject object);
} }

View File

@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
@Component @Component
public class ResponseDataVk implements ResponsibleData { public class ResponseDataVk implements ResponsibleData {
private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<JsonObject>(); private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
@Override @Override
public void add(JsonObject jsonObject) { public void add(JsonObject jsonObject) {

View File

@ -0,0 +1,7 @@
package org.sadtech.vkbot.listener.handlers;
public interface Handled {
void sortAndSend() throws Exception;
}

View File

@ -1,21 +0,0 @@
package org.sadtech.vkbot.listener.handlers;
import com.google.gson.JsonObject;
import org.sadtech.vkbot.listener.Observer;
import java.util.List;
public class MessageHandlerVk implements Observer {
private DispetcherHandler dispetcherHandler;
public MessageHandlerVk(DispetcherHandler dispetcherHandler) {
this.dispetcherHandler = dispetcherHandler;
dispetcherHandler.registerObserver(this);
}
@Override
public void update(List<JsonObject> jsonObjects) {
}
}

View File

@ -1,12 +1,12 @@
package org.sadtech.vkbot.listener.handlers; package org.sadtech.vkbot.listener.handlers.impl;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.extern.log4j.Log4j; import lombok.extern.log4j.Log4j;
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.sadtech.vkbot.listener.data.ResponsibleData; import org.sadtech.vkbot.listener.data.ResponsibleData;
import org.sadtech.vkbot.listener.handlers.Handled;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,20 +14,22 @@ import java.util.List;
@Log4j @Log4j
@Component @Component
public class DispetcherHandler implements Observable { public class DispetcherHandlerVk implements Observable, Handled {
private ResponsibleData date; private ResponsibleData date;
private List<Observer> observers = new ArrayList<Observer>(); private List<Observer> observers = new ArrayList<Observer>();
private JsonObject event;
public DispetcherHandler(ResponsibleData date) { public DispetcherHandlerVk(ResponsibleData date) {
this.date = date; this.date = date;
} }
@Async @Async
public void packaging() { public void sortAndSend() {
while (true) { while (true) {
if (date.getJsonObjects().peek() != null) { if (date.getJsonObjects().peek() != null) {
log.info(date.getJsonObjects().poll()); event = date.getJsonObjects().poll();
notifyObservers();
} }
} }
} }
@ -40,7 +42,7 @@ public class DispetcherHandler implements Observable {
@Override @Override
public void notifyObservers() { public void notifyObservers() {
for (Observer observer : observers) { for (Observer observer : observers) {
//observer.update(); observer.update(event);
} }
} }
} }

View File

@ -0,0 +1,39 @@
package org.sadtech.vkbot.listener.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.service.MessageService;
import org.sadtech.consultant.database.service.impl.MessageServiceImpl;
import org.sadtech.vkbot.listener.Observable;
import org.sadtech.vkbot.listener.Observer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Log4j
@Component
public class MessageHandlerVk implements Observer {
private Observable dispetcherHandler;
// @Autowired
// private MessageService service;
public MessageHandlerVk(Observable dispetcherHandler) {
this.dispetcherHandler = dispetcherHandler;
dispetcherHandler.registerObserver(this);
}
@Override
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);
}
}
private void send(Message message) {
log.info(message.getBody());
}
}

View File

@ -59,8 +59,6 @@ public class EventListenerVk implements EventListenable {
String test = "{\"one_time\":false,\"buttons\":[[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"1\\\"}\",\"label\":\"Red2\"},\"color\":\"negative\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"2\\\"}\",\"label\":\"Green\"},\"color\":\"positive\"}],[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"3\\\"}\",\"label\":\"White\"},\"color\":\"default\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"4\\\"}\",\"label\":\"Blue\"},\"color\":\"primary\"}]]}"; String test = "{\"one_time\":false,\"buttons\":[[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"1\\\"}\",\"label\":\"Red2\"},\"color\":\"negative\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"2\\\"}\",\"label\":\"Green\"},\"color\":\"positive\"}],[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"3\\\"}\",\"label\":\"White\"},\"color\":\"default\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"4\\\"}\",\"label\":\"Blue\"},\"color\":\"primary\"}]]}";
vk.messages().send(actor).peerId(244319573).keyboard(test).message("Сообщение получено").execute(); vk.messages().send(actor).peerId(244319573).keyboard(test).message("Сообщение получено").execute();
} }
longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20); longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20);
} while (true); } while (true);

View File

@ -1,9 +0,0 @@
package org.sadtech.vkbot.repo;
import org.sadtech.vkbot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}

View File

@ -1,11 +0,0 @@
package org.sadtech.vkbot.service;
import org.sadtech.vkbot.entity.User;
public interface UserService {
User addUser(User user);
void removeUser(long id);
}

View File

@ -1,26 +0,0 @@
package org.sadtech.vkbot.service.impl;
import org.sadtech.vkbot.entity.User;
import org.sadtech.vkbot.repo.UserRepository;
import org.sadtech.vkbot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User addUser(User user) {
User savedUser = userRepository.saveAndFlush(user);
return savedUser;
}
@Override
public void removeUser(long id) {
userRepository.deleteById(id);
}
}