This commit is contained in:
parent
0577555198
commit
6b6569aa05
22
apps/blakus-api/migrations/20250315172409_place_ratings.js
Normal file
22
apps/blakus-api/migrations/20250315172409_place_ratings.js
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function(knex) {
|
||||
return knex.schema
|
||||
.createTable('place_ratings', function (table) {
|
||||
table.uuid('id').primary().defaultTo(knex.raw(`gen_random_uuid()`));
|
||||
table.uuid('place_id').references('id').inTable('places').onDelete('CASCADE');
|
||||
table.uuid('user_id').references('id').inTable('users').onDelete('CASCADE');
|
||||
table.integer('rating').notNullable();
|
||||
table.timestamps(true, true);
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function(knex) {
|
||||
knex.schema.dropTable('place_ratings');
|
||||
};
|
||||
@ -1,4 +1,5 @@
|
||||
const { fakerLV : faker } = require('@faker-js/faker');
|
||||
const { randomUUID } = require('crypto');
|
||||
|
||||
const USER_COUNT = 500;
|
||||
|
||||
@ -8,6 +9,8 @@ const USER_COUNT = 500;
|
||||
*/
|
||||
exports.seed = async function(knex) {
|
||||
|
||||
const places = await knex('places').select('id');
|
||||
|
||||
const createUser = ({
|
||||
sex = faker.person.sexType(),
|
||||
first_name = `${faker.person.firstName(sex)}`,
|
||||
@ -15,13 +18,14 @@ exports.seed = async function(knex) {
|
||||
email = faker.internet.email({ lastName: last_name, firstName: first_name }).toLocaleLowerCase(),
|
||||
} = {}) => {
|
||||
return {
|
||||
id: randomUUID(),
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
}
|
||||
};
|
||||
|
||||
await knex('users').insert([
|
||||
const users = [
|
||||
createUser({
|
||||
first_name: 'Broks Randolfs',
|
||||
last_name: 'Gailītis',
|
||||
@ -32,5 +36,21 @@ exports.seed = async function(knex) {
|
||||
last_name: 'Gailīte'
|
||||
}),
|
||||
...new Array(USER_COUNT).fill(() => createUser()).map((fn, i) => fn(i)),
|
||||
]);
|
||||
];
|
||||
|
||||
await knex('users').insert(users);
|
||||
|
||||
// Generate multiple random place ratings for each user
|
||||
for (const user of users) {
|
||||
const placeIds = faker.helpers.arrayElements(places, { min: 1, max: 10 });
|
||||
for (const placeId of placeIds) {
|
||||
await knex('place_ratings').insert({
|
||||
id: randomUUID(),
|
||||
user_id: user.id,
|
||||
place_id: placeId.id,
|
||||
rating: faker.number.int({ min: 1, max: 5 }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@ -53,6 +53,8 @@ export default async function (fastify: FastifyInstance) {
|
||||
|
||||
const knownPlaces = await db('places')
|
||||
.leftJoin('categories', 'places.category_id', 'categories.id')
|
||||
.join('place_ratings', 'places.id', 'place_ratings.place_id')
|
||||
.avg('place_ratings.rating AS affinity') // TODO: work on real affinity score
|
||||
.select(
|
||||
'places.id',
|
||||
'places.name',
|
||||
@ -67,10 +69,13 @@ export default async function (fastify: FastifyInstance) {
|
||||
db.raw('ST_Y(places.coordinates::geometry) AS lat'),
|
||||
db.raw('ST_X(places.coordinates::geometry) AS lon'),
|
||||
'categories.name AS category'
|
||||
).whereRaw(
|
||||
)
|
||||
.whereRaw(
|
||||
'ST_DWithin(places.coordinates, ST_SetSRID(ST_MakePoint(?, ?), 4326), ?)',
|
||||
[lon, lat, 2000]
|
||||
);
|
||||
)
|
||||
.groupBy('places.id', 'categories.name')
|
||||
.orderBy('affinity', 'desc')
|
||||
|
||||
// const externalIds = await db.select('externalId').from('places').where('externalId', 'like', 'osm:%');
|
||||
|
||||
@ -114,7 +119,7 @@ export default async function (fastify: FastifyInstance) {
|
||||
lat: place.lat,
|
||||
lon: place.lon,
|
||||
},
|
||||
affinity: Math.floor(Math.random() * 101) // TODO: this is just a placeholder for "affinity" calculation
|
||||
affinity: place.affinity // TODO: this is just a placeholder for "affinity" calculation
|
||||
}));
|
||||
|
||||
reply.send(blakus);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user