Fetch frontend to API endpoint that checks UUID and if needed generates new, or restore previous
This commit is contained in:
@@ -5,19 +5,15 @@ import com.r11.tools.model.MockedMessageDto;
|
||||
import com.r11.tools.service.KlausService;
|
||||
import com.r11.tools.utilis.BusinessKey;
|
||||
import com.r11.tools.utilis.TrackingClient;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Returns the homepage and provides the api for javascript async requests.
|
||||
* @author Gabriel Modzelewski
|
||||
@@ -64,6 +60,32 @@ public class MockController {
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* If provided UUID does not exist in database returns ResponseEntity with new generated UUID(if previous UUID is not provided),
|
||||
* or old UUID(if previous UUID is provided). If provided UUID exists function returns provided UUID.
|
||||
* @param givenUUIDValue the UUID client wants to check exsitance in database
|
||||
* @param previousUUIDValue the previous UUID used by client(optional variable)
|
||||
* @return ResponseEntity with UUID
|
||||
*/
|
||||
@RequestMapping(
|
||||
method = RequestMethod.GET ,
|
||||
path = {"/check/{givenUUIDValue}/{previousUUIDValue}",
|
||||
"/check/{givenUUIDValue}"})
|
||||
public ResponseEntity<String> checkUUID(
|
||||
@PathVariable String givenUUIDValue
|
||||
,@PathVariable(required = false) String previousUUIDValue ){
|
||||
try{
|
||||
UUID.fromString(givenUUIDValue);
|
||||
} catch (IllegalArgumentException ex){
|
||||
if (previousUUIDValue == null || previousUUIDValue.equals("")){
|
||||
UUID newUUID = UUID.randomUUID();
|
||||
return ResponseEntity.ok(newUUID.toString());
|
||||
}
|
||||
return ResponseEntity.ok(previousUUIDValue);
|
||||
}
|
||||
return ResponseEntity.ok(givenUUIDValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts empty post request and creates new message in given set. The new message has default set of data,
|
||||
* which is constructed in {@link #buildDefaultMessage(UUID, int)} method.
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.r11.tools.repository;
|
||||
|
||||
import com.r11.tools.model.MockedMessage;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@@ -19,5 +20,5 @@ public interface MockedResponseRepository extends CrudRepository<MockedMessage,
|
||||
* @param clientUUID the key-uuid of given set of messages
|
||||
* @return Optional of list of {@link com.r11.tools.model.MockedMessage}
|
||||
*/
|
||||
List<MockedMessage> findAllByClientUUID(UUID clientUUID);
|
||||
Optional<List<MockedMessage>> findAllByClientUUID(UUID clientUUID);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import com.r11.tools.mappers.MockedMessageMapper;
|
||||
import com.r11.tools.model.MockedMessage;
|
||||
import com.r11.tools.model.MockedMessageDto;
|
||||
import com.r11.tools.repository.MockedResponseRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -16,6 +12,10 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Service for {@link com.r11.tools.controller.MockController} and {@link com.r11.tools.controller.MockController}
|
||||
@@ -52,10 +52,11 @@ public class KlausServiceImpl implements KlausService {
|
||||
* @return List of {@link MockedMessageDto}
|
||||
*/
|
||||
@Override
|
||||
public List<MockedMessageDto> getAllMockedResponses(UUID clientUUID) {
|
||||
return mockedResponseRepository.findAllByClientUUID(clientUUID).stream()
|
||||
public List<MockedMessageDto> getAllMockedResponses(UUID clientUUID){
|
||||
Optional<List<MockedMessage>> listOptional = mockedResponseRepository.findAllByClientUUID(clientUUID);
|
||||
return listOptional.map(mockedMessages -> mockedMessages.stream()
|
||||
.map(mockedMessageMapper::mockedMessageToMockedMessageDto)
|
||||
.collect(Collectors.toList());
|
||||
.collect(Collectors.toList())).orElse(List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,16 @@
|
||||
</div>
|
||||
<label for="editable">Editable</label>
|
||||
<input type="checkbox" onchange="changeEditionOfUUID(this)" name="editable" id="editable" value="false"/>
|
||||
|
||||
<div class="hiddable" id="uuid-validation-strategy">
|
||||
<label style="margin-bottom: 1%;display: block;"> UUID generation strategy:</label>
|
||||
<input type="radio" checked name="uuid-validation-type" value="new" id="generateNew"/>
|
||||
<label for="generateNew">Generate new UUID</label>
|
||||
<br>
|
||||
<input type="radio" name="uuid-validation-type" value="restore" id="restore"/>
|
||||
<label for="generateNew">Restore previous UUID</label>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div>
|
||||
<!-- h2 -->
|
||||
@@ -253,6 +263,16 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id="overlay"></div>
|
||||
<div id="modal-uuidChanged" class="modal">
|
||||
<div class="header">
|
||||
<div>Change UUID info<i class="r-exclamation"></i></div>
|
||||
<button onclick="window.location.reload();">×</button>
|
||||
</div>
|
||||
<div id="changeUUIDSuccess" class="body hiddable uuid-modal-body">Your message UUID has been changed successfully.</div>
|
||||
<div id="newUUID" class="body hiddable uuid-modal-body">You provided wrong UUID! <br> New UUID has been generated!</div>
|
||||
<div id="restoredUUID" class="body hiddable uuid-modal-body">You provided wrong UUID! <br> Old UUID has been restored!</div>
|
||||
<div id="noChgUUID" class="body hiddable uuid-modal-body">You doesn't provide any change to UUID!</div>
|
||||
</div>
|
||||
<div id="modal-confirm" class="modal">
|
||||
<div class="header">
|
||||
<div>Message saved<i class="r-exclamation"></i></div>
|
||||
|
||||
@@ -102,6 +102,7 @@ const idToDisplay = function(){
|
||||
}
|
||||
|
||||
function refreshData(){
|
||||
$("#uuid-input").val(clientUUID);
|
||||
fillMessageList();
|
||||
console.log("List initiated");
|
||||
let id = idToDisplay();
|
||||
@@ -247,10 +248,60 @@ function copyUUIDToClipboard(){
|
||||
navigator.clipboard.writeText( document.getElementById('uuid-input').value );
|
||||
}
|
||||
|
||||
|
||||
async function fetchUUIDCheck(givenUUID , strategy){
|
||||
var newUUID = "UUID" ;
|
||||
url = host + "/api/mock/check/";
|
||||
|
||||
switch(strategy){
|
||||
case "new":{
|
||||
await fetch(url + givenUUID, { method : "GET" })
|
||||
.then ( response => response.text() )
|
||||
.then ( data => {
|
||||
newUUID = data;
|
||||
console.log("newUUID: "+newUUID);
|
||||
} )
|
||||
break;
|
||||
}
|
||||
case "restore":{
|
||||
await fetch(url + givenUUID + "/" + clientUUID , { method: "GET" })
|
||||
.then (response => response.text() )
|
||||
.then (data => {
|
||||
newUUID = data;
|
||||
console.log("restoreUUID: "+newUUID);
|
||||
} )
|
||||
break;
|
||||
}
|
||||
}
|
||||
return newUUID ;
|
||||
}
|
||||
|
||||
function changeUUID(element){
|
||||
clientUUID = element.value ;
|
||||
checkUuid();
|
||||
setCookie();
|
||||
|
||||
const uuidStrategy = $('input[name="uuid-validation-type"]:checked').val();
|
||||
const givenUUID = element.value ;
|
||||
|
||||
if( givenUUID == clientUUID ){
|
||||
uuidChangeModalDisplay("noChg");
|
||||
return;
|
||||
}
|
||||
|
||||
var newUUID = fetchUUIDCheck(givenUUID , uuidStrategy);
|
||||
var changeMessage = uuidStrategy;
|
||||
newUUID
|
||||
.then( data => {
|
||||
if (givenUUID == data) {
|
||||
changeMessage = "success";
|
||||
}
|
||||
clientUUID = data;
|
||||
|
||||
uuidChangeModalDisplay(changeMessage);
|
||||
document.cookie = C_UUID + '=' + data ;
|
||||
} )
|
||||
$("#uuid-input").attr("disabled", true);
|
||||
$("#editable").attr("checked", false);
|
||||
loadCookies();
|
||||
refreshData();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ var methodToCall = {
|
||||
const overlay = $('#overlay');
|
||||
const savedModal = $('#modal-confirm');
|
||||
const dataLossModal = $('#modal-query');
|
||||
const uuidChangeModal = $('#modal-uuidChanged')
|
||||
const dataLossModalYes = dataLossModal.children().eq(2).children().eq(0);
|
||||
const dataLossModalNo = dataLossModal.children().eq(2).children().eq(1);
|
||||
const allModals = $('.modal');
|
||||
@@ -23,6 +24,37 @@ const dataLossModalDisplay = function(){
|
||||
showModal(dataLossModal);
|
||||
}
|
||||
|
||||
const uuidChangeModalDisplay = function(addidionalMessage){
|
||||
|
||||
switch(addidionalMessage){
|
||||
case "success":{
|
||||
console.log("success");
|
||||
$(".uuid-modal-body").removeClass("active");
|
||||
$("#changeUUIDSuccess").addClass("active");
|
||||
break;
|
||||
}
|
||||
case "new":{
|
||||
console.log("new UUID");
|
||||
$(".uuid-modal-body").removeClass("active");
|
||||
$("#newUUID").addClass("active");
|
||||
break;
|
||||
}
|
||||
case "restore":{
|
||||
console.log("restoredUUID");
|
||||
$(".uuid-modal-body").removeClass("active");
|
||||
$("#restoredUUID").addClass("active");
|
||||
break;
|
||||
}
|
||||
case "noChg":{
|
||||
console.log("No changes");
|
||||
$(".uuid-modal-body").removeClass("active");
|
||||
$("#noChgUUID").addClass("active");
|
||||
break;
|
||||
}
|
||||
}
|
||||
showModal(uuidChangeModal);
|
||||
}
|
||||
|
||||
function setMethodToCall(name, id){
|
||||
methodToCall.name = name;
|
||||
methodToCall.id = id;
|
||||
|
||||
@@ -3,12 +3,14 @@ var selectMenu = $("#selectMenuContent");
|
||||
var advancedTab = $("#advanced");
|
||||
var basicID = $("#basicItemData")
|
||||
var advancedID = $("#advancedItemData");
|
||||
var advancedUUIDOptions = $("#uuid-validation-strategy");
|
||||
var focusedField = false;
|
||||
function changeAdvancedVisibility(){
|
||||
if(advancedVisibility){
|
||||
selectMenu.removeClass('active');
|
||||
advancedTab.removeClass('active');
|
||||
advancedID.removeClass('active');
|
||||
advancedUUIDOptions.removeClass('active');
|
||||
basicID.addClass('active');
|
||||
advancedVisibility = false;
|
||||
}
|
||||
@@ -16,6 +18,7 @@ function changeAdvancedVisibility(){
|
||||
selectMenu.addClass('active');
|
||||
advancedTab.addClass('active');
|
||||
advancedID.addClass('active');
|
||||
advancedUUIDOptions.addClass('active');
|
||||
basicID.removeClass('active');
|
||||
advancedVisibility = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user