55import com .connect .dto .RoomDTO ;
66import com .connect .model .Room ;
77import com .connect .service .ChatUserService ;
8+ import com .connect .service .JwtTokenService ;
89import com .connect .service .RoomService ;
10+ import com .connect .utils .JwtUtil ;
11+ import jakarta .servlet .http .HttpServletRequest ;
912import lombok .extern .slf4j .Slf4j ;
1013import org .springframework .beans .factory .annotation .Autowired ;
1114import org .springframework .http .HttpStatus ;
1518import org .springframework .messaging .simp .SimpMessagingTemplate ;
1619import org .springframework .web .bind .annotation .*;
1720
18- import java .security .Principal ;
1921import java .util .List ;
2022import java .util .Map ;
2123import java .util .stream .Collectors ;
@@ -30,6 +32,9 @@ public class ChatRoomController {
3032 @ Autowired
3133 private RoomService roomService ;
3234
35+ @ Autowired
36+ private JwtTokenService jwtTokenService ;
37+
3338 @ Autowired
3439 private SimpMessagingTemplate messagingTemplate ;
3540
@@ -48,13 +53,29 @@ public void joinRoom(@Payload Map<String, String> joinReq) {
4853 // Handler when the user creates a new room.
4954 // Person who is creating the room must give the necessary information as in body.
5055 @ PostMapping ("/room/create" )
51- public ResponseEntity <String > createRoom (@ RequestBody Room room , Principal principal ) {
52- // Room name is been given by the owner.
53- // Here we derive the name of the user who is request with the help of principal.
54- Room createdRoom = roomService .addNewRoom (room , principal );
56+ public ResponseEntity <Map <String , String >> createRoom (@ RequestBody Room room , HttpServletRequest request ) {
57+ log .info ("New Room request" );
58+ String rawToken = request .getHeader ("Authorization" );
59+ // Checking the token is valid or not.
60+ // This service extracts the username till then the token is valid otherwise it will not.
61+ String token = rawToken .substring (7 );
62+ String username = jwtTokenService .extractUsername (token );
63+
64+ if (username .isEmpty ()) {
65+ return ResponseEntity
66+ .status (HttpStatus .UNAUTHORIZED )
67+ .body (Map .of ("response" , "Invalid Token" ));
68+ }
69+ // Here we need to send the data to the service class.
70+ var createdRoom = roomService .addNewRoom (room , username );
71+ if (createdRoom == null ) {
72+ return ResponseEntity
73+ .status (HttpStatus .INTERNAL_SERVER_ERROR )
74+ .body (Map .of ("response" , "Something went wrong at the server! Try again later." ));
75+ }
5576 return ResponseEntity
5677 .status (HttpStatus .CREATED )
57- .body ("Room created successfully" );
78+ .body (Map . of ( "response" , createdRoom . getRoomId (). toString ()) );
5879 }
5980
6081 @ GetMapping ("/room/getall" )
0 commit comments