EXPERIMENT10 Ejava
EXPERIMENT10 Ejava
Develop a simple task management system using Spring Boot to help users
organize their tasks efficiently. The system should allow users to create, update,
delete, and mark tasks as completed.
Code
Todolist.java
package pl.mbaracz.todoapp.dto;
import pl.mbaracz.todoapp.model.TodoList;
import pl.mbaracz.todoapp.model.TodoTask;
import java.time.LocalDateTime;
import java.util.ArrayList; import
java.util.List;
import java.util.UUID;
public record
TodoListDto( UUID id,
List<TodoTaskDto> tasks,
LocalDateTime createdAt,
LocalDateTime updatedAt
){
public static TodoListDto toDto(TodoList todoList)
{ List<TodoTaskDto> tasks = todoList.getTasks()
.stream()
.map(TodoTaskDto::toDto)
.toList();
todoList.addTasks(tasks);
return todoList;
}
}
TodoTaskDto.java
package pl.mbaracz.todoapp.dto;
import pl.mbaracz.todoapp.model.TodoTask;
Control
package pl.mbaracz.todoapp.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity; import
org.springframework.web.bind.annotation.*; import
pl.mbaracz.todoapp.dto.TodoListDto;
import pl.mbaracz.todoapp.service.TodoService;
import java.util.UUID;
@RestController @RequestMapping("/api/todos")
@CrossOrigin("http://localhost:5173")
@RequiredArgsConstructor
public class TodoController {
@GetMapping("/{id}")
public ResponseEntity<TodoListDto> findTodoListById(@PathVariable UUID id) { TodoListDto
todoListDto = todoService.findTodoListById(id);
if (todoListDto == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(todoListDto);
}
@PutMapping
public ResponseEntity<UUID> createTodoList(@RequestBody TodoListDto todoListDto) { UUID
todoListId = todoService.createTodoList(todoListDto);
return ResponseEntity.ok(todoListId);
}
@PatchMapping
public ResponseEntity<?> updateTodoList(@RequestBody TodoListDto todoListDto) { boolean
updated = todoService.updateTodoList(todoListDto);
if (updated) {
return ResponseEntity.ok().build();
}
return ResponseEntity.notFound().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteTodoList(@PathVariable UUID id) { boolean
result = todoService.deleteTodoList(id);
if (result) {
return ResponseEntity.ok().build();
}
return ResponseEntity.notFound().build();
}
}
Model
package pl.mbaracz.todoapp.model;
import java.time.LocalDateTime;
import java.util.ArrayList; import
java.util.List;
import java.util.UUID;
@Entity
@Table(name = "todo_lists")
@Getter @AllArgsConstructor
@NoArgsConstructor
public class TodoList {
@Id
@UuidGenerator
@Column(name = "id", updatable = false, nullable = false) private
UUID id;
@CreationTimestamp
@Column(name = "created_at", nullable = false, updatable = false) private
LocalDateTime createdAt;
@UpdateTimestamp
private LocalDateTime updatedAt;
Model Config
package pl.mbaracz.todoapp.model;
@Entity @Getter
@NoArgsConstructor
@AllArgsConstructor @Table(name
= "todo_tasks") public class
TodoTask {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) private
Long id;
boolean completed;
@Setter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "todo_list_id") private
TodoList todoList;
}
Services
package pl.mbaracz.todoapp.service;
import java.util.List;
import java.util.UUID;
@Service
@RequiredArgsConstructor public
class TodoService {
return TodoListDto.toDto(todoList);
}
public UUID createTodoList(TodoListDto todoListDto) {
if (todoListDto.tasks() == null || todoListDto.tasks().isEmpty()) { return
null;
}
TodoList todoList = TodoListDto.fromDto(todoListDto); todoList
= todoRepository.save(todoList);
return todoList.getId();
}
(todoList == null) {
return false;
}
todoList.addTasks(taskList);
todoRepository.save(todoList);
return true;
}
if (exists) { todoRepository.deleteById(id);
return true;
}
return false;
}
}
TASK
package pl.mbaracz.todoapp.task;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import pl.mbaracz.todoapp.model.TodoList;
import pl.mbaracz.todoapp.repository.TodoRepository;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor public
class TodoCleanupTask {
void cleanupOldTodoLists() {
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);
todoRepository.deleteAll(oldTodoLists);
}
}
package pl.mbaracz.todoapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringTodoApplication {
package pl.mbaracz.todoapp.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import
org.springframework.test.context.junit4.SpringRunner; import
pl.mbaracz.todoapp.dto.TodoListDto;
import pl.mbaracz.todoapp.dto.TodoTaskDto;
import java.util.List;
import java.util.UUID;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TodoServiceTest {
@Autowired
private TodoService todoService;
existingTodoListId = todoService.createTodoList(todoListDto);
}
@Test
public void findTodoListByNotExistingId() {
TodoListDto todoListDto = todoService.findTodoListById(UUID.randomUUID());
assertNull(todoListDto);
}
@Test
public void findTodoListById() {
TodoListDto retrieved = todoService.findTodoListById(existingTodoListId);
assertNotNull(retrieved.id());
assertNotNull(retrieved.createdAt());
assertNotNull(retrieved.updatedAt());
}
@Test
public void createTodoList()
{ List<TodoTaskDto> tasks = List.of(
new TodoTaskDto("First task", false)
);
TodoListDto todoListDto = new TodoListDto(null, tasks, null, null); UUID id =
todoService.createTodoList(todoListDto);
assertNotNull(id);
TodoTaskDto task = todoListDto.tasks().get(0); TodoTaskDto
expectedTask = tasks.get(0);
assertEquals(expectedTask.completed(), task.completed());
assertEquals(expectedTask.description(), task.description());
}
@Test
public void updateNotExistingTodoList()
{ List<TodoTaskDto> tasks = List.of(
new TodoTaskDto("First task", true)
);
TodoListDto todoListDto = new TodoListDto(UUID.randomUUID(), tasks, null, null); boolean updated
= todoService.updateTodoList(todoListDto);
assertFalse(updated);
}
@Test
public void updateTodoList()
{ List<TodoTaskDto> tasks = List.of(
new TodoTaskDto("Updated task", true)
);
TodoListDto todoListDto = new TodoListDto(existingTodoListId, tasks, null, null); boolean
updated = todoService.updateTodoList(todoListDto);
assertTrue(updated);
}
}