package com.kubilaycicek.todo_service.service import com.kubilaycicek.todo_service.data.dto.TodoDTO import com.kubilaycicek.todo_service.data.model.Todo import com.kubilaycicek.todo_service.repository.TodoRepository import org.springframework.stereotype.Service import java.util.* @Service class TodoServiceImpl(var todoRepository: TodoRepository) : TodoService { override fun updateTodo(todoDTO: TodoDTO): TodoDTO { val todo: Optional = todoRepository.findById(todoDTO.id); if (todo.isPresent) { val todoDb: Todo = todo.get() todoDb.name = todoDTO.title todoDb.description = todoDTO.description todoDb.completed = todoDTO.completed todoRepository.save(todoDb) return TodoDTO( id = todoDb.id, title = todoDb.name, description = todoDb.description, completed = todoDb.completed ) } else return throw Exception() } override fun saveTodo(todoDTO: TodoDTO): TodoDTO { val todoModel: Todo = Todo( id = todoDTO.id, name = todoDTO.title, description = todoDTO.description, completed = todoDTO.completed ); val savedTodo = todoRepository.save(todoModel); return TodoDTO( id = savedTodo.id, title = savedTodo.name, description = savedTodo.description, completed = savedTodo.completed ) } override fun getTodo(id: Long): TodoDTO? { val todo: Optional = todoRepository.findById(id); if (todo.isPresent) { val todoDb: Todo = todo.get() return TodoDTO( id = todoDb.id, title = todoDb.name, description = todoDb.description, completed = todoDb.completed ) } else return null } override fun deleteTodo(id: Long) { todoRepository.deleteById(id) } override fun getTodoList(): MutableList { var todoDtoList = mutableListOf() todoRepository.findAll().forEach { val add = TodoDTO( id = it.id, title = it.name, description = it.description, completed = it.completed ) todoDtoList.add(add) } return todoDtoList } }