Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
feat: Add get user in proximity + get cities
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanM64 committed Jan 14, 2022
1 parent a9dfb4d commit 5703944
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 2 deletions.
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ model User {
birthDate DateTime? @db.Timestamptz(6)
email String @unique
sex Int @default(0)
latitude Float
longitude Float
cityId String @db.Uuid()
city City @relation(fields: [cityId], references: [id])
animals Animal[]
Expand Down
3 changes: 2 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const prisma = new PrismaClient();
async function main() {
dotenv.config();
console.log('Seeding...');
await prisma.account.deleteMany({});
await prisma.animal.deleteMany({});
await prisma.user.deleteMany({});
await prisma.city.deleteMany({});
await prisma.breed.deleteMany({});
await prisma.animal.deleteMany({});

console.log('seeding Cities...');
await seedCities();
Expand Down
6 changes: 6 additions & 0 deletions prisma/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ export const seedUsers = async (): Promise<void> => {
email: Faker.internet.email(),
city: { connect: random.arrayElement(cities.map((m) => ({ id: m.id }))) },
sex: 1,
latitude: 44.837789 + generateRandomFloatInRange(-0.5, 0.5),
longitude: -0.57918 + generateRandomFloatInRange(-0.5, 0.5),
};
promises.push(prisma.user.create({ data: items }));
}
await Promise.all(promises);
};

function generateRandomFloatInRange(min, max) {
return Math.random() * (max - min + 1) + min;
}
1 change: 1 addition & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class AuthController {
@Body() body: CreateUserDto,
@Body('password') password: string,
): Promise<void> {
delete body.password;
const { id } = await this.citizenService.create(body);
await this.authService.register(body.username, password, id);
}
Expand Down
8 changes: 8 additions & 0 deletions src/dtos/users/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class CreateUserDto {
@IsNumber()
@IsOptional()
public sex?: number | null;

@IsNumber()
@IsOptional()
public latitude?: number | null;

@IsNumber()
@IsOptional()
public longitude?: number | null;
}

export class UserDto extends CreateUserDto {
Expand Down
8 changes: 7 additions & 1 deletion src/user/city/city.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import {
Param,
Post,
Put,
Query,
} from '@nestjs/common';
import { City } from '@prisma/client';
import { CreateCityDto } from 'src/dtos';
import { CreateCityDto, FilterDto } from 'src/dtos';

import { CityService } from './city.service';

@Controller('/cities')
export class CityController {
constructor(private readonly cityService: CityService) {}

@Get('/')
async list(@Query() query: FilterDto | undefined): Promise<City[]> {
return await this.cityService.list({});
}

@Get('/:id')
async get(@Param('id') id: string): Promise<City> {
return await this.cityService.get({ id: id });
Expand Down
15 changes: 15 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
Put,
Query,
UseGuards,
Request,
} from '@nestjs/common';
import { User } from '@prisma/client';
import { query } from 'express';
import { CreateUserDto, FilterDto } from 'src/dtos';
import { AuthGuard } from 'src/guards/auth.guard';

Expand All @@ -33,6 +35,19 @@ export class UserController {
});
}

@UseGuards(AuthGuard)
@Get('/proximity')
async getUsersInRadius(
@Query('radius') radius: string,
@Request() req: any,
): Promise<User[]> {
console.log(req);
const currentUser = await this.userService.get({
username: req.user.username,
});
return await this.userService.getUsersInRadius(radius, currentUser);
}

@UseGuards(AuthGuard)
@Get('/:id')
async get(@Param('id') id: string): Promise<User> {
Expand Down
46 changes: 46 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,50 @@ export class UserService {
where,
});
}

async getUsersInRadius(radius, currentUser): Promise<User[]> {
const users = this.prisma.user.findMany({});

const usersInProximity = [];
(await users).map((user) => {
const distance = Math.round(
calcCrow(
currentUser.latitude,
currentUser.longitude,
user.latitude,
user.longitude,
),
);
if (user.id != currentUser.id && distance <= parseInt(radius))
usersInProximity.push({ ...user, distance });
});

return usersInProximity;
}
}

//This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
function calcCrow(lat1, lon1, lat2, lon2) {
const R = 6371; // km
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
// eslint-disable-next-line no-var
const radLat1 = toRad(lat1);
// eslint-disable-next-line no-var
const radLat2 = toRad(lat2);

const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) *
Math.sin(dLon / 2) *
Math.cos(radLat1) *
Math.cos(radLat2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
const d = R * c;
return d;
}

// Converts numeric degrees to radians
function toRad(Value) {
return (Value * Math.PI) / 180;
}

0 comments on commit 5703944

Please sign in to comment.