diff options
author | terminaldweller <thabogre@gmail.com> | 2022-03-17 15:37:17 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-03-17 15:37:17 +0000 |
commit | 70aa3558759921f7ec44adffec081ba1e9d7fb6a (patch) | |
tree | eef7dd6dee34d26ac10f64076cd9f7df25d77f88 /src/main/java/com/terminaldweller/doc/DocService.java | |
parent | daily commit.wip (diff) | |
download | mdrtl-70aa3558759921f7ec44adffec081ba1e9d7fb6a.tar.gz mdrtl-70aa3558759921f7ec44adffec081ba1e9d7fb6a.zip |
daily commit.wip
Diffstat (limited to '')
-rw-r--r-- | src/main/java/com/terminaldweller/doc/DocService.java | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/main/java/com/terminaldweller/doc/DocService.java b/src/main/java/com/terminaldweller/doc/DocService.java index 4aee30b..90fd9df 100644 --- a/src/main/java/com/terminaldweller/doc/DocService.java +++ b/src/main/java/com/terminaldweller/doc/DocService.java @@ -1,12 +1,48 @@ package com.terminaldweller.doc; import java.util.List; +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** The document service class. */ @Service public class DocService { + private final DocRepository docRepository; + + @Autowired + public DocService(DocRepository docRepository) { + this.docRepository = docRepository; + } + public List<Doc> getDocs() { - return List.of(new Doc(1L, "loco", 0L)); + return docRepository.findAll(); + // return List.of(new Doc(1L, "loco", 0L)); + } + + /** + * Adds a new Document to the data store. + * + * @param doc the new Document to add. + */ + public void addNewDoc(Doc doc) { + Optional<Doc> docOptional = docRepository.findDocByName(doc.getName()); + if (docOptional.isPresent()) { + throw new IllegalStateException("Id is already taken"); + } + docRepository.save(doc); + } + + /** + * Deletes a document from the data store. + * + * @param id The identifier for the document to be deleted. + */ + public void deleteDoc(Long id) { + boolean exists = docRepository.existsById(id); + if (!exists) { + throw new IllegalStateException("doc " + id + " does not exitst"); + } + docRepository.deleteById(id); } } |