Skip to content

Commit

Permalink
Merge pull request #13 from fga-eps-mds/fix#144/ordenacao-pontos-de-p…
Browse files Browse the repository at this point in the history
…artida
  • Loading branch information
Neoprot authored Sep 5, 2024
2 parents efb4188 + 74bb739 commit 3c84a28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
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;
}
}

0 comments on commit 3c84a28

Please sign in to comment.