Skip to content

Commit

Permalink
Merge pull request #64 from ttthanhf/feature/websocket
Browse files Browse the repository at this point in the history
Feature/websocket
  • Loading branch information
ttthanhf committed Nov 14, 2023
2 parents 10e76b5 + 0378af7 commit 8b27c67
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 130 deletions.
32 changes: 32 additions & 0 deletions src/main/java/housemate/configs/WebSocketConfig.java
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 src/main/java/housemate/controllers/NotificationController.java
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 src/main/java/housemate/controllers/WebSocketController.java
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);
}
}
39 changes: 22 additions & 17 deletions src/main/java/housemate/entities/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@
package housemate.entities;

import jakarta.persistence.*;
import java.util.Date;
import java.time.LocalDateTime;
import lombok.Data;

/**
*
* @author ThanhF
*/
@Data
@Entity
@Table(name = "Notification")
@Table(name = "notifications")
public class Notification {

@Id
@Column(name = "notification_object_id")
private int notificationObjectId;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_id")
private int notificationId;

@Id
@Column(name = "receiver_id")
private int receiverId;
@Column(name = "user_id")
private int userId;

@Column(name = "sent_at")
private Date sentAt;
@Column(name = "notification_created_at")
private LocalDateTime createdAt;

@Column(name = "is_read")
private boolean isRead;

public Notification(int notificationObjectId, int receiverId, Date sentAt, boolean isRead) {
this.notificationObjectId = notificationObjectId;
this.receiverId = receiverId;
this.sentAt = sentAt;
this.isRead = isRead;
}
@Column(name = "message")
private String message;


}
@Column(name = "title")
private String title;

@Column(name = "entity_id")
private int entityId;

@Transient
UserAccount user;

}
56 changes: 0 additions & 56 deletions src/main/java/housemate/entities/NotificationObject.java

This file was deleted.

55 changes: 0 additions & 55 deletions src/main/java/housemate/entities/ServiceSchedule.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/housemate/filters/AuthorizationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AuthorizationFilter extends OncePerRequestFilter {
JwtPayloadMapper jwtPayloadMapper;


private final List<String> excludedUrls = Arrays.asList("/swagger-ui", "/auth", "/v3/api-docs", "/comment/services", "/period/all", "/services", "/feedback/service");
private final List<String> excludedUrls = Arrays.asList("/swagger-ui", "/auth", "/v3/api-docs", "/comment/services", "/period/all", "/services", "/feedback/service", "/ws");


private final List<String> excludedUrlsRegex = Arrays.asList(RegexConstants.REPLY_COMMENT_REGEX);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/housemate/repositories/NotificationRepository.java
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);
}
Loading

0 comments on commit 8b27c67

Please sign in to comment.