3
3
import com .github .gribanoveu .cuddle .constants .Constants ;
4
4
import com .github .gribanoveu .cuddle .constants .EmailMessages ;
5
5
import com .github .gribanoveu .cuddle .dtos .enums .BanReason ;
6
- import com .github .gribanoveu .cuddle .dtos .enums .ResponseCode ;
7
6
import com .github .gribanoveu .cuddle .dtos .enums .Role ;
8
- import com .github .gribanoveu .cuddle .dtos .enums .StatusLevel ;
9
7
import com .github .gribanoveu .cuddle .dtos .request .*;
10
- import com .github .gribanoveu .cuddle .dtos .response .ResponseDetails ;
11
- import com .github .gribanoveu .cuddle .dtos .response .StatusResponse ;
12
8
import com .github .gribanoveu .cuddle .entities .services .EmailService ;
13
9
import com .github .gribanoveu .cuddle .entities .services .RedisOtpService ;
14
10
import com .github .gribanoveu .cuddle .entities .services .UserService ;
15
11
import com .github .gribanoveu .cuddle .entities .tables .User ;
16
- import com .github .gribanoveu .cuddle .exeptions .CredentialEx ;
12
+ import com .github .gribanoveu .cuddle .exeptions .errors .UserMessage ;
13
+ import com .github .gribanoveu .cuddle .exeptions .responses .RestException ;
14
+ import com .github .gribanoveu .cuddle .exeptions .responses .RestResponse ;
17
15
import com .github .gribanoveu .cuddle .utils .JsonUtils ;
18
16
import com .github .gribanoveu .cuddle .utils .emails .EmailTemplates ;
19
17
import io .azam .ulidj .ULID ;
20
- import jakarta .servlet .http .HttpServletRequest ;
21
18
import lombok .RequiredArgsConstructor ;
22
19
import lombok .extern .slf4j .Slf4j ;
23
20
import org .springframework .beans .factory .annotation .Value ;
@@ -48,9 +45,9 @@ public class PublicAccountControllerImpl {
48
45
private final EmailService emailService ;
49
46
private final JsonUtils jsonUtils ;
50
47
51
- public ResponseEntity <StatusResponse > registerUser (RegisterDto request ) {
52
- if (!request .password ().equals (request .confirmPassword ())) throw new CredentialEx ( ResponseCode .PASSWORD_NOT_EQUALS );
53
- if (userService .userExistByEmail (request .email ())) throw new CredentialEx ( ResponseCode .USER_ALREADY_EXIST );
48
+ public ResponseEntity <RestResponse > registerUser (RegisterDto request ) {
49
+ if (!request .password ().equals (request .confirmPassword ())) throw new RestException ( UserMessage .PASSWORD_NOT_EQUALS );
50
+ if (userService .userExistByEmail (request .email ())) throw new RestException ( UserMessage .USER_ALREADY_EXIST );
54
51
55
52
var user = new User ();
56
53
user .setUlid (ULID .random (ThreadLocalRandom .current ()));
@@ -69,28 +66,27 @@ public ResponseEntity<StatusResponse> registerUser(RegisterDto request) {
69
66
EmailMessages .userRegisteredSubject , EmailMessages .userRegisteredTemplate ));
70
67
log .info ("Register confirm send to email: {}" , request .email ());
71
68
72
- return ResponseEntity .created (location ).body (StatusResponse .create (
73
- ResponseCode .USER_CREATED , StatusLevel .SUCCESS ));
69
+ return ResponseEntity .created (location ).body (RestResponse .create (UserMessage .USER_CREATED ));
74
70
}
75
71
76
72
// user change email from app, authenticated
77
- public ResponseEntity <StatusResponse > changeEmail (ChangeEmailDto request , Authentication authentication ) {
73
+ public ResponseEntity <RestResponse > changeEmail (ChangeEmailDto request , Authentication authentication ) {
78
74
var user = userService .findUserByEmail (authentication .getName ());
79
75
var oldEmail = user .getEmail ();
80
- if (request .email ().equals (oldEmail )) throw new CredentialEx ( ResponseCode .EMAIL_ALREADY_EXIST );
76
+ if (request .email ().equals (oldEmail )) throw new RestException ( UserMessage .EMAIL_ALREADY_EXIST );
81
77
userService .updateEmail (user , request .email ());
82
78
emailService .sendMail (EmailTemplates .emailChanged (oldEmail , request .email ()));
83
79
log .info ("Email change old email: {}, new email: {}" , oldEmail , request .email ());
84
- return ResponseEntity .ok (StatusResponse .create (ResponseCode .USER_UPDATED , StatusLevel . SUCCESS ));
80
+ return ResponseEntity .ok (RestResponse .create (UserMessage .USER_UPDATED ));
85
81
}
86
82
87
83
// check that password and confirm password match
88
84
// find user, if not exist -> error
89
85
// verify that the old password matches, if not -> error
90
86
// check that old password not equals new password, else -> error
91
87
// change password
92
- public ResponseEntity <StatusResponse > changePassword (ChangePasswordDto request , Authentication authentication ) {
93
- if (!request .password ().equals (request .confirmPassword ())) throw new CredentialEx ( ResponseCode .PASSWORD_NOT_EQUALS );
88
+ public ResponseEntity <RestResponse > changePassword (ChangePasswordDto request , Authentication authentication ) {
89
+ if (!request .password ().equals (request .confirmPassword ())) throw new RestException ( UserMessage .PASSWORD_NOT_EQUALS );
94
90
95
91
var user = userService .findUserByEmail (authentication .getName ());
96
92
if (passwordEncoder .matches (request .oldPassword (), user .getPassword ()))
@@ -101,10 +97,10 @@ public ResponseEntity<StatusResponse> changePassword(ChangePasswordDto request,
101
97
EmailMessages .passwordChangedSubject , EmailMessages .passwordChangedTemplate ));
102
98
log .info ("Password change message sent to: {}" , user .getEmail ());
103
99
104
- return ResponseEntity .ok (StatusResponse .create (ResponseCode .PASSWORD_UPDATED , StatusLevel . SUCCESS ));
105
- } else throw new CredentialEx ( ResponseCode .PASSWORD_EQUALS );
100
+ return ResponseEntity .ok (RestResponse .create (UserMessage .PASSWORD_UPDATED ));
101
+ } else throw new RestException ( UserMessage .PASSWORD_EQUALS );
106
102
107
- throw new CredentialEx ( ResponseCode .OLD_PASSWORD_NOT_MATCH );
103
+ throw new RestException ( UserMessage .OLD_PASSWORD_NOT_MATCH );
108
104
}
109
105
110
106
// user open restore form and enter email
@@ -113,9 +109,9 @@ public ResponseEntity<StatusResponse> changePassword(ChangePasswordDto request,
113
109
// else create new code
114
110
// send email, if not -> error
115
111
// save otp code to db (with lifetime)
116
- public ResponseEntity <StatusResponse > generateOtpCode (GenerateOtpDto request ) {
112
+ public ResponseEntity <RestResponse > generateOtpCode (GenerateOtpDto request ) {
117
113
var userExist = userService .userExistByEmail (request .email ());
118
- if (!userExist ) throw new CredentialEx ( ResponseCode .USER_NOT_EXIST );
114
+ if (!userExist ) throw new RestException ( UserMessage .USER_NOT_EXIST );
119
115
var otpCode = jsonUtils .generateRandomOtpCode ().toString ();
120
116
log .info ("Generate OTP code. Email {}, Code {}" , request .email (), otpCode );
121
117
@@ -125,16 +121,16 @@ public ResponseEntity<StatusResponse> generateOtpCode(GenerateOtpDto request) {
125
121
emailService .sendMail (EmailTemplates .generateOtpEmail (request .email (), otpCode , otpCodeLifeTime ));
126
122
log .info ("Email with code send to: {}" , request .email ());
127
123
128
- return ResponseEntity .ok (StatusResponse .create (ResponseCode .OTP_CODE_CREATED , StatusLevel . SUCCESS ));
124
+ return ResponseEntity .ok (RestResponse .create (UserMessage .OTP_CODE_CREATED ));
129
125
}
130
126
131
127
// next screen user enter otp code and new password and re-enter password again
132
128
// service check that code exist and have valid lifetime
133
129
// service find userId by otp code and change password in db
134
130
// send successful email
135
- public ResponseEntity <StatusResponse > restorePasswordByOtp (RestorePasswordDto request ) {
136
- if (!request .password ().equals (request .confirmPassword ())) throw new CredentialEx ( ResponseCode .PASSWORD_NOT_EQUALS );
137
- if (!redisOtpService .otpCodeValid (request .email (), request .otpCode ())) throw new CredentialEx ( ResponseCode .OTP_CODE_NOT_FOUND );
131
+ public ResponseEntity <RestResponse > restorePasswordByOtp (RestorePasswordDto request ) {
132
+ if (!request .password ().equals (request .confirmPassword ())) throw new RestException ( UserMessage .PASSWORD_NOT_EQUALS );
133
+ if (!redisOtpService .otpCodeValid (request .email (), request .otpCode ())) throw new RestException ( UserMessage .OTP_CODE_NOT_FOUND );
138
134
139
135
var user = userService .findUserByEmail (request .email ());
140
136
if (!passwordEncoder .matches (request .password (), user .getPassword ())) {
@@ -143,20 +139,20 @@ public ResponseEntity<StatusResponse> restorePasswordByOtp(RestorePasswordDto re
143
139
emailService .sendMail (EmailTemplates .simpleEmail (request .email (),
144
140
EmailMessages .passwordChangedSubject , EmailMessages .passwordChangedTemplate ));
145
141
log .info ("Password change message sent to: {}" , request .email ());
146
- return ResponseEntity .ok (StatusResponse .create (ResponseCode .PASSWORD_UPDATED , StatusLevel . SUCCESS ));
142
+ return ResponseEntity .ok (RestResponse .create (UserMessage .PASSWORD_UPDATED ));
147
143
}
148
- throw new CredentialEx ( ResponseCode .PASSWORD_EQUALS );
144
+ throw new RestException ( UserMessage .PASSWORD_EQUALS );
149
145
}
150
146
151
- public ResponseEntity <StatusResponse > getRestrictionsReason (String email ) {
147
+ public ResponseEntity <RestResponse > getRestrictionsReason (String email ) {
152
148
var userExist = userService .userExistByEmail (email );
153
- if (!userExist ) throw new CredentialEx ( ResponseCode .USER_NOT_EXIST );
149
+ if (!userExist ) throw new RestException ( UserMessage .USER_NOT_EXIST );
154
150
var user = userService .findUserByEmail (email );
155
- if (user .getBanExpiration () == null ) throw new CredentialEx ( ResponseCode .NO_RESTRICTIONS );
151
+ if (user .getBanExpiration () == null ) throw new RestException ( UserMessage .NO_RESTRICTIONS );
156
152
var reason = BanReason .getBanMessageByCode (user .getRestrictionReason ());
157
153
var responseText = String .format ("Ваша учетная запись заблокирована по причине: %1$s. Блокировка истекает: %2$s" ,
158
154
reason , user .getBanExpiration ().format (Constants .DEFAULT_TIME_FORMAT ));
159
155
160
- return ResponseEntity .ok (StatusResponse .create (new ResponseDetails ( responseText ), StatusLevel . SUCCESS ));
156
+ return ResponseEntity .ok (RestResponse .create (responseText ));
161
157
}
162
158
}
0 commit comments