callingmahendra / springboot3-sample

Springboot sample Repo
Apache License 2.0
0 stars 0 forks source link

Code review comments for the code #1

Open callingmahendra opened 2 months ago

callingmahendra commented 2 months ago

Review code and provide comments

callingmahendra commented 2 months ago
package me.mahendra.spring_demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.mahendra.spring_demo.entities.Todo;
import me.mahendra.spring_demo.repository.TodoRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service class for managing Todo entities.
 */
@Service
@Transactional 
public class TodoService {

    private final TodoRepository todoRepository;

    @Autowired
    public TodoService(TodoRepository todoRepository) {
        this.todoRepository = todoRepository;
    }

    /**
     * Retrieves all Todo entries.
     * @return A list of all Todo entries.
     */
    public List<Todo> getAllTodos() {
        return todoRepository.findAll();
    }

    /**
     * Retrieves all completed Todo entries.
     * @return A list of completed Todo entries.
     */
    public List<Todo> getCompletedTodos() {
        return todoRepository.findAllByCompleted(true);
    }

    /**
     * Creates a new Todo entry.
     * @param todo The Todo object to create.
     * @return The created Todo entry.
     */
    public Todo createTodo(Todo todo) {
        return todoRepository.save(todo);
    }

    /**
     * Updates an existing Todo entry.
     * @param id The ID of the Todo entry to update.
     * @param todo The updated Todo object.
     * @return The updated Todo entry, or null if not found.
     */
    public Todo updateTodo(Long id, Todo todo) {
        return todoRepository.findById(id)
                .map(existingTodo -> {
                    existingTodo.setTitle(todo.getTitle());
                    existingTodo.setCompleted(todo.isCompleted());
                    return todoRepository.save(existingTodo); 
                })
                .orElseThrow(() -> new TodoNotFoundException("Todo not found with id: " + id)); 
    }

    /**
     * Deletes a Todo entry.
     * @param id The ID of the Todo entry to delete.
     * @throws TodoNotFoundException if Todo with the given id is not found.
     */
    public void deleteTodo(Long id) {
        if (!todoRepository.existsById(id)) {
            throw new TodoNotFoundException("Todo not found with id: " + id);
        }
        todoRepository.deleteById(id);
    }
}

Suggested Changes & Comments

  1. Constructor-based Dependency Injection: Use constructor-based injection for TodoRepository for improved testability and to make dependencies explicit.

  2. @Transactional: Added @Transactional annotation to the service class. This ensures that all database operations within a single service method are treated as a single transaction.

  3. Javadoc Comments: Added Javadoc comments to clearly document the purpose of each method in the TodoService class.

  4. Error Handling:

    • Instead of returning null when a Todo is not found during update, it's better to throw a custom exception, TodoNotFoundException (You will need to create this class).
    • Added a check in deleteTodo to throw TodoNotFoundException if a Todo with the given ID doesn't exist.
  5. Naming Consistency: Ensured consistent naming conventions for variables and methods.

Additional Considerations

This enhanced TodoService is more robust, maintainable, and follows best practices.