Skip to content

Commit 83db2c2

Browse files
committed
chore: added routes and functionality for adding room to db
1 parent 345556f commit 83db2c2

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/main/java/com/connect/controller/ChatRoomController.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import com.connect.dto.RoomDTO;
66
import com.connect.model.Room;
77
import com.connect.service.ChatUserService;
8+
import com.connect.service.JwtTokenService;
89
import com.connect.service.RoomService;
10+
import com.connect.utils.JwtUtil;
11+
import jakarta.servlet.http.HttpServletRequest;
912
import lombok.extern.slf4j.Slf4j;
1013
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.http.HttpStatus;
@@ -15,7 +18,6 @@
1518
import org.springframework.messaging.simp.SimpMessagingTemplate;
1619
import org.springframework.web.bind.annotation.*;
1720

18-
import java.security.Principal;
1921
import java.util.List;
2022
import java.util.Map;
2123
import 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")

src/main/java/com/connect/controller/ChatUserController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public void fetchUsers() {
7272
.stream()
7373
.map(UserDTO::new)
7474
.collect(Collectors.toList());
75-
System.out.println("working dude");
7675
messagingTemplate.convertAndSend("/topic/users", modifiedUsers);
7776
});
7877
}
79-
8078
}

src/main/java/com/connect/service/ChatUserService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public Optional<List<Message>> chatHistoryHandler(String roomId) {
9393
}
9494

9595
public Optional<List<User>> fetchUser() {
96+
log.info("Fetching all users from database");
9697
return userRepository.findAllUser();
9798
}
9899

src/main/java/com/connect/service/RoomService.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ public boolean isRoomExists(ObjectId roomId) {
6767
}
6868

6969
// Method for adding a new Room
70-
public Room addNewRoom(Room room, Principal principal) {
71-
String user = principal.getName();
72-
if (user.isEmpty()) {
73-
log.error("User not found, failed to create the room");
74-
return null;
75-
}
76-
Optional<User> fetchedUser = userRepository.findByUsername(user);
70+
public Room addNewRoom(Room room, String username) {
71+
Optional<User> fetchedUser = userRepository.findByUsername(username);
7772
if (fetchedUser.isEmpty()) {
7873
log.error("No user found in the DB, failed to create new room");
7974
return null;

0 commit comments

Comments
 (0)