0% found this document useful (0 votes)
22 views

EXPERIMENT10 Ejava

Uploaded by

o8799621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

EXPERIMENT10 Ejava

Uploaded by

o8799621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EXPERIMENT -10

AIM OF THE EXPERIMENT: Spring Boot to do application.

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();

return new TodoListDto(


todoList.getId(), tasks,
todoList.getCreatedAt(),
todoList.getUpdatedAt()
);
}

public static TodoList fromDto(TodoListDto todoListDto)


{ TodoList todoList = new TodoList(
null,
null,
null,
new ArrayList<>()
);

List<TodoTask> tasks = todoListDto.tasks()


.stream()
.map(TodoTaskDto::fromDto)
.peek(todoTask -> todoTask.setTodoList(todoList))
.toList();

todoList.addTasks(tasks);

return todoList;

}
}

TodoTaskDto.java
package pl.mbaracz.todoapp.dto;
import pl.mbaracz.todoapp.model.TodoTask;

public record TodoTaskDto(


String description,
boolean completed
){
public static TodoTaskDto toDto(TodoTask todoTask) { return
new TodoTaskDto(
todoTask.getDescription(),
todoTask.isCompleted()
);
}

public static TodoTask fromDto(TodoTaskDto todoTaskDto) { return new


TodoTask(
null,
todoTaskDto.description(),
todoTaskDto.completed(), null
);
}
}

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 {

private final TodoService todoService;

@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 jakarta.persistence.*; import


lombok.AllArgsConstructor; import
lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp; import
org.hibernate.annotations.UuidGenerator;

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;

@OneToMany(mappedBy = "todoList", cascade = CascadeType.ALL, orphanRemoval = true, fetch =


FetchType.EAGER)
private List<TodoTask> tasks = new ArrayList<>();

public void clearTasks() {


this.tasks.clear();
}

public void addTasks(List<TodoTask> tasks) {


this.tasks.addAll(tasks);
}
}

Model Config
package pl.mbaracz.todoapp.model;

import jakarta.persistence.*; import


lombok.AllArgsConstructor; import
lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity @Getter
@NoArgsConstructor
@AllArgsConstructor @Table(name
= "todo_tasks") public class
TodoTask {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) private
Long id;

private String description; private

boolean completed;

@Setter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "todo_list_id") private
TodoList todoList;

}
Services
package pl.mbaracz.todoapp.service;

import lombok.RequiredArgsConstructor; import


org.springframework.stereotype.Service; import
pl.mbaracz.todoapp.dto.TodoListDto; import
pl.mbaracz.todoapp.dto.TodoTaskDto; import
pl.mbaracz.todoapp.model.TodoList; import
pl.mbaracz.todoapp.model.TodoTask;
import pl.mbaracz.todoapp.repository.TodoRepository;

import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor public
class TodoService {

private final TodoRepository todoRepository;

public TodoListDto findTodoListById(UUID uniqueId) {


TodoList todoList = todoRepository.findById(uniqueId).orElse(null);

if (todoList == null) { return


null;
}

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();
}

public boolean updateTodoList(TodoListDto todoListDto) { if


(todoListDto.id() == null || todoListDto.tasks() == null) {
return false;
}

TodoList todoList = todoRepository.findById(todoListDto.id()).orElse(null); if

(todoList == null) {
return false;
}

List<TodoTask> taskList = todoListDto.tasks()


.stream()
.map(TodoTaskDto::fromDto)
.peek(todoTask -> todoTask.setTodoList(todoList))
.toList(); todoList.clearTasks();

todoList.addTasks(taskList);

todoRepository.save(todoList);

return true;
}

public boolean deleteTodoList(UUID id) { boolean


exists = todoRepository.existsById(id);

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 {

private final TodoRepository todoRepository; @Transactional


@Scheduled(cron = "0 0 0 * * *") public

void cleanupOldTodoLists() {
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);

List<TodoList> oldTodoLists = todoRepository.findAllByUpdatedAtBefore(thirtyDaysAgo);

todoRepository.deleteAll(oldTodoLists);
}
}

package pl.mbaracz.todoapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringTodoApplication {

public static void main(String[] args)


{ SpringApplication.run(SpringTodoApplication.class, args);
}
}

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;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TodoServiceTest {

@Autowired
private TodoService todoService;

private UUID existingTodoListId;


@Before
public void setUp() { List<TodoTaskDto>
tasks = List.of(
new TodoTaskDto("First task", false)
);
TodoListDto todoListDto = new TodoListDto(null, tasks, null, null);

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);
}
}

NAME- KUSAGRA RAJ


Reg.- 2201030103
GROUP- 5A
BRANCH- CSE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy