forked from mtusi2026/mtusi2026lab3
на 3 готво тесты валидны
This commit is contained in:
@@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.example.dto.MessagePage;
|
||||
import org.example.dto.create.CreateMessageDto;
|
||||
import org.example.dto.update.UpdateMessageDto;
|
||||
import org.example.exception.ConflictException;
|
||||
import org.example.exception.ForbiddenException;
|
||||
import org.example.exception.NotFoundException;
|
||||
import org.example.model.IdempotencyKey;
|
||||
@@ -21,6 +22,8 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.example.config.TokenHasher.sha256;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MessageService {
|
||||
@@ -41,12 +44,16 @@ public class MessageService {
|
||||
}
|
||||
|
||||
String key = idempotencyKey.trim();
|
||||
|
||||
String hash = sha256(messageObject.getText());
|
||||
Optional<IdempotencyKey> exists = idempotencyKeyRepository.findByUserIdAndChatIdAndIdempotencyKey(senderId, chatId, key);
|
||||
|
||||
if (exists.isPresent()){
|
||||
return messageRepository.findById(exists.get().getMessageId())
|
||||
.orElseThrow(() -> new IllegalArgumentException("Сообщения нет"));
|
||||
if (exists.isPresent()) {
|
||||
var row = exists.get();
|
||||
if (!row.getRequestHash().equals(hash)) {
|
||||
throw new ConflictException("Idempotency key conflict");
|
||||
}
|
||||
return messageRepository.findById(row.getMessageId())
|
||||
.orElseThrow(() -> new IllegalArgumentException("Jшибка при отправке сообщения") );
|
||||
}
|
||||
|
||||
Message message = Message.builder()
|
||||
@@ -63,6 +70,7 @@ public class MessageService {
|
||||
.userId(senderId)
|
||||
.chatId(chatId)
|
||||
.idempotencyKey(key)
|
||||
.requestHash(hash)
|
||||
.messageId(message.getId())
|
||||
.build()
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user