index.ts
4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { HttpResponse, http } from 'msw'
import { database } from '@db/apps/kanban/db'
import type { AddNewKanbanItem, EditKanbanItem, KanbanBoard, KanbanState, RenameKanbanBoard } from '@db/apps/kanban/types'
export const handlerAppsKanban = [
// 👉 get all kanban data
http.get('/api/apps/kanban', () => {
return HttpResponse.json(database, { status: 200 })
}),
// 👉 rename board
http.put('/api/apps/kanban/board/rename', async ({ request }) => {
const boardData = await request.json() as RenameKanbanBoard
database.boards = database.boards.map(board => {
if (board.id === boardData.boardId)
board.title = boardData.newName
return board
})
return new HttpResponse(null, { status: 201 })
}),
// 👉 delete board
http.delete('/api/apps/kanban/board/:id', async ({ params }) => {
const boardId = Number(params.id)
database.boards = database.boards.filter(board => board.id !== boardId)
return new HttpResponse(null, { status: 204 })
}),
// 👉 add new board
http.post('/api/apps/kanban/board/add', async ({ request }) => {
const boardName = await request.json() as { title: string }
const getNewBoardId = () => {
const newBoardId = database.boards.length + 1
if (!(database.boards.some(board => board.id === newBoardId)))
return newBoardId
else
return newBoardId + 1
}
if (database.boards.some(board => board.title === boardName.title)) {
return HttpResponse.error()
}
else {
database.boards.push({
id: getNewBoardId(),
title: boardName.title,
itemsIds: [],
})
return new HttpResponse(null, { status: 201 })
}
}),
// 👉 add new item
http.post('/api/apps/kanban/item/add', async ({ request }) => {
const newItem = await request.json() as AddNewKanbanItem
const itemId = database.items[database.items.length - 1].id + 1
if (newItem.itemTitle && newItem.boardName) {
// Add the new item to the items list
database.items.push({
id: itemId,
title: newItem.itemTitle,
attachments: 0,
comments: '',
commentsCount: 0,
dueDate: '',
labels: [],
members: [],
})
// find the index of board in the database
const boardId = database.boards.findIndex(board => board.id === newItem.boardId)
// Add the new item to the board
database.boards[boardId].itemsIds.push(itemId)
}
else {
return HttpResponse.error()
}
return new HttpResponse(null, { status: 201 })
}),
// 👉 update item
http.put('/api/apps/kanban/item/update', async ({ request }) => {
const itemData = await request.json() as EditKanbanItem
database.items.forEach(item => {
if (itemData.item && item.id === itemData.item.id) {
item.title = itemData.item.title
item.attachments = itemData.item.attachments
item.comments = itemData.item.comments
item.commentsCount = itemData.item.commentsCount
item.dueDate = itemData.item.dueDate
item.labels = itemData.item.labels
item.members = itemData.item.members
}
})
return new HttpResponse(null, { status: 201 })
}),
// 👉 delete item
http.delete('/api/apps/kanban/item/:id', async ({ params }) => {
const itemId = Number(params.id)
database.items = database.items.filter(item => item.id !== itemId)
database.boards.forEach(board => {
board.itemsIds = board.itemsIds.filter(id => id !== itemId)
})
return new HttpResponse(null, { status: 204 })
}),
// 👉 update item state
http.put('/api/apps/kanban/item/state-update', async ({ request }) => {
const stateData = await request.json() as KanbanState
database.boards.forEach(board => {
if (board.id === stateData.boardId)
board.itemsIds = stateData.ids
})
return new HttpResponse(null, { status: 201 })
}),
// 👉 update board state
http.put('/api/apps/kanban/board/state-update', async ({ request }) => {
const boardState = await request.json() as number[]
// sort board as per boardState
const sortedBoards: KanbanBoard[] = boardState.map(boardId => {
return database.boards.find(board => board.id === boardId) as KanbanBoard
})
database.boards = sortedBoards
return new HttpResponse(null, { status: 201 })
}),
]