-
Notifications
You must be signed in to change notification settings - Fork 0
/
JooqTgChatService.java
64 lines (58 loc) · 2.27 KB
/
JooqTgChatService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package edu.eflerrr.scrapper.service.jooq;
import edu.eflerrr.scrapper.exception.DuplicateRegistrationException;
import edu.eflerrr.scrapper.exception.TgChatNotExistException;
import edu.eflerrr.scrapper.service.TgChatService;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.DSLContext;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import static edu.eflerrr.jooqcodegen.generated.Tables.CHAT;
import static edu.eflerrr.jooqcodegen.generated.Tables.LINK;
import static edu.eflerrr.jooqcodegen.generated.Tables.TRACKING;
@RequiredArgsConstructor
@Slf4j
public class JooqTgChatService implements TgChatService {
private final DSLContext dsl;
private final String autoUsernamePrefix = "auto-username@";
@Override
@Transactional
public void register(long tgChatId) {
log.debug("REGISTER IN TG-CHAT-SERVICE (JOOQ): tgChatId: {}", tgChatId);
try {
dsl.insertInto(CHAT)
.set(CHAT.ID, tgChatId)
.set(CHAT.USERNAME, autoUsernamePrefix + tgChatId)
.set(CHAT.CREATED_AT, OffsetDateTime.now(ZoneOffset.UTC))
.execute();
} catch (DuplicateKeyException ex) {
throw new DuplicateRegistrationException(ex.getMessage());
}
}
@Override
@Transactional
public void unregister(long tgChatId) {
log.debug("UNREGISTER IN TG-CHAT-SERVICE (JOOQ): tgChatId: {}", tgChatId);
var trackingList = dsl.selectFrom(TRACKING)
.where(TRACKING.CHAT_ID.eq(tgChatId))
.fetch();
if (!trackingList.isEmpty()) {
for (var tracking : trackingList) {
var linkId = tracking.getLinkId();
if (dsl.fetchCount(TRACKING, TRACKING.LINK_ID.eq(linkId)) == 1) {
dsl.deleteFrom(LINK)
.where(LINK.ID.eq(linkId))
.execute();
}
}
}
var checker = dsl.deleteFrom(CHAT)
.where(CHAT.ID.eq(tgChatId))
.execute();
if (checker == 0) {
throw new TgChatNotExistException("Chat not found!");
}
}
}