Skip to content

Commit e9033ed

Browse files
committed
Update the way the deletion is handled
1 parent 21a8c56 commit e9033ed

4 files changed

Lines changed: 49 additions & 22 deletions

File tree

TrashEmailService/src/main/java/io/github/trashemail/Respositories/UserRepository.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
@Repository
1515
public interface UserRepository extends CrudRepository<User, Integer>{
16-
public List<User> findByChatId(long chatId);
17-
public User findByEmailId(String emailId);
18-
public boolean existsByEmailId(String emailId);
16+
public List<User> findByChatIdAndIsActiveTrue(long chatId);
17+
public User findByEmailIdAndIsActiveTrue(String emailId);
18+
public User findByEmailIdAndChatId(String emailId , long chatId);
19+
public boolean existsByEmailIdAndIsActiveTrue(String emailId);
1920
public void delete(User user);
21+
2022
public long count();
23+
2124
public List<User> findByEmailIdEndsWith(String domain);
2225

2326
@Query(value = "select max(u.id) from User u")

TrashEmailService/src/main/java/io/github/trashemail/Telegram/TelegramRequestHandler.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public String deleteEmail(User user)
4343
throws HttpClientErrorException{
4444
Boolean isDeleted = emailServerInteraction.deleteEmailId(user);
4545
if(isDeleted){
46-
userRepository.delete(user);
46+
47+
user.setIsActive(false);
48+
userRepository.save(user);
49+
4750
return "Email Id *deleted* and added to open pool.";
4851
}
4952
return "No mail ID on the server was identified with" +
@@ -96,8 +99,10 @@ public TelegramResponse handleRequest(long chatId, String text) {
9699
);
97100

98101
case "/create":
99-
if(userRepository.findByChatId(chatId).size() >= trashemailConfig.getMaxEmailsPerUser()) {
100-
responseText = "Only " + trashemailConfig.getMaxEmailsPerUser() + " " +
102+
if(userRepository.findByChatIdAndIsActiveTrue(chatId).size()
103+
>= trashemailConfig.getMaxEmailsPerUser()) {
104+
responseText = "Only " +
105+
trashemailConfig.getMaxEmailsPerUser() + " " +
101106
"email Ids are allowed per-user\n" +
102107
"You can get the list of all the emails @ /emails";
103108
return new TelegramResponse(
@@ -110,7 +115,7 @@ public TelegramResponse handleRequest(long chatId, String text) {
110115
if(argument != null) {
111116
/*
112117
User entered something like : /create username.
113-
No offer him interactive response.
118+
Now offer him interactive response.
114119
*/
115120
String emailRegex = "^[A-Za-z0-9._%+-]+@(" +
116121
String.join("|",
@@ -122,11 +127,12 @@ public TelegramResponse handleRequest(long chatId, String text) {
122127
// A valid domain email is inputted by the user
123128
if (matcher.matches()) {
124129
String emailId = argument;
125-
if(userRepository.existsByEmailId(emailId)){
130+
if(userRepository.existsByEmailIdAndIsActiveTrue(
131+
emailId)){
126132
// Email ID Already taken
127133
responseText = "Email ID *" + argument + "* " +
128134
"is already taken, " +
129-
"please get some other";
135+
"please try some other email id.";
130136
return new TelegramResponse(
131137
chatId,
132138
responseText
@@ -165,7 +171,19 @@ public TelegramResponse handleRequest(long chatId, String text) {
165171
}
166172

167173
if(response!=null) {
168-
userRepository.save(user);
174+
// Check if the same user has taken the same
175+
// email before, then it would exist in DB so
176+
// just set isActive to true.
177+
User existingUser =
178+
userRepository.findByEmailIdAndChatId(
179+
emailId,
180+
chatId);
181+
if(existingUser == null)
182+
userRepository.save(user);
183+
else{
184+
existingUser.setIsActive(true);
185+
userRepository.save(existingUser);
186+
}
169187
responseText = "Email successfully created" +
170188
" - " +
171189
"" +
@@ -280,7 +298,8 @@ else if(argument == null) {
280298
case "/emails":
281299
String response = "Currently, you have below mentioned " +
282300
"emails with you.\n*";
283-
List<User> allEmailsWithUser = userRepository.findByChatId(
301+
List<User> allEmailsWithUser =
302+
userRepository.findByChatIdAndIsActiveTrue(
284303
chatId);
285304

286305
for(User emailWithUser: allEmailsWithUser){
@@ -297,7 +316,8 @@ else if(argument == null) {
297316
if(argument == null){
298317
responseText = "Pick an email to delete.";
299318

300-
List<User> emailsWithUser = userRepository.findByChatId(
319+
List<User> emailsWithUser =
320+
userRepository.findByChatIdAndIsActiveTrue(
301321
chatId);
302322

303323
int buttonPerRow = 1;
@@ -340,9 +360,10 @@ else if(argument == null) {
340360
// A valid domain email is inputted by the user
341361
if (matcher.matches()) {
342362
String emailId = argument;
343-
User user = userRepository.findByEmailId(emailId);
363+
User user = userRepository.findByEmailIdAndIsActiveTrue(
364+
emailId);
344365
// user should only delete email owned by user.
345-
if (userRepository.existsByEmailId(emailId)) {
366+
if (userRepository.existsByEmailIdAndIsActiveTrue(emailId)) {
346367
if (((Long) chatId).equals(user.getChatId())) {
347368
responseText = this.deleteEmail(user);
348369
return new TelegramResponse(

TrashEmailService/src/main/java/io/github/trashemail/Telegram/TelegramResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public TelegramResponse messageHandler(
9191
@GetMapping(value = "/getChatId")
9292
public User getChatIdFortargetEmailAddress(@RequestParam String emailId){
9393

94-
User user = userRepository.findByEmailId(emailId);
94+
User user = userRepository.findByEmailIdAndIsActiveTrue(emailId);
9595
emailCounterRepository.updateCount();
9696

9797
return user;

TrashEmailService/src/main/java/io/github/trashemail/models/User.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
import lombok.Setter;
77
import org.hibernate.annotations.CreationTimestamp;
88

9-
import javax.persistence.Column;
10-
import javax.persistence.Entity;
11-
import javax.persistence.GeneratedValue;
12-
import javax.persistence.Id;
13-
import javax.persistence.Table;
9+
import javax.persistence.*;
1410
import java.time.LocalDateTime;
1511
import java.util.Date;
1612

@@ -25,18 +21,25 @@ public class User {
2521
private Integer id;
2622
private long chatId;
2723

28-
@Column(unique = true)
2924
private String emailId;
3025

3126
private String forwardsTo;
3227

3328
@CreationTimestamp
3429
private LocalDateTime createDateTime;
3530

36-
public User(long chatId, String emailId, String forwardsTo) {
31+
private Boolean isActive;
32+
33+
public User(long chatId, String emailId, String forwardsTo,
34+
Boolean isActive) {
3735
this.chatId = chatId;
3836
this.emailId = emailId;
3937
this.forwardsTo = forwardsTo;
38+
this.isActive = isActive;
39+
}
40+
41+
public User(long chatId, String emailId, String forwardsTo) {
42+
this(chatId, emailId, forwardsTo, Boolean.TRUE);
4043
}
4144

4245
@Override

0 commit comments

Comments
 (0)