|
@@ -3,18 +3,20 @@ package in.ocsf.these.days.app.service;/* kpmy 19.02.2017 */
|
|
|
import com.pengrad.telegrambot.model.Update;
|
|
|
import in.ocsf.these.days.app.messaging.ChatHelper;
|
|
|
import in.ocsf.these.days.app.messaging.UpdateHelper;
|
|
|
+import in.ocsf.these.days.app.object.Chat;
|
|
|
import in.ocsf.these.days.app.object.User;
|
|
|
import in.ocsf.these.days.app.repo.CardRepositrory;
|
|
|
+import in.ocsf.these.days.app.repo.ChatRepository;
|
|
|
import in.ocsf.these.days.app.repo.MessageRepository;
|
|
|
import in.ocsf.these.days.app.repo.UserRepository;
|
|
|
-import in.ocsf.these.days.app.state.UserEvent;
|
|
|
-import in.ocsf.these.days.app.state.UserState;
|
|
|
+import in.ocsf.these.days.app.state.*;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.statemachine.StateMachine;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
@Service
|
|
|
public class UpdateService {
|
|
@@ -33,6 +35,9 @@ public class UpdateService {
|
|
|
@Autowired
|
|
|
private StateService stateService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ChatRepository chatRepo;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ChatHelper chat;
|
|
|
|
|
@@ -44,12 +49,26 @@ public class UpdateService {
|
|
|
switch (cmd.get(0)) {
|
|
|
case "/start":
|
|
|
User user = User.fromUser(upd.getUser());
|
|
|
+ user = Optional.ofNullable(userRepo.findOne(user.getId())).orElse(user);
|
|
|
StateMachine<UserState, UserEvent> state = stateService.getStateFor(user);
|
|
|
- if (state.getState().getId().equals(UserState.unknown)) {
|
|
|
- stateService.setStateFor(user, state);
|
|
|
- chat.sendSimpleTextMessage(upd.getChatId(), "дратути...");
|
|
|
- userRepo.save(user);
|
|
|
+ if (state.getState() == null) state.start();
|
|
|
+ switch (state.getState().getId()) {
|
|
|
+ case unknown:
|
|
|
+ stateService.setStateFor(user, state);
|
|
|
+ userRepo.save(user);
|
|
|
+
|
|
|
+ chatRepo.findByUserIdAndThenClose(user.getId());
|
|
|
+
|
|
|
+ Chat chat = new Chat();
|
|
|
+ chat.setUser(user);
|
|
|
+ chat.setType(ChatType.welcome);
|
|
|
+ StateMachine<WelcomeChatState, WelcomeChatEvent> chatState = stateService.getChatStateFor(chat);
|
|
|
+ chatState.start();
|
|
|
+ stateService.setChatStateFor(chat, chatState);
|
|
|
+ chatRepo.save(chat);
|
|
|
+ break;
|
|
|
}
|
|
|
+
|
|
|
break;
|
|
|
}
|
|
|
} else {
|