Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented persistent reorder todo list functionality #15

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
env: {
node: true,
"es6": true,
},
extends: 'eslint:recommended',
parserOptions: {
Expand Down
19 changes: 18 additions & 1 deletion backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
createTodoItem,
fetchTodoItemById,
updateTodoItem,
deleteTodoItem
deleteTodoItem,
reorderTodoItems
} from './todo-service.js'

const app = express()
Expand All @@ -29,6 +30,12 @@ app.get('/api/todo-list/:listId', async (req, res) => {
try {
const todoList = await fetchTodoListById(req.params.listId)
if (!todoList) return res.status(404).json({ message: 'Todo list not found' })

// Sort items by order before returning
if (todoList.items) {
todoList.items.sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity))
}

res.json(todoList)
} catch (error) {
res.status(500).json({ message: error.message })
Expand Down Expand Up @@ -63,6 +70,16 @@ app.patch('/api/todo-list/:listId/item/:itemId', async (req, res) => {
}
})

app.patch('/api/todo-list/:listId/reorder', async (req, res) => {
try {
const { itemIds } = req.body
const reorderedItems = await reorderTodoItems(req.params.listId, itemIds)
res.json(reorderedItems)
} catch (error) {
res.status(500).json({ message: error.message })
}
})

app.delete('/api/todo-list/:listId/item/:itemId', async (req, res) => {
try {
const deletedTodoItem = await deleteTodoItem(req.params.listId, req.params.itemId)
Expand Down
28 changes: 26 additions & 2 deletions backend/src/data-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@ let dataSource = null
const seedData = async (dataSource) => {
const todoListRepository = dataSource.getRepository(TodoList)
const todoItemRepository = dataSource.getRepository(TodoItem)

const firstList = await todoListRepository.save({ title: 'First List' })
await todoItemRepository.save({ itemTitle: 'First todo of first list!', list: firstList })
await todoItemRepository.save([
{
itemTitle: 'First todo of first list!',
list: firstList,
order: 0
},
{
itemTitle: 'Second todo of first list!',
list: firstList,
order: 1
}
])

const secondList = await todoListRepository.save({ title: 'Second List' })
await todoItemRepository.save({ itemTitle: 'First todo of second list!', list: secondList })
await todoItemRepository.save([
{
itemTitle: 'First todo of second list!',
list: secondList,
order: 0
},
{
itemTitle: 'Second todo of second list!',
list: secondList,
order: 1
}
])
}

export const getAppDataSource = async () => {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/todo-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const TodoItem = new EntitySchema({
type: Boolean,
default: false,
},
order: {
type: Number,
nullable: false,
},
},
relations: {
list: {
Expand Down
52 changes: 48 additions & 4 deletions backend/src/todo-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ export const fetchTodoListById = async (listId) => {

export const createTodoItem = async (listId, todoItemData) => {
const AppDataSource = await getAppDataSource()
const todoList = await AppDataSource.getRepository(TodoList).findOneBy({ id: listId })
const todoListRepository = AppDataSource.getRepository(TodoList)
const todoItemRepository = AppDataSource.getRepository(TodoItem)

const todoList = await todoListRepository.findOneBy({ id: listId })
if (!todoList) throw new Error(`Todo list ${listId} not found`)

const todoItem = AppDataSource.getRepository(TodoItem).create({ ...todoItemData, list: todoList })
return AppDataSource.getRepository(TodoItem).save(todoItem)

const maxOrderItem = await todoItemRepository.findOne({
where: { list: { id: listId } },
order: { order: 'DESC' }
})

const newOrder = maxOrderItem ? maxOrderItem.order + 1 : 0

const todoItem = todoItemRepository.create({
...todoItemData,
list: todoList,
order: newOrder
})

return todoItemRepository.save(todoItem)
}

export const fetchTodoItemById = async (listId, itemId) => {
Expand Down Expand Up @@ -56,3 +71,32 @@ export const deleteTodoItem = async (listId, itemId) => {
await todoRepository.delete(itemId)
return todoItem
}

export const reorderTodoItems = async (listId, itemIds) => {
const AppDataSource = await getAppDataSource()
const todoItemRepository = AppDataSource.getRepository(TodoItem)

try {
const todoItems = await todoItemRepository.find({
where: { list: { id: listId } }
})

// Use Promise.all for concurrent updates
await Promise.all(itemIds.map((itemId, index) => {
const todoItem = todoItems.find(item => item.id === parseInt(itemId))

if (todoItem) {
todoItem.order = index
return todoItemRepository.save(todoItem)
}
}))

return todoItemRepository.find({
where: { list: { id: listId } },
order: { order: 'ASC' }
})
} catch (error) {
console.error('Error reordering items:', error)
throw error
}
}
Loading