diff --git a/src/main/java/com/apside/assistDbBackend/controller/TagsController.java b/src/main/java/com/apside/assistDbBackend/controller/TagsController.java new file mode 100644 index 0000000..437e81f --- /dev/null +++ b/src/main/java/com/apside/assistDbBackend/controller/TagsController.java @@ -0,0 +1,44 @@ +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/api") +public class TagsController { + + @Autowired + private TagsService tagsService; + + @GetMapping("/tags/all") + public List getTags() throws IOException { + return tagsService.getAllTags(); + } + + /*@DeleteMapping("/tags/deleteAll") + public void deleteAllTags() { + tagsService.deleteAllTags(); + }*/ + + @DeleteMapping("/tag/delete/{nameTag}") + public void deleteTag(@PathVariable("nameTag") final String nameTag) throws IOException { + tagsService.deleteTag(nameTag); + } + + @PostMapping("/tag/add") + public void addTag(@RequestBody Tag tag) throws IOException { + tagsService.addTag(tag); + } + + @PutMapping("/tag/update/{prevTag}") + public void updateTag(@PathVariable("prevTag") final String prevTag, @RequestBody Tag tag) throws IOException { + tagsService.updateTag(prevTag, tag); + } +} diff --git a/src/main/java/com/apside/assistDbBackend/model/Tag.java b/src/main/java/com/apside/assistDbBackend/model/Tag.java new file mode 100644 index 0000000..7050350 --- /dev/null +++ b/src/main/java/com/apside/assistDbBackend/model/Tag.java @@ -0,0 +1,23 @@ +package com.apside.assistDbBackend.model; + +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Data; + +@Data +public class Tag { + + private int tagId; + + private String nameTag; + + private String descriptionTag; + + public Tag(){} + + public Tag(int tagId, String nameTag, String descriptionTag){ + this.tagId = tagId; + this.nameTag = nameTag; + this.descriptionTag = descriptionTag; + } +} diff --git a/src/main/java/com/apside/assistDbBackend/service/TagsService.java b/src/main/java/com/apside/assistDbBackend/service/TagsService.java new file mode 100644 index 0000000..6a316fc --- /dev/null +++ b/src/main/java/com/apside/assistDbBackend/service/TagsService.java @@ -0,0 +1,105 @@ +package com.apside.assistDbBackend.service; + +import com.apside.assistDbBackend.model.Tag; +import lombok.Data; +import org.json.JSONArray; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@Data +@Service +public class TagsService { + + private final String tempDirectoryPath; + private final Path path; + private String jsonContent; + private JSONObject dataGlobal; + private JSONArray data; + public TagsService() throws IOException { + tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json"; + path = Paths.get(tempDirectoryPath); + } + + private void initialize() throws IOException { + jsonContent = new String(Files.readAllBytes(path)); + dataGlobal = new JSONObject(jsonContent); + data = dataGlobal.getJSONArray("data"); + } + + public List getAllTags() throws IOException { + initialize(); + List listOfTag = new ArrayList<>(); + for (int i=0; i