aboutsummaryrefslogblamecommitdiffstats
path: root/src/main/java/com/terminaldweller/doc/DocService.java
blob: c8bab2363f8b55ee5c234920f9669f3babcede63 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                

                                                              




                                              






                                                  

                                         















                                                                           















                                                                                









                                                                        

   
package com.terminaldweller.doc;

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 Optional<Doc> getDocs(Long id) {
    return docRepository.findById(id);
  }

  /**
   * 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);
  }

  /**
   * Update a Document.
   *
   * @param doc the document to update.
   */
  public void updateDoc(Doc doc, Long id) {
    Optional<Doc> docOptional = docRepository.findById(id);
    if (!docOptional.isPresent()) {
      throw new IllegalStateException("Resource must be created before update");
    }
    doc.setId(id);
    doc.setLastModified(System.currentTimeMillis() / 1000L);
    doc.setBody(doc.getBody());
    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);
  }
}