-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from ttthanhf/feature/websocket
Feature/websocket
- Loading branch information
Showing
11 changed files
with
317 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||
*/ | ||
package housemate.configs; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
/** | ||
* | ||
* @author ThanhF | ||
*/ | ||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS(); | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
registry.enableSimpleBroker("/topic", "/user"); | ||
registry.setApplicationDestinationPrefixes("/app"); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/housemate/controllers/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package housemate.controllers; | ||
|
||
import housemate.entities.Notification; | ||
import housemate.services.NotificationService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||
*/ | ||
/** | ||
* | ||
* @author ThanhF | ||
*/ | ||
@RestController | ||
@RequestMapping("/notifications") | ||
@CrossOrigin | ||
@Tag(name = "Notification") | ||
@SecurityRequirement(name = "bearerAuth") | ||
public class NotificationController { | ||
|
||
@Autowired | ||
NotificationService notificationService; | ||
|
||
@Operation(summary = "Get all notification for user") | ||
@GetMapping | ||
public ResponseEntity<List<Notification>> getAllNotificationByUser(HttpServletRequest request) { | ||
return notificationService.getAllNotificationByUser(request); | ||
} | ||
|
||
@Operation(summary = "Change status read notification to true") | ||
@PutMapping("/{notificationId}/read") | ||
public ResponseEntity<String> updateReadStatusNotification(HttpServletRequest request, @PathVariable int notificationId) { | ||
return notificationService.updateReadStatusNotification(request, notificationId); | ||
} | ||
|
||
@Operation(summary = "Change all status read notification to true") | ||
@PutMapping("/read-all") | ||
public ResponseEntity<String> updateAllReadStatusNotification(HttpServletRequest request) { | ||
return notificationService.updateAllReadStatusNotification(request); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/housemate/controllers/WebSocketController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||
*/ | ||
package housemate.controllers; | ||
|
||
import housemate.entities.Notification; | ||
import housemate.services.WebSocketService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* | ||
* @author ThanhF | ||
*/ | ||
@RestController | ||
public class WebSocketController { | ||
|
||
@Autowired | ||
private WebSocketService webSocketService; | ||
|
||
@PostMapping("/ws/customer/{userId}") | ||
public void sendNotificationToUser(@PathVariable String userId, @RequestBody Notification notification) { | ||
webSocketService.sendNotificationToUser(userId, notification); | ||
} | ||
|
||
@PostMapping("/ws/staff") | ||
public void sendNotification(@RequestBody Notification notification) { | ||
webSocketService.sendNotificationToEveryone(notification); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/housemate/repositories/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||
*/ | ||
package housemate.repositories; | ||
|
||
import housemate.entities.Notification; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* | ||
* @author ThanhF | ||
*/ | ||
@Repository | ||
public interface NotificationRepository extends JpaRepository<Notification, Integer> { | ||
|
||
@Query("SELECT n FROM Notification n WHERE n.userId = :userId") | ||
List<Notification> getAllNotificationByUserId(int userId); | ||
|
||
@Query("SELECT n FROM Notification n WHERE n.userId = :userId AND n.notificationId = :notificationId") | ||
Notification getNotificationByNotificationIdAndUserId(int userId, int notificationId); | ||
} |
Oops, something went wrong.