Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.randomwalk.twitterservice.controller.advice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.randomwalk.twitterservice.model.dto.ApiErrorDto;
import ru.randomwalk.twitterservice.model.exception.NotFoundException;

@RestControllerAdvice
@Slf4j
public class TwitterControllerAdvice {

@ExceptionHandler({NotFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ApiErrorDto> exceptionHandler(NotFoundException e) {
log.warn("Handle not found exception", e);
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiErrorDto(e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.randomwalk.twitterservice.model.dto;

import lombok.Builder;

@Builder(toBuilder = true)
public record ApiErrorDto (
String message
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ public void addNewDeviceToken(UUID userId, String deviceToken) {
@Override
public void refreshExistingToken(UUID userId, String previousToken, String newToken) {
log.info("Refreshing existing token for user {}", userId);
var device = deviceRepository.findByUser_UserIdAndDeviceToken(userId, previousToken)
.orElseThrow(() -> new NotFoundException("Device with provided token does not exist"));
device.setDeviceToken(newToken);
deviceRepository.save(device);
log.info("Token was refreshed for device {}", device.getId());
var optionalDevice = deviceRepository.findByUser_UserIdAndDeviceToken(userId, previousToken);
if (optionalDevice.isPresent()) {
var device = optionalDevice.get();
device.setDeviceToken(newToken);
deviceRepository.save(device);
log.info("Token was refreshed for device {}", device.getId());
} else {
addNewDeviceToken(userId, newToken);
}
}

private Device createDevice(UserAccount user, String deviceToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,17 @@ void refreshExistingToken() {
var device = deviceRepository.findById(deviceId).get();
assertEquals(newToken, device.getDeviceToken());
}

@Test
void createNewTokenIfNothingToRefresh() {
String deviceToken = "some device token";
UUID userId = UUID.randomUUID();
String newToken = "new token";

deviceService.refreshExistingToken(userId, deviceToken, newToken);

var device = deviceRepository.findByUser_UserIdAndDeviceToken(userId, newToken);
assertTrue(device.isPresent());
assertEquals(userId, device.get().getUser().getUserId());
}
}
Loading