add Script/tag link

version_2
floxx2112 1 year ago
parent 09a954a2e2
commit c4f5863ce0
  1. 12
      src/main/java/com/apside/assistDbBackend/controller/ScriptController.java
  2. 22
      src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java
  3. 101
      src/main/java/com/apside/assistDbBackend/service/ScriptsService.java
  4. 8
      src/main/resources/application.properties

@ -1,5 +1,6 @@
package com.apside.assistDbBackend.controller; package com.apside.assistDbBackend.controller;
import com.apside.assistDbBackend.model.LinkScriptTag;
import com.apside.assistDbBackend.model.Script; import com.apside.assistDbBackend.model.Script;
import com.apside.assistDbBackend.service.ScriptsService; import com.apside.assistDbBackend.service.ScriptsService;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
@ -21,6 +22,11 @@ public class ScriptController {
return scriptsService.retrieveScripts(); return scriptsService.retrieveScripts();
} }
@GetMapping("/scripts/link")
public List<LinkScriptTag> getAllLinkScriptsTags() throws IOException, GitAPIException {
return scriptsService.getAllScriptTag();
}
@DeleteMapping("/script/delete/{name}") @DeleteMapping("/script/delete/{name}")
public void deleteScript(@PathVariable("name") final String name){ public void deleteScript(@PathVariable("name") final String name){
scriptsService.deleteOneScript(name); scriptsService.deleteOneScript(name);
@ -28,14 +34,16 @@ public class ScriptController {
@GetMapping("/script/add") @GetMapping("/script/add")
public void addScript(@RequestParam("content") String content, @RequestParam("name") String name) throws IOException, GitAPIException, URISyntaxException { public void addScript(@RequestParam("content") String content, @RequestParam("name") String name, @RequestParam("tagList") List<String> tagsList, @RequestParam("linkid") int linkId) throws IOException, GitAPIException, URISyntaxException {
scriptsService.addOneScript(content, name); scriptsService.addOneScript(content, name);
scriptsService.addOneLink(name, tagsList, linkId);
} }
@GetMapping("/script/edit") @GetMapping("/script/edit")
public void editScript(@RequestParam("content") String content, @RequestParam("defaultname") String defaultName, @RequestParam("newname") String newName) throws IOException, GitAPIException, URISyntaxException { public void editScript(@RequestParam("content") String content, @RequestParam("defaultname") String defaultName, @RequestParam("newname") String newName, @RequestParam("tagList") List<String> tagsList, @RequestParam("linkid") int linkId) throws IOException, GitAPIException, URISyntaxException {
scriptsService.simpleDeleteScript(defaultName); scriptsService.simpleDeleteScript(defaultName);
scriptsService.addOneScript(content, newName); scriptsService.addOneScript(content, newName);
scriptsService.updateOneLink(newName, tagsList, linkId);
} }

@ -0,0 +1,22 @@
package com.apside.assistDbBackend.model;
import lombok.Data;
import java.util.List;
@Data
public class LinkScriptTag {
private int id;
private String scriptName;
private List<String> tags;
public void LinkScriptTag(){}
public LinkScriptTag(int id, String scriptName, List<String> tags){
this.id = id;
this.scriptName = scriptName;
this.tags = tags;
}
}

@ -1,5 +1,6 @@
package com.apside.assistDbBackend.service; package com.apside.assistDbBackend.service;
import com.apside.assistDbBackend.model.LinkScriptTag;
import com.apside.assistDbBackend.model.Script; import com.apside.assistDbBackend.model.Script;
import lombok.Data; import lombok.Data;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
@ -9,6 +10,8 @@ import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.transport.PushResult; import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties; import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
@ -21,15 +24,31 @@ import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.nio.file.Paths;
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
import java.util.List;
@Data @Data
@Service @Service
public class ScriptsService { public class ScriptsService {
private final String linkScriptTagPath;
private final Path pathOfLink;
private String allScriptsContent;
private JSONObject dataGlobalScripts;
private JSONArray dataLinkScriptsTags;
public ScriptsService() throws IOException {
linkScriptTagPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/scripts.json";
pathOfLink = Paths.get(linkScriptTagPath);
}
private void initialize() throws IOException {
allScriptsContent = new String((Files.readAllBytes(pathOfLink)));
dataGlobalScripts = new JSONObject(allScriptsContent);
dataLinkScriptsTags = dataGlobalScripts.getJSONArray("data");
System.out.println(dataLinkScriptsTags);
}
public List<Script> retrieveScripts() throws IOException, GitAPIException { public List<Script> retrieveScripts() throws IOException, GitAPIException {
pullFromGit(); pullFromGit();
List<Script> listOfScripts = new ArrayList<>(); List<Script> listOfScripts = new ArrayList<>();
@ -49,6 +68,29 @@ public class ScriptsService {
return listOfScripts; return listOfScripts;
} }
public List<LinkScriptTag> getAllScriptTag() throws IOException {
initialize();
List<LinkScriptTag> listLinkScriptTag = new ArrayList<>();
if (dataLinkScriptsTags.length()>0){
for (int i = 0; i<dataLinkScriptsTags.length(); i++){
JSONObject link = dataLinkScriptsTags.getJSONObject(i);
int linkId = link.getInt("id");
String scriptName = link.getString("filename");
List<String> tagList = new ArrayList<>();
JSONArray tags = link.getJSONArray("tags");
for (int j=0; j<tags.length(); j++){
JSONObject unitTag = tags.getJSONObject(j);
String tagName = unitTag.getString("tagname");
tagList.add(tagName);
}
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, tagList);
listLinkScriptTag.add(newLinkScriptTag);
}
}
return listLinkScriptTag;
}
public void deleteOneScript(final String name){ public void deleteOneScript(final String name){
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
try { try {
@ -79,9 +121,60 @@ public class ScriptsService {
writerDataFile.write(content); writerDataFile.write(content);
writerDataFile.close(); writerDataFile.close();
}
public void addOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
initialize();
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put("tagname", tagList.get(i).toString());
tagArray.put(tempTag);
}
newLink.put("tags", tagArray);
JSONArray newArr = dataLinkScriptsTags.put(newLink);
JSONObject newObj = dataGlobalScripts.put("data", newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
pushToGit(); pushToGit();
} }
public void updateOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
initialize();
JSONArray newArr = new JSONArray();
for (int d = 0; d<dataLinkScriptsTags.length(); d++){
JSONObject actualLink = dataLinkScriptsTags.getJSONObject(d);
int actualId = actualLink.getInt("id");
if (!Objects.equals(linkId, actualId)){
newArr.put(actualLink);
} else {
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put("tagname", tagList.get(i).toString());
tagArray.put(tempTag);
}
newLink.put("tags", tagArray);
newArr.put(newLink);
}
}
JSONObject newObj = dataGlobalScripts.put("data", newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
pushToGit();
}
@Value("${USERNAME_GIT}") @Value("${USERNAME_GIT}")

@ -1,12 +1,12 @@
spring.application.name=AssistDBBackend spring.application.name=AssistDBBackend
spring.datasource.url=jdbc:mysql://localhost:3306/prudencetest spring.datasource.url=jdbc:mysql://localhost:3306/prudencetest
spring.datasource.username= spring.datasource.username=root
spring.datasource.password= spring.datasource.password=Pompom.21
server.port=9001 server.port=9001
USERNAME_GIT = USERNAME_GIT = fhibert@apside.fr
ACCESS_TOKEN_GIT = ACCESS_TOKEN_GIT = e54612b6d1d73eef4f0a49c88b0e35ccf02d45eb
logging.level.org.springframework.boot.web.embedded.tomcat=INFO logging.level.org.springframework.boot.web.embedded.tomcat=INFO
logging.level.org.springframework=error logging.level.org.springframework=error

Loading…
Cancel
Save