Skip to content

Commit

Permalink
feat: completed list sort
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 14, 2023
1 parent 2943fe2 commit c6e6edf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/backend/src/projects/schemas/project.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Document } from 'mongoose'

export type ProjectDocument = Project & Document

@Schema({ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } })
@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
export class Project {
@Prop({ required: true })
name: string
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/tasks/schemas/task.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Project } from '../../projects/schemas/project.schema'

export type TaskDocument = Task & Document

@Schema({ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } })
@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
export class Task {
@Prop({ required: true })
title: string
Expand Down
23 changes: 18 additions & 5 deletions apps/backend/src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common'
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common'
import { TasksService } from './tasks.service'
import { Task } from './schemas/task.schema'
import { CreateTaskDto } from './dto/create-task.dto'
Expand All @@ -14,8 +23,12 @@ export class TasksController {
}

@Get()
findAll(@Query('projectId') projectId?: string, @Query('status') status?: string) {
return this.tasksService.findAll(projectId, status)
findAll(
@Query('projectId') projectId?: string,
@Query('status') status?: string,
@Query('sortBy') sortBy?: string,
) {
return this.tasksService.findAll(projectId, status, sortBy)
}

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

@Patch(':id')
async update(
@Param('id') id: string,
@Body() updateTaskDto: UpdateTaskDto,
@Param('id') id: string,
@Body() updateTaskDto: UpdateTaskDto,
): Promise<Task> {
return this.tasksService.update(id, updateTaskDto)
}
Expand Down
17 changes: 14 additions & 3 deletions apps/backend/src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export class TasksService {
return createdTask.save()
}

async findAll(projectId?: string, status?: string): Promise<Task[]> {
async findAll(
projectId?: string,
status?: string,
sortBy = 'position',
): Promise<Task[]> {
const query: {
projectId?: Types.ObjectId
status?: string
Expand All @@ -33,7 +37,10 @@ export class TasksService {
if (status)
query.status = status

return this.taskModel.find(query).sort({ position: -1 }).exec()
return this.taskModel
.find(query)
.sort({ [sortBy]: -1 })
.exec()
}

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

const updatedTask = await this.taskModel
.findByIdAndUpdate(id, { $set: { ...updateTaskDto, ...update } }, { new: true })
.findByIdAndUpdate(
id,
{ $set: { ...updateTaskDto, ...update } },
{ new: true },
)
.exec()

if (!updatedTask)
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/api/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import type { TaskResponse } from './types'
import type { Task } from '@/store'
import { TaskStatus } from '@/store'

export function fetchAllTasks({ pId, status }: { pId?: string; status?: TaskStatus }) {
export function fetchAllTasks({ pId, status, sortBy }: { pId?: string; status?: TaskStatus; sortBy?: string }) {
return http.get<TaskResponse[], TaskResponse[]>('/tasks', {
params: {
projectId: pId,
status,
sortBy,
},
})
}
Expand Down
8 changes: 4 additions & 4 deletions apps/frontend/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export interface TaskResponse {
projectId: string
position: number
_id: string
created_at: string
updated_at: string
createdAt: string
updatedAt: string
}

export interface ProjectResponse {
created_at: string
createdAt: string
name: string
updated_at: string
updatedAt: string
_id: string
}
2 changes: 2 additions & 0 deletions apps/frontend/src/store/smartProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export const useSmartProjects = defineStore('smartProjects', () => {

export async function loadSmartProjectTasks(smartProjectName: string) {
const status = smartProjectName === '已完成' ? TaskStatus.COMPLETED : TaskStatus.REMOVED
// 基于 updatedAt 来做排序
const rawTasks = await fetchAllTasks({
status,
sortBy: 'updatedAt',
})
return rawTasks
}

1 comment on commit c6e6edf

@vercel
Copy link

@vercel vercel bot commented on c6e6edf Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.