parent
0ea0c130af
commit
57433c8966
@ -0,0 +1,169 @@
|
||||
import { Router, Request, Response, NextFunction, RequestHandler } from 'express';
|
||||
import Controller from '../Icontroller';
|
||||
import HttpException from '../../middleware/exeption/httpExeption';
|
||||
import LocationService from '../../service/LocationService';
|
||||
|
||||
|
||||
class TaskController implements Controller {
|
||||
public path = '/task';
|
||||
public router = Router();
|
||||
private userService = new UserService();
|
||||
private locationService = new LocationService();
|
||||
|
||||
constructor() {
|
||||
this.initialiseRoutes();
|
||||
}
|
||||
|
||||
private initialiseRoutes(): void {
|
||||
//create
|
||||
this.router.post(`${this.path}`,this.createUser);
|
||||
|
||||
// // get One
|
||||
this.router.get (`${this.path}/:userId`, this.getUserById);
|
||||
// // get All
|
||||
this.router.get (`${this.path}`, this.getAllUsers);
|
||||
|
||||
//update One
|
||||
this.router.put (`${this.path}/:userId`, this.updateUser);
|
||||
|
||||
//Delete One
|
||||
this.router.delete (`${this.path}/:userId`, this.deleteUser);
|
||||
|
||||
}
|
||||
|
||||
private createUser = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
|
||||
console.log(req.body);
|
||||
const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
||||
checkIfIsValidCreateTaskReqBody(reqBody);
|
||||
await this.userService.createUserById(reqBody.fin
|
||||
);
|
||||
|
||||
res.status(200).send({ status: "Success", msg: "Success add" });
|
||||
|
||||
|
||||
} catch (error) {
|
||||
next(new HttpException(400, 'Cannot create post'));
|
||||
}
|
||||
};
|
||||
private readonly getUserById: RequestHandler = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
const id = req.params.taskId;
|
||||
const userId = req.params.userId;
|
||||
|
||||
const data = await this.userService.getUserById(id, userId);
|
||||
res.status(201).send(data);
|
||||
|
||||
}
|
||||
catch(error){
|
||||
next(new HttpException(400, 'Cannot create post'));
|
||||
}
|
||||
|
||||
}
|
||||
private readonly getAllUsers: RequestHandler = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
const userId = req.params.userId;
|
||||
const tasks = await this.userService.getUsers(userId);
|
||||
const responseList = tasks.map(task => new TaskResumedRes(task));
|
||||
res.status(201).send(responseList);
|
||||
|
||||
}
|
||||
catch(error){
|
||||
next(new HttpException(400, 'Cannot get user task'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private deleteUser = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
const id = req.params.taskId;
|
||||
const userId = req.params.userId;
|
||||
await this.userService.DeleteUser(id, userId);
|
||||
return res.status(200).send({ status: "Success", msg: "Data Removed" });
|
||||
} catch (error) {
|
||||
next(new HttpException(400, 'Cannot create post'));
|
||||
}
|
||||
};
|
||||
|
||||
private updateUser = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
|
||||
const taskId = req.params.taskId;
|
||||
const userId = req.params.userId;
|
||||
const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
||||
|
||||
const updatedTask = await this.userService.UpdateTask(
|
||||
// req.auth!.uid,
|
||||
taskId,
|
||||
userId,
|
||||
// firebase.auth().currentUser.getIdToken()
|
||||
reqBody.nom,
|
||||
reqBody.description,
|
||||
reqBody.logo,
|
||||
reqBody.duration,
|
||||
reqBody.done,
|
||||
// reqBody.tags,
|
||||
reqBody.repepat,
|
||||
reqBody.deb,
|
||||
reqBody.fin
|
||||
);
|
||||
// res.send('Success add');
|
||||
// res.status(201).json({ task });
|
||||
res.status(204).send(`Update a new contact: ${updatedTask}`);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
next(new HttpException(403, 'Cannot create post'));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
private getUserNext: RequestHandler = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> => {
|
||||
try {
|
||||
const longitude = Number(req.body.longitude);
|
||||
const latitude = Number(req.body.latitude);
|
||||
//verify::val_int(){
|
||||
if (isNaN(longitude) || isNaN(latitude)) {
|
||||
console.log('Impossible de convertir la chaîne en nombre');
|
||||
}
|
||||
//}
|
||||
const userId = req.body.userId;
|
||||
const data = await this.locationService.getNearUser(userId,latitude,longitude);
|
||||
console.log(data);
|
||||
res.status(201).send(data);
|
||||
|
||||
}
|
||||
catch(error){
|
||||
next(new HttpException(400, 'Cannot create post'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default TaskController;
|
@ -0,0 +1,3 @@
|
||||
const uri = "mongodb+srv://fladDevDb:ZslYlNRWIOUU7i6o@fladcluster.b29tytu.mongodb.net/?retryWrites=true&w=majority"
|
||||
|
||||
export default db = new MongoClient(uri);
|
@ -0,0 +1,6 @@
|
||||
const notificationSchema = new mongoose.Schema({
|
||||
type: {type: String, required: true},
|
||||
content: {type: String, required: true}
|
||||
});
|
||||
|
||||
export default {Annonce: mongoose.model("nofitication", notificationSchema)}
|
@ -0,0 +1,21 @@
|
||||
|
||||
const userSchema: Schema = new mongoose.Schema<IUser>({
|
||||
pseudo: {type: String, index: { unique: true }},
|
||||
email: {type: String},
|
||||
idDafl: {type: String, index: { unique: true }},
|
||||
idSpotify: {type: String},
|
||||
password: {type: String},
|
||||
prenom: {type: String, default: ""},
|
||||
description: {type: String, default: ""},
|
||||
nom: {type: String, default: ""},
|
||||
ville: {type: String, default: ""},
|
||||
profilPic: {type: String},
|
||||
noteList: [],
|
||||
notifications: [],
|
||||
friends: {type: [String] },
|
||||
favoris: [],
|
||||
conversations: {type: [String] }
|
||||
});
|
||||
// fladDevDb
|
||||
// ZslYlNRWIOUU7i6o
|
||||
export const User: Model<IUser> = model('User', userSchema);
|
@ -0,0 +1,5 @@
|
||||
export default interface IUser{
|
||||
name: string;
|
||||
email: string;
|
||||
avatar?: string;
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
export interface Position {
|
||||
/**
|
||||
* Creation timestamp for coords
|
||||
*/
|
||||
timestamp: number;
|
||||
/**
|
||||
* The GPS coordinates along with the accuracy of the data
|
||||
*/
|
||||
coords: {
|
||||
/**
|
||||
* Latitude in decimal degrees
|
||||
*/
|
||||
latitude: number;
|
||||
/**
|
||||
* longitude in decimal degrees
|
||||
*/
|
||||
longitude: number;
|
||||
};
|
||||
}
|
||||
export class PlacePosition implements Position {
|
||||
timestamp: number;
|
||||
coords: {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
};
|
||||
constructor(timestamp: number,latitude : number ,longitude: number){
|
||||
this.timestamp = timestamp;
|
||||
this.coords = {latitude, longitude};
|
||||
}
|
||||
}
|
||||
export class UserLocation {
|
||||
uuid: string;
|
||||
latitude : number;
|
||||
longitude: number;
|
||||
constructor(uuid: string, latitude: number, longitude: number){
|
||||
this.uuid = uuid;
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
}
|
||||
}
|
||||
|
||||
export class Place{
|
||||
position: Position;
|
||||
address: Address;
|
||||
constructor(address: Address,position: Position){
|
||||
this.position = position;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export type Address = {
|
||||
street : string;
|
||||
city : string;
|
||||
state : string;
|
||||
zip : string;
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
// import db from '../database';
|
||||
import { Place, PlacePosition, Position, UserLocation } from '../model/locationModel';
|
||||
import axios from 'axios';
|
||||
|
||||
class LocationService {
|
||||
private API_KEY : string = "AIzaSyBFCEAtmhZ8jvw84UTQvX3Aqpr66GVqB_A";
|
||||
public async getNearUser(uuid : string, latitude : number, longitude : number)
|
||||
{
|
||||
const UserCollectionref = db.collection('UserPosition');
|
||||
db.collection('UserPosition').doc(uuid).set({uuid : uuid, latitude : latitude, longitude : longitude});
|
||||
// const newPosition = {
|
||||
// name: 'Los Angeles',
|
||||
// state: 'CA',
|
||||
// country: 'USA'
|
||||
// };
|
||||
const snapshot = await UserCollectionref.where("uuid", "!=", uuid).get();
|
||||
if (snapshot.empty) {
|
||||
console.log('No matching documents.');
|
||||
return;
|
||||
}
|
||||
|
||||
let dbUsersList:UserLocation[] = [];
|
||||
snapshot.forEach(doc => {
|
||||
dbUsersList.push(new UserLocation(doc.data().uuid,doc.data().latitude,doc.data().longitude));
|
||||
console.log(doc.id, '=>', doc.data());
|
||||
});
|
||||
|
||||
let listUser: string[] = []; //Set the listUser to an empty list
|
||||
dbUsersList.forEach(user => {
|
||||
console.log(user);
|
||||
const dist = this.distanceBetween(latitude , longitude , user.latitude, user.longitude); //With the function meters, determinate the distance between the current user and the user who is in the actual row
|
||||
console.log(user.uuid,dist);
|
||||
if (dist <= 5) { //If the user in the actual row is less than 100 meters away of the current user
|
||||
|
||||
listUser.push(user.uuid); //Add the username and the ID of the song that user who is in the actual row is listening
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return listUser; //Return an array
|
||||
// $listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening
|
||||
|
||||
}
|
||||
|
||||
public getCenter (points: Position[]) {
|
||||
if (Array.isArray(points) === false || points.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const numberOfPoints = points.length;
|
||||
|
||||
const sum = points.reduce(
|
||||
(acc, point) => {
|
||||
const pointLat = this.toRad(point.coords.latitude);
|
||||
const pointLon = this.toRad(point.coords.longitude);
|
||||
return {
|
||||
X: acc.X + Math.cos(pointLat) * Math.cos(pointLon),
|
||||
Y: acc.Y + Math.cos(pointLat) * Math.sin(pointLon),
|
||||
Z: acc.Z + Math.sin(pointLat),
|
||||
};
|
||||
},
|
||||
{ X: 0, Y: 0, Z: 0 }
|
||||
);
|
||||
|
||||
const X = sum.X / numberOfPoints;
|
||||
const Y = sum.Y / numberOfPoints;
|
||||
const Z = sum.Z / numberOfPoints;
|
||||
|
||||
return {
|
||||
longitude: this.toDeg(Math.atan2(Y, X)),
|
||||
latitude: this.toDeg(Math.atan2(Z, Math.sqrt(X * X + Y * Y))),
|
||||
};
|
||||
};
|
||||
|
||||
public toRad = (value: number) => (value * Math.PI) / 180;
|
||||
public toDeg = (value: number) => (value * 180) / Math.PI;
|
||||
|
||||
// sa c'est un utils du coup mettre dans une calss utils
|
||||
// resulta en km
|
||||
private distanceBetween (lat1 : number, lon1 : number, lat2: number, lon2 : number) : number {
|
||||
if ((lat1 == lat2) && (lon1 == lon2)) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
var radlat1 = Math.PI * lat1/180;
|
||||
var radlat2 = Math.PI * lat2/180;
|
||||
var theta = lon1-lon2;
|
||||
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;
|
||||
dist = dist * 1.609344;
|
||||
|
||||
return dist;
|
||||
}
|
||||
}
|
||||
private distanceBetweenPosition(first : Position, second : Position) : number {
|
||||
return this.distanceBetween (first.coords.latitude, first.coords.longitude, second.coords.latitude, second.coords.longitude)
|
||||
}
|
||||
|
||||
// give a array of position sorted by distance and return the first
|
||||
private findNearest(main : Position, list : Position[]){
|
||||
this.orderByDistance(main, list)[0]
|
||||
}
|
||||
|
||||
//distanceFn: DistanceFn = getDistance est param sa serrait cool de lui passer un fonction
|
||||
private orderByDistance (mainPos: Position,coords: Position[]){
|
||||
return coords
|
||||
.slice()
|
||||
.sort((a, b) => this.distanceBetweenPosition(mainPos, a) - this.distanceBetweenPosition(mainPos, b));
|
||||
};
|
||||
|
||||
// getCenter(coords)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default LocationService;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,111 +1,111 @@
|
||||
// import { useState } from "react";
|
||||
// import SpotifyService from "../services/spotify/spotify.service";
|
||||
import { useState } from "react";
|
||||
import SpotifyService from "../services/spotify/spotify.service";
|
||||
|
||||
// class Manager{
|
||||
class Manager{
|
||||
|
||||
// // injection de dépences
|
||||
// spotifyService = new SpotifyService();
|
||||
// userService = new userService();
|
||||
// injection de dépences
|
||||
spotifyService = new SpotifyService();
|
||||
userService = new userService();
|
||||
|
||||
// private currentUser = useState(null);
|
||||
private currentUser = useState(null);
|
||||
|
||||
// constructor() {
|
||||
// }
|
||||
constructor() {
|
||||
}
|
||||
|
||||
// // spotify methods
|
||||
// apiAuthorization(url : string) {
|
||||
// spotifyService.apiAuthorization(url);
|
||||
// }
|
||||
// spotify methods
|
||||
apiAuthorization(url : string) {
|
||||
spotifyService.apiAuthorization(url);
|
||||
}
|
||||
|
||||
// getCompleteMusic = async (id : string) :Promise<Music> =>{
|
||||
// // Map info = await spotifyService.getTrackInfo(id);
|
||||
// // return Music(id, info['name'], info['artist'], info['cover']);
|
||||
// }
|
||||
getCompleteMusic = async (id : string) :Promise<Music> =>{
|
||||
// Map info = await spotifyService.getTrackInfo(id);
|
||||
// return Music(id, info['name'], info['artist'], info['cover']);
|
||||
}
|
||||
|
||||
// removeFromPlaylist(id : string) {
|
||||
// this.spotifyService.removeFromPlaylist(id);
|
||||
// }
|
||||
removeFromPlaylist(id : string) {
|
||||
this.spotifyService.removeFromPlaylist(id);
|
||||
}
|
||||
|
||||
|
||||
// addToPlaylist(id : string) {
|
||||
// this.spotifyService.addToPlaylist(id);
|
||||
// }
|
||||
addToPlaylist(id : string) {
|
||||
this.spotifyService.addToPlaylist(id);
|
||||
}
|
||||
|
||||
// playTrack(id : string) {
|
||||
// this.spotifyService.playTrack(id);
|
||||
// }
|
||||
// ////////////////////////////////////////////
|
||||
playTrack(id : string) {
|
||||
this.spotifyService.playTrack(id);
|
||||
}
|
||||
////////////////////////////////////////////
|
||||
|
||||
// // private readonly getTaskById: RequestHandler = async (
|
||||
// // req: Request,
|
||||
// // res: Response,
|
||||
// // next: NextFunction
|
||||
// // ): Promise<Response | void> => {
|
||||
// // try {
|
||||
// // const id = req.params.taskId;
|
||||
// // const userId = req.params.userId;
|
||||
// private readonly getTaskById: RequestHandler = async (
|
||||
// req: Request,
|
||||
// res: Response,
|
||||
// next: NextFunction
|
||||
// ): Promise<Response | void> => {
|
||||
// try {
|
||||
// const id = req.params.taskId;
|
||||
// const userId = req.params.userId;
|
||||
|
||||
// // const data = await this.Taskservice.getTaskById(id, userId);
|
||||
// // res.status(201).send(data);
|
||||
// const data = await this.Taskservice.getTaskById(id, userId);
|
||||
// res.status(201).send(data);
|
||||
|
||||
// // }
|
||||
// // catch(error){
|
||||
// // next(new HttpException(400, 'Cannot create post'));
|
||||
// // }
|
||||
// }
|
||||
// catch(error){
|
||||
// next(new HttpException(400, 'Cannot create post'));
|
||||
// }
|
||||
|
||||
// // }
|
||||
// }
|
||||
|
||||
|
||||
// // private delete = async (
|
||||
// // req: Request,
|
||||
// // res: Response,
|
||||
// // next: NextFunction
|
||||
// // ): Promise<Response | void> => {
|
||||
// // try {
|
||||
// // const id = req.params.taskId;
|
||||
// // const userId = req.params.userId;
|
||||
// // await this.Taskservice.DeleteTask(id, userId);
|
||||
// // return res.status(200).send({ status: "Success", msg: "Data Removed" });
|
||||
// // } catch (error) {
|
||||
// // next(new HttpException(400, 'Cannot create post'));
|
||||
// // }
|
||||
// // };
|
||||
// private delete = async (
|
||||
// req: Request,
|
||||
// res: Response,
|
||||
// next: NextFunction
|
||||
// ): Promise<Response | void> => {
|
||||
// try {
|
||||
// const id = req.params.taskId;
|
||||
// const userId = req.params.userId;
|
||||
// await this.Taskservice.DeleteTask(id, userId);
|
||||
// return res.status(200).send({ status: "Success", msg: "Data Removed" });
|
||||
// } catch (error) {
|
||||
// next(new HttpException(400, 'Cannot create post'));
|
||||
// }
|
||||
// };
|
||||
|
||||
// // private updateTask = async (
|
||||
// // req: Request,
|
||||
// // res: Response,
|
||||
// // next: NextFunction
|
||||
// // ): Promise<Response | void> => {
|
||||
// // try {
|
||||
// private updateTask = async (
|
||||
// req: Request,
|
||||
// res: Response,
|
||||
// next: NextFunction
|
||||
// ): Promise<Response | void> => {
|
||||
// try {
|
||||
|
||||
// // const taskId = req.params.taskId;
|
||||
// // const userId = req.params.userId;
|
||||
// // const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
||||
// const taskId = req.params.taskId;
|
||||
// const userId = req.params.userId;
|
||||
// const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
||||
|
||||
// // const updatedTask = await this.Taskservice.UpdateTask(
|
||||
// // // req.auth!.uid,
|
||||
// // taskId,
|
||||
// // userId,
|
||||
// // // firebase.auth().currentUser.getIdToken()
|
||||
// // reqBody.nom,
|
||||
// // reqBody.description,
|
||||
// // reqBody.logo,
|
||||
// // reqBody.duration,
|
||||
// // reqBody.done,
|
||||
// // // reqBody.tags,
|
||||
// // reqBody.repepat,
|
||||
// // reqBody.deb,
|
||||
// // reqBody.fin
|
||||
// // );
|
||||
// // // res.send('Success add');
|
||||
// // // res.status(201).json({ task });
|
||||
// // res.status(204).send(`Update a new contact: ${updatedTask}`);
|
||||
// // } catch (error) {
|
||||
// // console.log(error);
|
||||
// // next(new HttpException(403, 'Cannot create post'));
|
||||
// // }
|
||||
// // };
|
||||
// const updatedTask = await this.Taskservice.UpdateTask(
|
||||
// // req.auth!.uid,
|
||||
// taskId,
|
||||
// userId,
|
||||
// // firebase.auth().currentUser.getIdToken()
|
||||
// reqBody.nom,
|
||||
// reqBody.description,
|
||||
// reqBody.logo,
|
||||
// reqBody.duration,
|
||||
// reqBody.done,
|
||||
// // reqBody.tags,
|
||||
// reqBody.repepat,
|
||||
// reqBody.deb,
|
||||
// reqBody.fin
|
||||
// );
|
||||
// // res.send('Success add');
|
||||
// // res.status(201).json({ task });
|
||||
// res.status(204).send(`Update a new contact: ${updatedTask}`);
|
||||
// } catch (error) {
|
||||
// console.log(error);
|
||||
// next(new HttpException(403, 'Cannot create post'));
|
||||
// }
|
||||
// };
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
// export default Manager;
|
||||
export default Manager;
|
Loading…
Reference in new issue