From bc9fbb58dca69462f721c2ad29954fc2e969b62a Mon Sep 17 00:00:00 2001 From: KirillTepechin Date: Tue, 12 Dec 2023 03:39:24 +0400 Subject: [PATCH] forgot smth --- .../java/org/example/DocumentController.java | 48 ++++++++++++++++++ .../java/org/example/DocumentStorage.java | 50 +++++++++++++++++++ .../java/org/example/dto/DocumentInfo.java | 13 +++++ 3 files changed, 111 insertions(+) create mode 100644 tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentController.java create mode 100644 tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentStorage.java create mode 100644 tepechin_kirill_lab_3/opop-service/src/main/java/org/example/dto/DocumentInfo.java diff --git a/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentController.java b/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentController.java new file mode 100644 index 0000000..a744ec1 --- /dev/null +++ b/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentController.java @@ -0,0 +1,48 @@ +package org.example; + +import com.fasterxml.jackson.annotation.JsonView; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/document") +public class DocumentController { + + @Autowired + private DocumentStorage documentStorage; + + @JsonView(Views.Private.class) + @GetMapping + public List getAllDocuments(){ + return documentStorage.getAllDocuments(); + } + @JsonView(Views.Private.class) + @GetMapping("/{id}") + public Document getDocument(@PathVariable long id){ + return documentStorage.getDocument(id); + } + + @PostMapping + public Document addDocument(@RequestBody @JsonView(Views.Public.class) Document document){ + return documentStorage.addDocument(document); + } + @PutMapping + public void editDocument(@RequestBody Document opopDtoList){ + documentStorage.editDocument(opopDtoList); + } + + @DeleteMapping("/{id}") + public void deleteDocument(@PathVariable long id){ + documentStorage.deleteDocument(id); + } + + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(Exception.class) + public String handleNotFound(Exception e) { + return e.getMessage(); + } + +} diff --git a/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentStorage.java b/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentStorage.java new file mode 100644 index 0000000..df0f95b --- /dev/null +++ b/tepechin_kirill_lab_3/document-service/src/main/java/org/example/DocumentStorage.java @@ -0,0 +1,50 @@ +package org.example; + +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class DocumentStorage { + private long currentId = 1; + private static final List documentList = new ArrayList<>(); + + public List getAllDocuments(){ + return documentList; + } + + public Document getDocument(long id){ + return documentList.get(findIndexById(id)); + } + + public Document addDocument(Document document){ + long id = currentId++; + document.setId(id); + + documentList.add(document); + return document; + } + + public void deleteDocument(long id){ + documentList.remove(documentList.get(findIndexById(id))); + } + + public void editDocument(Document document){ + int index = findIndexById(document.getId()); + Document docFromDb = documentList.get(index); + docFromDb.setText(document.getText()); + + documentList.set(index, docFromDb); + } + + private int findIndexById(long idToFind) { + for (int i = 0; i < documentList.size(); i++) { + Document obj = documentList.get(i); + if (obj.getId() == idToFind) { + return i; + } + } + return -1; + } +} diff --git a/tepechin_kirill_lab_3/opop-service/src/main/java/org/example/dto/DocumentInfo.java b/tepechin_kirill_lab_3/opop-service/src/main/java/org/example/dto/DocumentInfo.java new file mode 100644 index 0000000..285078e --- /dev/null +++ b/tepechin_kirill_lab_3/opop-service/src/main/java/org/example/dto/DocumentInfo.java @@ -0,0 +1,13 @@ +package org.example.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class DocumentInfo { + private long id; + private String text; +}