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

Order start point #12

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/start_point/dtos/create-start-point.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export class CreateStartPointDto {
@IsOptional()
@IsMongoId()
user?: string;

order?: Number;
}
17 changes: 17 additions & 0 deletions src/start_point/dtos/update-point.dto.ts
Original file line number Diff line number Diff line change
@@ -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[]
}
11 changes: 11 additions & 0 deletions src/start_point/point.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions src/start_point/point.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
);
Expand All @@ -15,4 +16,5 @@ export interface Point extends mongoose.Document {
description: string;
user: mongoose.Schema.Types.ObjectId;
journeys?: mongoose.Types.ObjectId[];
order?: Number;
}
19 changes: 19 additions & 0 deletions src/start_point/point.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
Loading