Skip to content
Snippets Groups Projects
Commit 81016c29 authored by abdu's avatar abdu
Browse files

add method to mark todos as completed (FE + BE)

parent d3c58fd6
No related branches found
No related tags found
No related merge requests found
......@@ -91,6 +91,21 @@ export default {
await this.getAllTodos()
}
},
async setTodoComplete(todoId){
let url = `http://localhost:9876/user/setTodoComplete?userId=${localStorage.getItem('userId')}&todoId=${todoId}`
const response = await fetch(url, {
method: 'PATCH',
headers: {
"Content-Type": "application/json"
},
credentials: 'include'
})
if(response.status === 200){
await this.getAllTodos()
}
},
async signout(){
let url = "http://localhost:9876/user/signout"
......@@ -115,14 +130,14 @@ export default {
}
</script>
<template class="h100">
<template>
<div class="h-100">
<b-container class="h-100" fluid id="main-container">
<b-button variant="danger" class="mb-4" @click="signout">Log out</b-button>
<b-row class="h-100 justify-content-center align-items-center">
<b-col class="h-100">
<b-card class="shadow-lg w-75 h-75 mx-auto" border-variant="secondary" title="ToDo-App">
<b-card-body class="h-75 d-flex flex-column">
<b-card class="shadow-lg w-75 h-100 mx-auto" border-variant="secondary" title="ToDo-App">
<b-card-body class="h-100 d-flex flex-column">
<b-button class="mb-4" variant="primary" @click="showAddModal = true">
Hinzufügen
</b-button>
......@@ -134,16 +149,16 @@ export default {
<b-form-input v-model="todo.description" placeholder="Beschreibung eingeben"></b-form-input>
</b-form-group>
</b-modal>
<b-tabs content-class="mt-2">
<b-tabs class="mt-2 tab-content-scrollable">
<b-tab title="Active ToDos">
<ul class="list-group mt-3 h-75" v-if="items.length > 0">
<ul class="list-group mt-3 h-100" v-if="items.length > 0">
<li v-for="(item, index) in items" :key="index" class="list-group-item">
<div class="d-flex justify-content-between align-items-center w-100">
<div class="flex-grow-1">
<CustomListElement :title="item.title" :description="item.description" :completed="item.completed"></CustomListElement>
</div>
<div class="badge-container">
<div class="custom-badge-not-completed">running</div>
<b-button @click="setTodoComplete(item.todoId)" class="custom-badge-not-completed">mark as completed</b-button>
</div>
<b-button variant="danger" @click.stop="deleteTodo(item.todoId)">Delete</b-button>
</div>
......@@ -152,7 +167,7 @@ export default {
<p v-else class="text-muted text-center">No Todos. Click the button above to add new todos.</p>
</b-tab>
<b-tab title="Completed ToDos">
<ul class="list-group mt-3 h-75" v-if="completedItems.length > 0">
<ul class="list-group mt-3 h-100" v-if="completedItems.length > 0">
<li v-for="(item, index) in completedItems" :key="index" class="list-group-item">
<div class="d-flex justify-content-between align-items-center w-100">
<div class="flex-grow-1">
......@@ -179,15 +194,19 @@ export default {
</div>
</template>
<style>
<style scoped>
html, body {
height: 100%;
margin: 0;
}
.list-group {
height: 50%;
height: 70%; /* Adjust this value as needed */
overflow-y: auto;
margin-bottom: 100px;
}
.tab-content-scrollable {
height: 70%; /* Adjust this value as needed */
overflow-y: auto;
}
......@@ -198,6 +217,7 @@ html, body {
border: 1px gray solid;
border-radius: 25px;
width: 100%;
margin-bottom: 2px;
}
.list-group-item:hover {
......@@ -217,7 +237,7 @@ html, body {
.custom-badge-completed,
.custom-badge-not-completed {
padding: 5px;
padding: 6px 10px;
margin-left: 10px; /* Add some space between the badge and the element before it */
border-radius: 15px;
}
......
......@@ -81,4 +81,14 @@ public class UserController {
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
}
@PatchMapping("setTodoComplete")
public ResponseEntity<List<TodoDTO>> setTodoComplete(HttpServletRequest request, @RequestParam String userId, @RequestParam String todoId) {
if(userService.validateSession(request)){
List<TodoDTO> todos = userService.setTodoComplete(userId, todoId);
return new ResponseEntity<>(todos, HttpStatus.OK);
}
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
}
}
......@@ -84,37 +84,42 @@ public class UserService {
public List<TodoDTO> addTodo(TodoDTO todoDTO){
Todo todo = todoMapper.dtoToEntity(todoDTO);
todoRepository.save(todo);
List<Todo> todoList = todoRepository.findAllByUserId(todo.getUserId());
List<TodoDTO> todoDTOList = new ArrayList<>();
todoList.forEach((todoItem) -> {
todoDTOList.add(todoMapper.entityToDto(todoItem));
});
return todoDTOList;
return getAllTodosForUser(todoDTO.getUserId());
}
public List<TodoDTO> deleteTodo(String userId, String todoId) {
System.out.println("service here");
if(todoRepository.existsById(todoId)){
todoRepository.deleteById(todoId);
}
List<Todo> todoList = todoRepository.findAllByUserId(userId);
List<TodoDTO> todoDTOList = new ArrayList<>();
return getAllTodosForUser(userId);
}
todoList.forEach((todoItem) -> {
todoDTOList.add(todoMapper.entityToDto(todoItem));
});
public List<TodoDTO> setTodoComplete(String userId, String todoId) {
Todo todo = todoRepository.findById(todoId).get();
todo.setCompleted(true);
todoRepository.save(todo);
return todoDTOList;
return getAllTodosForUser(userId);
}
//helper methods
public boolean validateSession(HttpServletRequest request) {
HttpSession session = request.getSession(false);
System.out.println("Session ID: " + (session != null ? session.getId() : "null"));
return session != null && session.getAttribute("userId") != null;
}
public List<TodoDTO> getAllTodosForUser(String userId) {
List<Todo> todoList = todoRepository.findAllByUserId(userId);
List<TodoDTO> todoDTOList = new ArrayList<>();
todoList.forEach((todoItem) -> {
todoDTOList.add(todoMapper.entityToDto(todoItem));
});
return todoDTOList;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment