tag fuction

version_2
floxx2112 2 years ago
parent bd23448f42
commit 8e1bd3a7a8
  1. 44
      src/main/java/com/apside/assistDbBackend/controller/TagsController.java
  2. 23
      src/main/java/com/apside/assistDbBackend/model/Tag.java
  3. 105
      src/main/java/com/apside/assistDbBackend/service/TagsService.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<Tag> 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);
}
}

@ -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;
}
}

@ -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<Tag> getAllTags() throws IOException {
initialize();
List<Tag> listOfTag = new ArrayList<>();
for (int i=0; i<data.length(); i++){
JSONObject tag = data.getJSONObject(i);
int tagId = tag.getInt("tagId");
String tagName = tag.getString("tag");
String tagDescription = tag.getString("description");
Tag tempTag = new Tag(tagId, tagName, tagDescription);
listOfTag.add(tempTag);
}
return listOfTag;
}
public void deleteTag(final String deleteNameTag) throws IOException {
initialize();
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString("tag");
if(!Objects.equals(tagName, deleteNameTag)){
newArr.put(tag);
}
}
System.out.println(newArr);
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
}
/*public void deleteAllTags() {
tagsRepository.deleteAll();
}*/
public void addTag(Tag tag) throws IOException {
initialize();
JSONObject newTag = new JSONObject();
newTag.put("tagId", tag.getTagId());
newTag.put("tag", tag.getNameTag().toString());
newTag.put("description", tag.getDescriptionTag().toString());
JSONArray newArr = data.put(newTag);
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
}
public void updateTag(final String prevTag, Tag modTag) throws IOException {
initialize();
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString("tag");
if(!Objects.equals(tagName, prevTag)){
newArr.put(tag);
} else {
JSONObject newTag = new JSONObject();
newTag.put("tagId", modTag.getTagId());
newTag.put("tag", modTag.getNameTag().toString());
newTag.put("description", modTag.getDescriptionTag().toString());
newArr.put(newTag);
}
}
JSONObject newObj = dataGlobal.put("data", newArr);
Files.write(path, newObj.toString().getBytes());
}
}
Loading…
Cancel
Save