Skip to content

Commit

Permalink
change some vk api properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed Nov 1, 2023
1 parent 6e28fae commit f63fc17
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/main/java/ru/nsu/fit/directors/userservice/api/VkApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import ru.nsu.fit.directors.userservice.dto.request.RequestVkNotification;
import ru.nsu.fit.directors.userservice.dto.response.VkNotificationResponse;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class VkApi {
private final WebClient vkApiClient;

public List<VkNotificationResponse> sendNotification(RequestVkNotification requestVkNotification) {
ParameterizedTypeReference<List<VkNotificationResponse>> reference = new ParameterizedTypeReference<>() {};
public VkNotificationResponse sendNotification(RequestVkNotification requestVkNotification) {
ParameterizedTypeReference<VkNotificationResponse> reference = new ParameterizedTypeReference<>() {};
return vkApiClient.post()
.uri(uriBuilder -> uriBuilder.path("/method/notifications.sendMessage").build())
.contentType(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ru.nsu.fit.directors.userservice.dto.request;

import java.util.List;

public record RequestVkNotification(
String message,
String access_token,
List<Long> userIds
String userId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.Data;

import java.util.Map;

@Data
public class VkNotificationResponse {
private Long userId;
private boolean status;
private Map<String, Object> response;
private Map<String, Object> error;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.nsu.fit.directors.userservice.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
Expand All @@ -18,6 +19,7 @@
@Service
@RequiredArgsConstructor
@ParametersAreNonnullByDefault
@Slf4j
public class NotificationServiceImpl implements NotificationService {
private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
Expand All @@ -31,14 +33,23 @@ public class NotificationServiceImpl implements NotificationService {
public void handleOrderNotification(OrderNotificationEvent event) {
User user = userRepository.findById(event.userId()).orElseThrow();
if (user.getVkUserId() != null) {
vkApi.sendNotification(
new RequestVkNotification(event.message(), vkServiceKey, List.of(user.getVkUserId()))
);
try {
vkApi.sendNotification(
new RequestVkNotification(event.message(), vkServiceKey, String.valueOf(user.getVkUserId()))
);
} catch (Exception e) {
saveNotification(event, user);
log.error(e.getMessage());
}
} else {
notificationRepository.save(new Notification().setUser(user).setMessage(event.message()).setWasReceived(false));
saveNotification(event, user);
}
}

private void saveNotification(OrderNotificationEvent event, User user) {
notificationRepository.save(new Notification().setUser(user).setMessage(event.message()).setWasReceived(false));
}

@Override
public List<NotificationDto> getNotifications() {
List<Notification> notifications = notificationRepository.findByUserAndWasReceived(
Expand Down

0 comments on commit f63fc17

Please sign in to comment.