Skip to content

Commit c6e6edf

Browse files
committed
feat: completed list sort
1 parent 2943fe2 commit c6e6edf

7 files changed

Lines changed: 42 additions & 15 deletions

File tree

apps/backend/src/projects/schemas/project.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Document } from 'mongoose'
33

44
export type ProjectDocument = Project & Document
55

6-
@Schema({ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } })
6+
@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
77
export class Project {
88
@Prop({ required: true })
99
name: string

apps/backend/src/tasks/schemas/task.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Project } from '../../projects/schemas/project.schema'
55

66
export type TaskDocument = Task & Document
77

8-
@Schema({ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } })
8+
@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
99
export class Task {
1010
@Prop({ required: true })
1111
title: string

apps/backend/src/tasks/tasks.controller.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common'
1+
import {
2+
Body,
3+
Controller,
4+
Delete,
5+
Get,
6+
Param,
7+
Patch,
8+
Post,
9+
Query,
10+
} from '@nestjs/common'
211
import { TasksService } from './tasks.service'
312
import { Task } from './schemas/task.schema'
413
import { CreateTaskDto } from './dto/create-task.dto'
@@ -14,8 +23,12 @@ export class TasksController {
1423
}
1524

1625
@Get()
17-
findAll(@Query('projectId') projectId?: string, @Query('status') status?: string) {
18-
return this.tasksService.findAll(projectId, status)
26+
findAll(
27+
@Query('projectId') projectId?: string,
28+
@Query('status') status?: string,
29+
@Query('sortBy') sortBy?: string,
30+
) {
31+
return this.tasksService.findAll(projectId, status, sortBy)
1932
}
2033

2134
@Get(':id')
@@ -30,8 +43,8 @@ export class TasksController {
3043

3144
@Patch(':id')
3245
async update(
33-
@Param('id') id: string,
34-
@Body() updateTaskDto: UpdateTaskDto,
46+
@Param('id') id: string,
47+
@Body() updateTaskDto: UpdateTaskDto,
3548
): Promise<Task> {
3649
return this.tasksService.update(id, updateTaskDto)
3750
}

apps/backend/src/tasks/tasks.service.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export class TasksService {
2121
return createdTask.save()
2222
}
2323

24-
async findAll(projectId?: string, status?: string): Promise<Task[]> {
24+
async findAll(
25+
projectId?: string,
26+
status?: string,
27+
sortBy = 'position',
28+
): Promise<Task[]> {
2529
const query: {
2630
projectId?: Types.ObjectId
2731
status?: string
@@ -33,7 +37,10 @@ export class TasksService {
3337
if (status)
3438
query.status = status
3539

36-
return this.taskModel.find(query).sort({ position: -1 }).exec()
40+
return this.taskModel
41+
.find(query)
42+
.sort({ [sortBy]: -1 })
43+
.exec()
3744
}
3845

3946
async findOne(id: string): Promise<Task> {
@@ -51,7 +58,11 @@ export class TasksService {
5158
update.projectId = new Types.ObjectId(updateTaskDto.projectId)
5259

5360
const updatedTask = await this.taskModel
54-
.findByIdAndUpdate(id, { $set: { ...updateTaskDto, ...update } }, { new: true })
61+
.findByIdAndUpdate(
62+
id,
63+
{ $set: { ...updateTaskDto, ...update } },
64+
{ new: true },
65+
)
5566
.exec()
5667

5768
if (!updatedTask)

apps/frontend/src/api/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { TaskResponse } from './types'
33
import type { Task } from '@/store'
44
import { TaskStatus } from '@/store'
55

6-
export function fetchAllTasks({ pId, status }: { pId?: string; status?: TaskStatus }) {
6+
export function fetchAllTasks({ pId, status, sortBy }: { pId?: string; status?: TaskStatus; sortBy?: string }) {
77
return http.get<TaskResponse[], TaskResponse[]>('/tasks', {
88
params: {
99
projectId: pId,
1010
status,
11+
sortBy,
1112
},
1213
})
1314
}

apps/frontend/src/api/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export interface TaskResponse {
77
projectId: string
88
position: number
99
_id: string
10-
created_at: string
11-
updated_at: string
10+
createdAt: string
11+
updatedAt: string
1212
}
1313

1414
export interface ProjectResponse {
15-
created_at: string
15+
createdAt: string
1616
name: string
17-
updated_at: string
17+
updatedAt: string
1818
_id: string
1919
}

apps/frontend/src/store/smartProjects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ export const useSmartProjects = defineStore('smartProjects', () => {
4242

4343
export async function loadSmartProjectTasks(smartProjectName: string) {
4444
const status = smartProjectName === '已完成' ? TaskStatus.COMPLETED : TaskStatus.REMOVED
45+
// 基于 updatedAt 来做排序
4546
const rawTasks = await fetchAllTasks({
4647
status,
48+
sortBy: 'updatedAt',
4749
})
4850
return rawTasks
4951
}

0 commit comments

Comments
 (0)