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.service.KlausService;
|
||||||
import com.r11.tools.utilis.BusinessKey;
|
import com.r11.tools.utilis.BusinessKey;
|
||||||
import com.r11.tools.utilis.TrackingClient;
|
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.AllArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the homepage and provides the api for javascript async requests.
|
* Returns the homepage and provides the api for javascript async requests.
|
||||||
* @author Gabriel Modzelewski
|
* @author Gabriel Modzelewski
|
||||||
@@ -64,6 +60,32 @@ public class MockController {
|
|||||||
return messages;
|
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,
|
* 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.
|
* 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 com.r11.tools.model.MockedMessage;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
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
|
* @param clientUUID the key-uuid of given set of messages
|
||||||
* @return Optional of list of {@link com.r11.tools.model.MockedMessage}
|
* @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.MockedMessage;
|
||||||
import com.r11.tools.model.MockedMessageDto;
|
import com.r11.tools.model.MockedMessageDto;
|
||||||
import com.r11.tools.repository.MockedResponseRepository;
|
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.AllArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -16,6 +12,10 @@ import org.springframework.http.HttpHeaders;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
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}
|
* 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}
|
* @return List of {@link MockedMessageDto}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<MockedMessageDto> getAllMockedResponses(UUID clientUUID) {
|
public List<MockedMessageDto> getAllMockedResponses(UUID clientUUID){
|
||||||
return mockedResponseRepository.findAllByClientUUID(clientUUID).stream()
|
Optional<List<MockedMessage>> listOptional = mockedResponseRepository.findAllByClientUUID(clientUUID);
|
||||||
|
return listOptional.map(mockedMessages -> mockedMessages.stream()
|
||||||
.map(mockedMessageMapper::mockedMessageToMockedMessageDto)
|
.map(mockedMessageMapper::mockedMessageToMockedMessageDto)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList())).orElse(List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -27,6 +27,16 @@
|
|||||||
</div>
|
</div>
|
||||||
<label for="editable">Editable</label>
|
<label for="editable">Editable</label>
|
||||||
<input type="checkbox" onchange="changeEditionOfUUID(this)" name="editable" id="editable" value="false"/>
|
<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>
|
||||||
<div>
|
<div>
|
||||||
<!-- h2 -->
|
<!-- h2 -->
|
||||||
@@ -253,6 +263,16 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="overlay"></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 id="modal-confirm" class="modal">
|
||||||
<div class="header">
|
<div class="header">
|
||||||
<div>Message saved<i class="r-exclamation"></i></div>
|
<div>Message saved<i class="r-exclamation"></i></div>
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ const idToDisplay = function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function refreshData(){
|
function refreshData(){
|
||||||
|
$("#uuid-input").val(clientUUID);
|
||||||
fillMessageList();
|
fillMessageList();
|
||||||
console.log("List initiated");
|
console.log("List initiated");
|
||||||
let id = idToDisplay();
|
let id = idToDisplay();
|
||||||
@@ -247,10 +248,60 @@ function copyUUIDToClipboard(){
|
|||||||
navigator.clipboard.writeText( document.getElementById('uuid-input').value );
|
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){
|
function changeUUID(element){
|
||||||
clientUUID = element.value ;
|
|
||||||
checkUuid();
|
const uuidStrategy = $('input[name="uuid-validation-type"]:checked').val();
|
||||||
setCookie();
|
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 overlay = $('#overlay');
|
||||||
const savedModal = $('#modal-confirm');
|
const savedModal = $('#modal-confirm');
|
||||||
const dataLossModal = $('#modal-query');
|
const dataLossModal = $('#modal-query');
|
||||||
|
const uuidChangeModal = $('#modal-uuidChanged')
|
||||||
const dataLossModalYes = dataLossModal.children().eq(2).children().eq(0);
|
const dataLossModalYes = dataLossModal.children().eq(2).children().eq(0);
|
||||||
const dataLossModalNo = dataLossModal.children().eq(2).children().eq(1);
|
const dataLossModalNo = dataLossModal.children().eq(2).children().eq(1);
|
||||||
const allModals = $('.modal');
|
const allModals = $('.modal');
|
||||||
@@ -23,6 +24,37 @@ const dataLossModalDisplay = function(){
|
|||||||
showModal(dataLossModal);
|
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){
|
function setMethodToCall(name, id){
|
||||||
methodToCall.name = name;
|
methodToCall.name = name;
|
||||||
methodToCall.id = id;
|
methodToCall.id = id;
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ var selectMenu = $("#selectMenuContent");
|
|||||||
var advancedTab = $("#advanced");
|
var advancedTab = $("#advanced");
|
||||||
var basicID = $("#basicItemData")
|
var basicID = $("#basicItemData")
|
||||||
var advancedID = $("#advancedItemData");
|
var advancedID = $("#advancedItemData");
|
||||||
|
var advancedUUIDOptions = $("#uuid-validation-strategy");
|
||||||
var focusedField = false;
|
var focusedField = false;
|
||||||
function changeAdvancedVisibility(){
|
function changeAdvancedVisibility(){
|
||||||
if(advancedVisibility){
|
if(advancedVisibility){
|
||||||
selectMenu.removeClass('active');
|
selectMenu.removeClass('active');
|
||||||
advancedTab.removeClass('active');
|
advancedTab.removeClass('active');
|
||||||
advancedID.removeClass('active');
|
advancedID.removeClass('active');
|
||||||
|
advancedUUIDOptions.removeClass('active');
|
||||||
basicID.addClass('active');
|
basicID.addClass('active');
|
||||||
advancedVisibility = false;
|
advancedVisibility = false;
|
||||||
}
|
}
|
||||||
@@ -16,6 +18,7 @@ function changeAdvancedVisibility(){
|
|||||||
selectMenu.addClass('active');
|
selectMenu.addClass('active');
|
||||||
advancedTab.addClass('active');
|
advancedTab.addClass('active');
|
||||||
advancedID.addClass('active');
|
advancedID.addClass('active');
|
||||||
|
advancedUUIDOptions.addClass('active');
|
||||||
basicID.removeClass('active');
|
basicID.removeClass('active');
|
||||||
advancedVisibility = true;
|
advancedVisibility = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user