From 8512a97924efd4793d8229f2a1584570f90b887b Mon Sep 17 00:00:00 2001 From: floxx2112 Date: Mon, 24 Apr 2023 16:58:45 +0200 Subject: [PATCH] opti + end of multiple functionnalitties --- .../controller/ScriptController.java | 8 +-- .../controller/TagsController.java | 10 +-- .../assistDbBackend/model/LinkScriptTag.java | 5 +- .../apside/assistDbBackend/model/Script.java | 1 + .../assistDbBackend/service/GitService.java | 50 +++++++++++++++ .../service/ScriptsService.java | 63 +++++-------------- .../assistDbBackend/service/TagsService.java | 27 +++++--- 7 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/apside/assistDbBackend/service/GitService.java diff --git a/src/main/java/com/apside/assistDbBackend/controller/ScriptController.java b/src/main/java/com/apside/assistDbBackend/controller/ScriptController.java index c8536d5..845cbaf 100644 --- a/src/main/java/com/apside/assistDbBackend/controller/ScriptController.java +++ b/src/main/java/com/apside/assistDbBackend/controller/ScriptController.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 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 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 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 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); } diff --git a/src/main/java/com/apside/assistDbBackend/controller/TagsController.java b/src/main/java/com/apside/assistDbBackend/controller/TagsController.java index 437e81f..04dd3a8 100644 --- a/src/main/java/com/apside/assistDbBackend/controller/TagsController.java +++ b/src/main/java/com/apside/assistDbBackend/controller/TagsController.java @@ -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 getTags() throws IOException { + public List 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); } } diff --git a/src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java b/src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java index c816271..29330c5 100644 --- a/src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java +++ b/src/main/java/com/apside/assistDbBackend/model/LinkScriptTag.java @@ -10,13 +10,16 @@ public class LinkScriptTag { private String scriptName; + private String description; + private List tags; public void LinkScriptTag(){} - public LinkScriptTag(int id, String scriptName, List tags){ + public LinkScriptTag(int id, String scriptName, String description, List tags){ this.id = id; this.scriptName = scriptName; + this.description = description; this.tags = tags; } } diff --git a/src/main/java/com/apside/assistDbBackend/model/Script.java b/src/main/java/com/apside/assistDbBackend/model/Script.java index fb9e18f..45188c2 100644 --- a/src/main/java/com/apside/assistDbBackend/model/Script.java +++ b/src/main/java/com/apside/assistDbBackend/model/Script.java @@ -10,6 +10,7 @@ public class Script { private String data; + public Script(){} public Script(String extension, String name, String data){ diff --git a/src/main/java/com/apside/assistDbBackend/service/GitService.java b/src/main/java/com/apside/assistDbBackend/service/GitService.java new file mode 100644 index 0000000..c568ec1 --- /dev/null +++ b/src/main/java/com/apside/assistDbBackend/service/GitService.java @@ -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(); + } +} diff --git a/src/main/java/com/apside/assistDbBackend/service/ScriptsService.java b/src/main/java/com/apside/assistDbBackend/service/ScriptsService.java index 90e3d0f..94ea0d7 100644 --- a/src/main/java/com/apside/assistDbBackend/service/ScriptsService.java +++ b/src/main/java/com/apside/assistDbBackend/service/ScriptsService.java @@ -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