Skip to content

Commit 7491887

Browse files
Рефакторинг классов Middleware, LocalisationMiddleware и LoginMiddleware: изменен метод preHandle для использования исключений для остановки цепочки middleware и маршрутизации. Обновлены пакеты для соответствия новой структуре.
1 parent 5afedc3 commit 7491887

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/main/java/ru/untitled_devs/bot/features/localisation/midlewares/LocalisationMiddleware.java renamed to src/main/java/ru/untitled_devs/bot/features/localisation/LocalisationMiddleware.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package ru.untitled_devs.bot.features.localisation.midlewares;
1+
package ru.untitled_devs.bot.features.localisation;
22

33
import org.telegram.telegrambots.meta.api.objects.Update;
4-
import ru.untitled_devs.bot.features.localisation.LocalisationStates;
54
import ru.untitled_devs.core.context.UpdateContext;
65
import ru.untitled_devs.core.fsm.context.DataKey;
76
import ru.untitled_devs.core.fsm.context.FSMContext;
@@ -12,19 +11,19 @@
1211

1312
import java.util.Locale;
1413

15-
public class LocalisationMiddleware implements Middleware {
14+
public class LocalisationMiddleware extends Middleware {
1615
private final SceneManager sceneManager;
1716
public LocalisationMiddleware(SceneManager sceneManager) {
1817
this.sceneManager = sceneManager;
1918
}
2019

2120
@Override
22-
public boolean preHandle(UpdateContext update, FSMContext ctx) {
21+
public void preHandle(UpdateContext update, FSMContext ctx) {
2322
DataKey<Locale> langKey = DataKey.of("lang", Locale.class);
2423
Locale loc = ctx.getData(langKey);
2524

2625
if (StatesGroup.contains(LocalisationStates.class, ctx.getState()))
27-
return false;
26+
stopMiddlewareChain();
2827

2928
if (loc == null) {
3029
DataKey<Update> updateKey = DataKey.of("register:Update", Update.class);
@@ -34,9 +33,8 @@ public boolean preHandle(UpdateContext update, FSMContext ctx) {
3433
ctx.setData(stateKey, ctx.getState());
3534

3635
sceneManager.enterScene("lang", update.getChatId(), ctx);
37-
return false;
36+
stopMiddlewareChain();
3837
}
3938

40-
return true;
4139
}
4240
}

src/main/java/ru/untitled_devs/bot/features/registration/middlewares/LoginMiddleware.java renamed to src/main/java/ru/untitled_devs/bot/features/registration/LoginMiddleware.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
package ru.untitled_devs.bot.features.registration.middlewares;
1+
package ru.untitled_devs.bot.features.registration;
22

3-
import ru.untitled_devs.bot.features.registration.RegistrationStates;
43
import ru.untitled_devs.core.context.UpdateContext;
54
import ru.untitled_devs.core.fsm.context.FSMContext;
65
import ru.untitled_devs.core.fsm.states.State;
76
import ru.untitled_devs.core.fsm.states.StatesGroup;
87
import ru.untitled_devs.core.middlewares.Middleware;
98
import ru.untitled_devs.core.routers.scenes.SceneManager;
109

11-
public class LoginMiddleware implements Middleware {
10+
public class LoginMiddleware extends Middleware {
1211
private final SceneManager sceneManager;
1312

1413
public LoginMiddleware(SceneManager sceneManager) {
1514
this.sceneManager = sceneManager;
1615
}
1716

1817
@Override
19-
public boolean preHandle(UpdateContext update, FSMContext ctx) {
18+
public void preHandle(UpdateContext update, FSMContext ctx) {
2019
State state = ctx.getState();
2120
if (! StatesGroup.contains(RegistrationStates.class, state)) {
2221
sceneManager.enterScene("register", update.getChatId(), ctx);
23-
return false;
22+
stopRouting();
2423
}
25-
return true;
2624
}
2725
}

src/main/java/ru/untitled_devs/core/middlewares/Middleware.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
import org.telegram.telegrambots.meta.api.objects.Update;
44
import ru.untitled_devs.core.context.UpdateContext;
5+
import ru.untitled_devs.core.exceptions.StopMiddlewareException;
6+
import ru.untitled_devs.core.exceptions.StopRoutingException;
57
import ru.untitled_devs.core.fsm.context.FSMContext;
68

7-
public interface Middleware {
8-
boolean preHandle(UpdateContext update, FSMContext ctx);
9+
public abstract class Middleware {
10+
public abstract void preHandle(UpdateContext update, FSMContext ctx);
11+
12+
protected void stopMiddlewareChain() {
13+
throw new StopMiddlewareException();
14+
}
15+
16+
protected void stopRouting() {
17+
throw new StopRoutingException();
18+
}
919
}

0 commit comments

Comments
 (0)