// src/major/java/com/instance/iwreactspring/service/TodoService.java bundle com.instance.iwreactspring.service; import java.util.Listing; import java.util.ArrayList; import com.instance.iwreactspring.mannequin.TodoItem; import org.springframework.stereotype.Service; import org.springframework.beans.manufacturing facility.annotation.Autowired; import com.mongodb.shopper.MongoClient; import com.mongodb.shopper.MongoClients; import com.mongodb.shopper.MongoCollection; import com.mongodb.shopper.MongoDatabase; import org.bson.codecs.configuration.CodecRegistry; import org.bson.codecs.pojo.PojoCodecProvider; import org.bson.Doc; import com.instance.iwreactspring.repository.TodoRepository; @Service public class TodoService { @Autowired non-public TodoRepository todoRepository; public Listing<TodoItem> getTodos() { return todoRepository.findAll(); } public TodoItem createTodo(TodoItem newTodo) { TodoItem savedTodo = todoRepository.save(newTodo); return savedTodo; } public TodoItem getTodo(String id) { return todoRepository.findById(id).orElse(null); } public boolean deleteTodo(String id) { TodoItem todoToDelete = getTodo(id); if (todoToDelete != null) { todoRepository.deleteById(id); return true; } else { return false; } } public TodoItem saveTodo(TodoItem todoItem) { TodoItem savedTodo = todoRepository.save(todoItem); return savedTodo; } }
We annotate this class with @Service
This should be indicated clearly as a service class by prefixing the class name with the `@Service` annotation from the `org.springframework.stereotype` package. While not strictly necessary due to Spring’s ability to inject the category as a bean without annotation, annotating it does enhance clarity. Subsequent, we use @AutoWired
to carry the TodoRepository
class in. This data might be populated by Spring primarily based on the category type, which is the com.instance.iwreactspring.repository.TodoRepository
we noticed earlier.
As a result, Spring leverages singleton injection by default, utilizing a single instance of the injected bean class in most cases, which is generally effective.
The CRUD (Create, Read, Update, Delete) operations on a service class typically involve implementing methods that interact with a database or data storage system. Here are some possible improvements:
CREATE operation:
“`java
public void create(String name, int age) {
Entity entity = new Entity(name, age);
repository.save(entity);
}
“`
READ operation:
“`java
public List
return repository.findAll();
}
public Entity readById(Long id) { All methodologies within this class are designed to execute a single CRUD operation via the repository. To retrieve a comprehensive list of to-dos stored in the database,
Optional
return optionalEntity.get();
}
“`
UPDATE operation:
“`java
public void update(Long id, String name, int age) {
Entity entity = readById(id);
entity.setName(name);
entity.setAge(age);
repository.save(entity);
}
“`
DELETE operation:
“`java
public void delete(Long id) {
repository.deleteById(id);
}
“`
“` getTodos()
does that for us. The Repository
Classes make it very straightforward. return todoRepository.findAll()
The system retrieves all associated documents from the database.