opti + end of multiple functionnalitties

version_2
floxx2112 2 years ago
parent c4f5863ce0
commit 8512a97924
  1. 8
      src/main/java/com/apside/assistDbBackend/controller/ScriptController.java
  2. 10
      src/main/java/com/apside/assistDbBackend/controller/TagsController.java
  3. 5
      src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java
  4. 1
      src/main/java/com/apside/assistDbBackend/model/Script.java
  5. 50
      src/main/java/com/apside/assistDbBackend/service/GitService.java
  6. 63
      src/main/java/com/apside/assistDbBackend/service/ScriptsService.java
  7. 27
      src/main/java/com/apside/assistDbBackend/service/TagsService.java

@ -34,16 +34,16 @@ public class ScriptController {
@GetMapping("/script/add")
public void addScript(@RequestParam("content") String content, @RequestParam("name") String name, @RequestParam("tagList") List<String> tagsList, @RequestParam("linkid") int linkId) throws IOException, GitAPIException, URISyntaxException {
public void addScript(@RequestParam("content") String content, @RequestParam("name") String name, @RequestParam("desc") String description, @RequestParam("tagList") List<String> tagsList, @RequestParam("linkid") int linkId) throws IOException, GitAPIException, URISyntaxException {
scriptsService.addOneScript(content, name);
scriptsService.addOneLink(name, tagsList, linkId);
scriptsService.addOneLink(name, description, tagsList, linkId);
}
@GetMapping("/script/edit")
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 {
public void editScript(@RequestParam("content") String content, @RequestParam("defaultname") String defaultName, @RequestParam("newname") String newName, @RequestParam("desc") String description, @RequestParam("tagList") List<String> tagsList, @RequestParam("linkid") int linkId) throws IOException, GitAPIException, URISyntaxException {
scriptsService.simpleDeleteScript(defaultName);
scriptsService.addOneScript(content, newName);
scriptsService.updateOneLink(newName, tagsList, linkId);
scriptsService.updateOneLink(newName, description, tagsList, linkId);
}

@ -3,10 +3,12 @@ package com.apside.assistDbBackend.controller;
import com.apside.assistDbBackend.model.InfoColumn;
import com.apside.assistDbBackend.model.Tag;
import com.apside.assistDbBackend.service.TagsService;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
@ -18,7 +20,7 @@ public class TagsController {
private TagsService tagsService;
@GetMapping("/tags/all")
public List<Tag> getTags() throws IOException {
public List<Tag> getTags() throws IOException, GitAPIException {
return tagsService.getAllTags();
}
@ -28,17 +30,17 @@ public class TagsController {
}*/
@DeleteMapping("/tag/delete/{nameTag}")
public void deleteTag(@PathVariable("nameTag") final String nameTag) throws IOException {
public void deleteTag(@PathVariable("nameTag") final String nameTag) throws IOException, GitAPIException, URISyntaxException {
tagsService.deleteTag(nameTag);
}
@PostMapping("/tag/add")
public void addTag(@RequestBody Tag tag) throws IOException {
public void addTag(@RequestBody Tag tag) throws IOException, GitAPIException, URISyntaxException {
tagsService.addTag(tag);
}
@PutMapping("/tag/update/{prevTag}")
public void updateTag(@PathVariable("prevTag") final String prevTag, @RequestBody Tag tag) throws IOException {
public void updateTag(@PathVariable("prevTag") final String prevTag, @RequestBody Tag tag) throws IOException, GitAPIException, URISyntaxException {
tagsService.updateTag(prevTag, tag);
}
}

@ -10,13 +10,16 @@ public class LinkScriptTag {
private String scriptName;
private String description;
private List<String> tags;
public void LinkScriptTag(){}
public LinkScriptTag(int id, String scriptName, List<String> tags){
public LinkScriptTag(int id, String scriptName, String description, List<String> tags){
this.id = id;
this.scriptName = scriptName;
this.description = description;
this.tags = tags;
}
}

@ -10,6 +10,7 @@ public class Script {
private String data;
public Script(){}
public Script(String extension, String name, String data){

@ -0,0 +1,50 @@
package com.apside.assistDbBackend.service;
import lombok.Data;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
@Data
@Service
public class GitService {
private final String tempDirectoryPath;
public GitService(){
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
}
@Value("${USERNAME_GIT}")
private String userGit;
@Value("${ACCESS_TOKEN_GIT}")
private String accesToken;
public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
Git git = Git.open(new File(tempDirectoryPath));
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
// you can add more settings here if needed
remoteAddCommand.call();
git.add().setUpdate(true).addFilepattern(".").call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
}
public void pullFromGit() throws IOException, GitAPIException {
Git git = Git.open(new File(tempDirectoryPath));
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
}
}

@ -30,7 +30,8 @@ import java.util.*;
@Data
@Service
public class ScriptsService {
@Autowired
private GitService gitService;
private final String linkScriptTagPath;
private final Path pathOfLink;
private String allScriptsContent;
@ -46,11 +47,10 @@ public class ScriptsService {
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 {
pullFromGit();
gitService.pullFromGit();
List<Script> listOfScripts = new ArrayList<>();
//Creating a File object for directory
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
@ -76,6 +76,7 @@ public class ScriptsService {
JSONObject link = dataLinkScriptsTags.getJSONObject(i);
int linkId = link.getInt("id");
String scriptName = link.getString("filename");
String desc = link.getString("description");
List<String> tagList = new ArrayList<>();
JSONArray tags = link.getJSONArray("tags");
for (int j=0; j<tags.length(); j++){
@ -83,7 +84,7 @@ public class ScriptsService {
String tagName = unitTag.getString("tagname");
tagList.add(tagName);
}
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, tagList);
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, desc, tagList);
listLinkScriptTag.add(newLinkScriptTag);
}
}
@ -96,8 +97,8 @@ public class ScriptsService {
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
pushToGit();
pullFromGit();
gitService.pushToGit();
gitService.pullFromGit();
} catch (Exception e){
e.printStackTrace();
}
@ -124,11 +125,13 @@ public class ScriptsService {
}
public void addOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
public void addOneLink(final String name, final String description, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
initialize();
gitService.pullFromGit();
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
newLink.put("description", description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
@ -139,11 +142,12 @@ public class ScriptsService {
JSONArray newArr = dataLinkScriptsTags.put(newLink);
JSONObject newObj = dataGlobalScripts.put("data", newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
pushToGit();
gitService.pushToGit();
}
public void updateOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
public void updateOneLink(final String name, final String description, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
initialize();
gitService.pullFromGit();
JSONArray newArr = new JSONArray();
for (int d = 0; d<dataLinkScriptsTags.length(); d++){
JSONObject actualLink = dataLinkScriptsTags.getJSONObject(d);
@ -154,6 +158,7 @@ public class ScriptsService {
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
newLink.put("description", description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
@ -167,45 +172,7 @@ public class ScriptsService {
JSONObject newObj = dataGlobalScripts.put("data", newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
pushToGit();
}
@Value("${USERNAME_GIT}")
private String userGit;
@Value("${ACCESS_TOKEN_GIT}")
private String accesToken;
public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
Git git = Git.open(new File(tempDirectoryPath));
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
// you can add more settings here if needed
remoteAddCommand.call();
git.add().setUpdate(true).addFilepattern(".").call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
}
public void pullFromGit() throws IOException, GitAPIException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
gitService.pushToGit();
Git git = Git.open(new File(tempDirectoryPath));
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
}
}

@ -2,6 +2,7 @@ package com.apside.assistDbBackend.service;
import com.apside.assistDbBackend.model.Tag;
import lombok.Data;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
@ -10,6 +11,7 @@ import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -21,7 +23,8 @@ import java.util.Optional;
@Data
@Service
public class TagsService {
@Autowired
private GitService gitService;
private final String tempDirectoryPath;
private final Path path;
private String jsonContent;
@ -38,7 +41,8 @@ public class TagsService {
data = dataGlobal.getJSONArray("data");
}
public List<Tag> getAllTags() throws IOException {
public List<Tag> getAllTags() throws IOException, GitAPIException {
gitService.pullFromGit();
initialize();
List<Tag> listOfTag = new ArrayList<>();
for (int i=0; i<data.length(); i++){
@ -52,7 +56,7 @@ public class TagsService {
return listOfTag;
}
public void deleteTag(final String deleteNameTag) throws IOException {
public void deleteTag(final String deleteNameTag) throws IOException, GitAPIException, URISyntaxException {
initialize();
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
@ -65,14 +69,13 @@ public class TagsService {
System.out.println(newArr);
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
}
/*public void deleteAllTags() {
tagsRepository.deleteAll();
}*/
public void addTag(Tag tag) throws IOException {
public void addTag(Tag tag) throws IOException, GitAPIException, URISyntaxException {
initialize();
gitService.pullFromGit();
JSONObject newTag = new JSONObject();
newTag.put("tagId", tag.getTagId());
newTag.put("tag", tag.getNameTag().toString());
@ -80,10 +83,12 @@ public class TagsService {
JSONArray newArr = data.put(newTag);
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
}
public void updateTag(final String prevTag, Tag modTag) throws IOException {
public void updateTag(final String prevTag, Tag modTag) throws IOException, GitAPIException, URISyntaxException {
initialize();
gitService.pullFromGit();
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
@ -100,6 +105,8 @@ public class TagsService {
}
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
}
}

Loading…
Cancel
Save