forgot smth
This commit is contained in:
parent
1ca5dad17e
commit
bc9fbb58dc
@ -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<Document> 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();
|
||||
}
|
||||
|
||||
}
|
@ -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<Document> documentList = new ArrayList<>();
|
||||
|
||||
public List<Document> 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user