-
Notifications
You must be signed in to change notification settings - Fork 0
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 #20 from duongminhhieu/develop
Develop into Master
- Loading branch information
Showing
32 changed files
with
2,848 additions
and
624 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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/learning/yasminishop/common/configs/GeminiConfig.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,33 @@ | ||
package com.learning.yasminishop.common.configs; | ||
|
||
import com.google.cloud.vertexai.VertexAI; | ||
import com.google.cloud.vertexai.generativeai.ChatSession; | ||
import com.google.cloud.vertexai.generativeai.GenerativeModel; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
public class GeminiConfig { | ||
|
||
private static final String MODEL_NAME = "gemini-1.5-flash"; | ||
private static final String LOCATION = "asia-southeast1"; | ||
private static final String PROJECT_ID = "yasmini"; | ||
|
||
@Bean | ||
public VertexAI vertexAI() { | ||
return new VertexAI(PROJECT_ID, LOCATION); | ||
} | ||
|
||
@Bean | ||
public GenerativeModel getModel(VertexAI vertexAI) { | ||
return new GenerativeModel(MODEL_NAME, vertexAI); | ||
} | ||
|
||
@Bean | ||
public ChatSession chatSession(@Qualifier("getModel") GenerativeModel generativeModel) { | ||
return new ChatSession(generativeModel); | ||
} | ||
|
||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/learning/yasminishop/common/entity/Notification.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,35 @@ | ||
package com.learning.yasminishop.common.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Entity | ||
@Table(name = "t_notification") | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class Notification extends AuditEntity<String>{ | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
String id; | ||
|
||
@Column(columnDefinition = "TEXT") | ||
String title; | ||
|
||
@Column(columnDefinition = "TEXT") | ||
String content; | ||
|
||
String thumbnail; | ||
|
||
String link; | ||
|
||
Boolean isRead = false; | ||
|
||
@ManyToOne | ||
User user; | ||
|
||
} |
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 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 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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/learning/yasminishop/notification/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,36 @@ | ||
package com.learning.yasminishop.notification; | ||
|
||
import com.learning.yasminishop.common.dto.APIResponse; | ||
import com.learning.yasminishop.notification.dto.response.NotificationResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/notifications") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class NotificationController { | ||
|
||
private final NotificationService notificationService; | ||
|
||
@GetMapping(path = "/subscribe/{token}", produces = MediaType.TEXT_EVENT_STREAM_VALUE) | ||
public SseEmitter subscribe( @PathVariable String token) { | ||
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); | ||
notificationService.addEmitter(emitter, token); | ||
return emitter; | ||
} | ||
|
||
@GetMapping | ||
public APIResponse<List<NotificationResponse>> getNotifications() { | ||
List<NotificationResponse> notifications = notificationService.getNotifications(); | ||
return APIResponse.<List<NotificationResponse>>builder() | ||
.result(notifications) | ||
.build(); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/learning/yasminishop/notification/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,12 @@ | ||
package com.learning.yasminishop.notification; | ||
|
||
import com.learning.yasminishop.common.entity.Notification; | ||
import com.learning.yasminishop.common.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, String>{ | ||
|
||
List<Notification> findAllByUserOrderByCreatedDateDesc(User user); | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/learning/yasminishop/notification/NotificationService.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,80 @@ | ||
package com.learning.yasminishop.notification; | ||
|
||
import com.learning.yasminishop.common.configs.security.JwtService; | ||
import com.learning.yasminishop.common.entity.Notification; | ||
import com.learning.yasminishop.common.entity.User; | ||
import com.learning.yasminishop.common.exception.AppException; | ||
import com.learning.yasminishop.common.exception.ErrorCode; | ||
import com.learning.yasminishop.notification.dto.response.NotificationResponse; | ||
import com.learning.yasminishop.notification.mapper.NotificationMapper; | ||
import com.learning.yasminishop.user.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Transactional(readOnly = true) | ||
public class NotificationService { | ||
|
||
private final NotificationRepository notificationRepository; | ||
private final UserRepository userRepository; | ||
private final NotificationMapper notificationMapper; | ||
|
||
private final JwtService jwtService; | ||
private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>(); | ||
|
||
|
||
public void addEmitter(SseEmitter emitter, String token) { | ||
|
||
if(!jwtService.isTokenValid(token)) { | ||
log.error("Token is not valid"); | ||
emitter.completeWithError(new AppException(ErrorCode.INVALID_TOKEN)); | ||
return; | ||
} | ||
|
||
String email = jwtService.extractUserEmail(token); | ||
log.info("User with email {} subscribed to notifications", email); | ||
emitters.put(email, emitter); | ||
} | ||
|
||
public void sendNotification(String clientId, NotificationResponse notificationResponse) { | ||
SseEmitter emitter = emitters.get(clientId); | ||
if (emitter != null) { | ||
try { | ||
emitter.send(notificationResponse); | ||
} catch (IOException e) { | ||
emitter.completeWithError(e); | ||
emitters.remove(clientId); | ||
} | ||
} | ||
} | ||
|
||
public List<NotificationResponse> getNotifications() { | ||
String email = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
|
||
User user = userRepository.findByEmail(email) | ||
.orElseThrow(() -> new AppException(ErrorCode.USER_NOT_FOUND)); | ||
|
||
List<Notification> notifications = notificationRepository.findAllByUserOrderByCreatedDateDesc(user); | ||
return notifications | ||
.stream() | ||
.map(notificationMapper::toNotificationResponse) | ||
.toList(); | ||
} | ||
|
||
public void createNotification(Notification notification) { | ||
notificationRepository.save(notification); | ||
sendNotification(notification.getUser().getEmail(), notificationMapper.toNotificationResponse(notification)); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/learning/yasminishop/notification/dto/response/NotificationResponse.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,26 @@ | ||
package com.learning.yasminishop.notification.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
@FieldDefaults(level = lombok.AccessLevel.PRIVATE) | ||
public class NotificationResponse { | ||
|
||
String id; | ||
String title; | ||
String content; | ||
String thumbnail; | ||
String link; | ||
Boolean isRead; | ||
|
||
LocalDateTime createdDate; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/learning/yasminishop/notification/mapper/NotificationMapper.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,10 @@ | ||
package com.learning.yasminishop.notification.mapper; | ||
|
||
import com.learning.yasminishop.common.entity.Notification; | ||
import com.learning.yasminishop.notification.dto.response.NotificationResponse; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface NotificationMapper { | ||
NotificationResponse toNotificationResponse(Notification notification); | ||
} |
Oops, something went wrong.