Exemple #1
0
 @Test
 public void testMongoRepository() throws Exception {
   assertNotNull(todoRepository);
   List<Todo> todos = todoRepository.findByOwner("self");
   assertNotNull(todos);
   assertTrue(todos.size() > 0);
 }
  @Override
  public List<TodoDTO> findAll() {
    LOGGER.info("Finding all todo entries.");

    List<Todo> todoEntries = repository.findAll();

    LOGGER.info("Found {} todo entries", todoEntries.size());

    return convertToDTOs(todoEntries);
  }
  @Override
  public TodoDTO delete(String id) {
    LOGGER.info("Deleting a todo entry with id: {}", id);

    Todo deleted = findTodoById(id);
    repository.delete(deleted);

    LOGGER.info("Deleted todo entry with informtation: {}", deleted);

    return convertToDTO(deleted);
  }
  @Override
  public TodoDTO update(TodoDTO todo) {
    LOGGER.info("Updating todo entry with information: {}", todo);

    Todo updated = findTodoById(todo.getId());
    updated.update(todo.getTitle(), todo.getDescription());
    updated = repository.save(updated);

    LOGGER.info("Updated todo entry with information: {}", updated);

    return convertToDTO(updated);
  }
  @Override
  public TodoDTO create(TodoDTO todo) {
    LOGGER.info("Creating a new todo entry with information: {}", todo);

    Todo persisted =
        Todo.getBuilder().title(todo.getTitle()).description(todo.getDescription()).build();

    persisted = repository.save(persisted);
    LOGGER.info("Created a new todo entry with information: {}", persisted);

    return convertToDTO(persisted);
  }
  @Transactional(readOnly = true)
  @Override
  public Page<TodoDTO> findBySearchTerm(String searchTerm, Pageable pageRequest) {
    LOGGER.info(
        "Finding todo entries by search term: {} and page request: {}", searchTerm, pageRequest);

    Page<Todo> searchResultPage =
        repository.findAll(titleOrDescriptionContainsIgnoreCase(searchTerm), pageRequest);
    LOGGER.info(
        "Found {} todo entries. Returned page {} contains {} todo entries",
        searchResultPage.getTotalElements(),
        searchResultPage.getNumber(),
        searchResultPage.getNumberOfElements());

    return TodoMapper.mapEntityPageIntoDTOPage(pageRequest, searchResultPage);
  }
 private Todo findTodoById(String id) {
   Optional<Todo> result = repository.findOne(id);
   return result.orElseThrow(() -> new TodoNotFoundException(id));
 }