diff --git a/src/start_point/dtos/create-start-point.dto.ts b/src/start_point/dtos/create-start-point.dto.ts index e492baa..04b5b77 100644 --- a/src/start_point/dtos/create-start-point.dto.ts +++ b/src/start_point/dtos/create-start-point.dto.ts @@ -10,4 +10,6 @@ export class CreateStartPointDto { @IsOptional() @IsMongoId() user?: string; + + order?: Number; } diff --git a/src/start_point/dtos/update-point.dto.ts b/src/start_point/dtos/update-point.dto.ts new file mode 100644 index 0000000..a4a5aa4 --- /dev/null +++ b/src/start_point/dtos/update-point.dto.ts @@ -0,0 +1,17 @@ +export interface UpdatePointInterface { + _id: string; + name: string; + description?: string; + user?: string; + journeys?: string[]; + order: Number; + createdAt: string; + __v: number; + updatedAt: string; + journey?: string; + + } + +export class UpdatePointOrderDto { + points: UpdatePointInterface[] +} \ No newline at end of file diff --git a/src/start_point/point.controller.ts b/src/start_point/point.controller.ts index 382b3bd..9e8a98b 100644 --- a/src/start_point/point.controller.ts +++ b/src/start_point/point.controller.ts @@ -14,6 +14,7 @@ import { import { PointService } from './point.service'; import { Request } from 'express'; import { CreateStartPointDto } from './dtos/create-start-point.dto'; +import { UpdatePointOrderDto} from './dtos/update-point.dto'; @Controller('points') export class PointController { @@ -81,4 +82,14 @@ export class PointController { throw error; } } + + @Patch('/update-point-order') + async updatePointOrder( + @Body() + pointsDto: UpdatePointOrderDto + ) { + console.log(pointsDto) + const result = await this.pointService.updateOrder(pointsDto.points); + return result + } } diff --git a/src/start_point/point.schema.ts b/src/start_point/point.schema.ts index 5ab24a3..2f57a37 100644 --- a/src/start_point/point.schema.ts +++ b/src/start_point/point.schema.ts @@ -6,6 +6,7 @@ export const PointSchema = new mongoose.Schema( description: { type: String, required: true }, user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, journeys: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Journey' }], + order: { type:Number, default:0}, }, { timestamps: true, collection: 'startpoints' }, ); @@ -15,4 +16,5 @@ export interface Point extends mongoose.Document { description: string; user: mongoose.Schema.Types.ObjectId; journeys?: mongoose.Types.ObjectId[]; + order?: Number; } diff --git a/src/start_point/point.service.ts b/src/start_point/point.service.ts index 5a3446c..e483eaa 100644 --- a/src/start_point/point.service.ts +++ b/src/start_point/point.service.ts @@ -10,6 +10,7 @@ import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Point } from './point.schema'; import { CreateStartPointDto } from './dtos/create-start-point.dto'; +import { UpdatePointInterface} from './dtos/update-point.dto'; @Injectable() export class PointService { @@ -30,9 +31,13 @@ export class PointService { throw new UnauthorizedException('Invalid token'); } + const existent_array = this.findAll(); + + const newPoint = new this.pointModel({ ...createStartPointDto, user: userId, + order: (await existent_array).length, }); const savedPoint = await newPoint.save(); @@ -137,4 +142,18 @@ export class PointService { } return point.journeys || []; } + + async updateOrder(journeys: UpdatePointInterface[]) { + console.log(journeys); + const bulkOperations = journeys.map((trail) => ({ + updateOne: { + filter: { _id: new Types.ObjectId(trail._id) }, + update: { $set: { order: trail.order } }, + }, + })); + + const result = await this.pointModel.bulkWrite(bulkOperations); + console.log(`Bulk update result: ${JSON.stringify(result)}`); + return result; + } }