22
33import com .hemant .db .model .Employee ;
44import com .hemant .db .repository .EmployeeRepository ;
5+ import com .hemant .db .model .Response ;
56import org .springframework .beans .factory .annotation .Autowired ;
67import org .springframework .web .bind .MethodArgumentNotValidException ;
78import org .springframework .web .bind .annotation .*;
@@ -25,25 +26,34 @@ public Employee fetchByEmail(@Valid @RequestParam("email") String email) {
2526 }
2627
2728 @ PostMapping ("/login" )
28- public ResponseEntity <String > login (@ Valid @ RequestParam ("email" ) String email , @ Valid @ RequestParam ("password" ) String password ) {
29+ public ResponseEntity <Response > login (@ Valid @ RequestParam ("email" ) String email , @ Valid @ RequestParam ("password" ) String password ) {
30+ Response r = new Response ();
2931 Employee e = EmployeeRepository .findByEmail (email );
3032 try {
3133 String correctPassword = e .getPassword ();
3234 if (e .getStatus ().equals ("Logged In" )) {
33- return new ResponseEntity <>("Error: User Already logged in" , HttpStatus .FORBIDDEN );
35+ r .setStatus (false );
36+ r .setEmployee (e );
37+ return new ResponseEntity <>(r , HttpStatus .FORBIDDEN );
3438 }
3539 else {
3640 if (password .equals (correctPassword )) {
3741 e .setStatus ("Logged In" );
3842 EmployeeRepository .save (e );
39- return new ResponseEntity <>("Logged In\n Username: " + e .getEmail () + "\n Name: " + e .getName (), HttpStatus .OK );
43+ r .setStatus (true );
44+ r .setEmployee (e );
45+ return new ResponseEntity <>(r , HttpStatus .OK );
4046 }
4147 else {
42- return new ResponseEntity <>("Login Failure, Wrong email or password" , HttpStatus .BAD_REQUEST );
48+ r .setStatus (false );
49+ r .setMessage ("Incorrect Email or Password" );
50+ return new ResponseEntity <>(r , HttpStatus .BAD_REQUEST );
4351 }
4452 }
4553 } catch (Exception e1 ) {
46- return new ResponseEntity <>("Error:" + e1 .getMessage (), HttpStatus .BAD_REQUEST );
54+ r .setStatus (false );
55+ r .setMessage ("Something went wrong " + e1 .getMessage ());
56+ return new ResponseEntity <>(r , HttpStatus .BAD_REQUEST );
4757 }
4858
4959 }
0 commit comments