Skip to content

Commit

Permalink
Merge pull request #32 from deviate-team/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TeetouchQQ authored Aug 9, 2023
2 parents 47b8e40 + d9710cf commit 68f4e50
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/tickets/tickets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,15 @@ export class TicketsController {
data: ticketRemoved,
};
}

@Get(':id/distance')
async getDistance(@Param('id') id: string) {
const distance = await this.ticketsService.getDistance(id);

return {
success: true,
message: 'Distance calculated successfully',
distance: distance,
};
}
}
37 changes: 37 additions & 0 deletions src/tickets/tickets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,41 @@ export class TicketsService {
}
await this.ticketModel.findByIdAndDelete(id);
}

async getDistance(id: string) {
const ticketExists = await this.ticketModel.findById(id).exec();
if(!ticketExists){
throw new HttpException(
{
success: false,
message: 'Ticket not found',
},
404,
);
}
const origin_latitude = ticketExists.depart_location.latitude;
const origin_longitude = ticketExists.depart_location.longitude;

const destination_latitude = ticketExists.arrive_location.latitude;
const destination_longitude = ticketExists.arrive_location.longitude;
if ((origin_latitude == destination_latitude) && (origin_longitude == destination_longitude)) {
return 0;
}
else {
var radlat1 = Math.PI * origin_latitude/180;
var radlat2 = Math.PI * destination_latitude/180;
var theta = origin_longitude-destination_longitude;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
const unit = "K";
if (unit=="K") { dist = dist * 1.609344 }
return dist;
}
}
}

0 comments on commit 68f4e50

Please sign in to comment.