Skip to content

Commit

Permalink
rating and views component
Browse files Browse the repository at this point in the history
  • Loading branch information
laughugin committed Jun 3, 2024
1 parent ffff113 commit 4fd4dba
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 37 deletions.
2 changes: 2 additions & 0 deletions plugins/ios-backend/migrations/20240416001940_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ exports.up = async function up(knex) {
table.string('user_entity_ref').notNullable().comment("Username from catalog ");
table.text('user_avatar').comment("Link to avatar of user");
table.specificType('user_projects_ids', 'INTEGER[]').comment("Ids of projects that user in");
table.specificType('viewed_projects_ids', 'INTEGER[]').comment("Ids of projects that user ever viewed");
table.specificType('rated_projects_ids', 'INTEGER[]').comment("Ids of projects that user ever rated");
});

await knex.schema.createTable('ios-table-comments', table => {
Expand Down
105 changes: 105 additions & 0 deletions plugins/ios-backend/src/service/DatabaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,111 @@ export class DatabaseHandler {
}
}

async updateUserViewedProjects(
user_entity_ref: string,
viewed_project_id: number[]
): Promise<void> {
try {
const projectsArray = "{" + viewed_project_id.join(",") + "}";

await this.client.raw(`
UPDATE "ios-table-users"
SET "viewed_projects_ids" = ?
WHERE "user_entity_ref" = ?
`, [projectsArray, user_entity_ref]);

console.log(`User projects updated successfully.`);
} catch (error) {
console.error('Error updating user projects:', error);
throw error;
}
}

async getUsersViewedProjects(user_ref: string): Promise<number[]> {
const result = await this.client
.select('viewed_projects_ids')
.from('ios-table-users')
.where('user_entity_ref', user_ref);

// Assuming result[0].viewed_projects_ids contains the JSON array
return result.length > 0 ? JSON.parse(result[0].viewed_projects_ids) : [];
}

async updateUserRatedProjects(
user_entity_ref: string,
rated_project_id: number[]
): Promise<void> {
try {
const projectsArray = "{" + rated_project_id.join(",") + "}";

await this.client.raw(`
UPDATE "ios-table-users"
SET "rated_projects_ids" = ?
WHERE "user_entity_ref" = ?
`, [projectsArray, user_entity_ref]);

console.log(`User projects updated successfully.`);
} catch (error) {
console.error('Error updating user projects:', error);
throw error;
}
}

async deleteUserRatedProject(
user_entity_ref: string,
project_id_to_delete: number
): Promise<void> {
try {
// Fetch the current rated_projects_ids for the user
const result = await this.client
.select('rated_projects_ids')
.from('ios-table-users')
.where('user_entity_ref', user_entity_ref);

if (result.length === 0) {
console.log(`No user found with user_entity_ref: ${user_entity_ref}`);
return;
}

// Parse the current rated_projects_ids
let rated_projects_ids: number[] = result[0].rated_projects_ids || [];
if (typeof rated_projects_ids === 'string') {
rated_projects_ids = JSON.parse(rated_projects_ids);
}

// Filter out the project_id_to_delete
const updated_rated_projects_ids = rated_projects_ids.filter(
id => id !== project_id_to_delete
);

// Convert the array to the required format
const projectsArray = "{" + updated_rated_projects_ids.join(",") + "}";

// Update the database with the new array
await this.client.raw(`
UPDATE "ios-table-users"
SET "rated_projects_ids" = ?
WHERE "user_entity_ref" = ?
`, [projectsArray, user_entity_ref]);

console.log(`User projects updated successfully.`);
} catch (error) {
console.error('Error updating user projects:', error);
throw error;
}
}


async getUsersRatedProjects(user_ref: string): Promise<number[]> {
const result = await this.client
.select('rated_projects_ids')
.from('ios-table-users')
.where('user_entity_ref', user_ref);

return result.length > 0 ? JSON.parse(result[0].rated_projects_ids) : [];
}


async getUsersByProjectID(projectId: number): Promise<Array<{ user_id: number; username: string; user_avatar: string; entity_ref: string }>> {
const result = await this.client
.select('user_avatar', 'user_entity_ref')
Expand Down
123 changes: 119 additions & 4 deletions plugins/ios-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,108 @@ export async function createRouter(
response.status(500).send('Internal server error.');
}
});
router.put('/ios_members/add_view', async (request, response) => {
const {
user_entity,
viewed_projects_id,
} = request.body;

let viewed_projects_ids_ar = await dbHandler.getUsersViewedProjects(user_entity);

let viewed_projects_ids
console.info('312-------------------- ', viewed_projects_ids);
if (!viewed_projects_ids_ar || viewed_projects_ids_ar.length === 0) {
viewed_projects_ids = [viewed_projects_id];
} else {
console.info('313-------------------- ', viewed_projects_ids);
viewed_projects_ids_ar = viewed_projects_ids_ar[0][0];
console.info('314-------------------- ', viewed_projects_ids);
viewed_projects_ids_ar.push(viewed_projects_id);
}
console.info('315-------------------- ', viewed_projects_ids);

try {
await dbHandler.updateUserViewedProjects(user_entity, viewed_projects_ids);
response.status(200).send('User viewed projects updated successfully.');
} catch (error) {
console.error('Error updating user viewed projects:', error);
response.status(500).send('Internal server error.');
}
});

router.post('/ios_members/views', async (request, response) => {
const {
user_entity,
} = request.body;
console.log("body", user_entity);
try{
const viewed = await dbHandler.getUsersViewedProjects(user_entity);
response.status(200).json(viewed);
} catch (error) {
console.error('Error getting user views: ', error);
response.status(500).send('Internal server error');
}
});

router.put('/ios_members/add_rate/:rated_projects_ids', async (request, response) => {
const {
user_entity,
} = request.body;
const rated_projects_id_str = request.params.rated_projects_ids;
const rated_projects_id = parseInt(rated_projects_id_str, 10);
let rated_projects_ids_ar = await dbHandler.getUsersRatedProjects(user_entity);

let rated_projects_ids

console.info('312-------------------- ', rated_projects_ids_ar);
if (!rated_projects_ids_ar || rated_projects_ids_ar.length === 0) {
rated_projects_ids = [rated_projects_id];
} else {
rated_projects_ids_ar = rated_projects_ids_ar[0][0];
rated_projects_ids_ar.push(rated_projects_id);
}
console.info('315-------------------- ', rated_projects_ids);



try {
await dbHandler.updateUserRatedProjects(user_entity, rated_projects_ids);
response.status(200).send('User rated projects updated successfully.');
} catch (error) {
console.error('Error updating user rated projects:', error);
response.status(500).send('Internal server error.');
}
});

router.post('/ios_members/rates', async (request, response) => {
const {
user_entity,
} = request.body;
console.log("body", user_entity);
try{
const rated = await dbHandler.getUsersRatedProjects(user_entity);
response.status(200).json(rated);
} catch (error) {
console.error('Error getting user rates: ', error);
response.status(500).send('Internal server error');
}
});

router.delete('/ios_members/rates_del/:project_id', async (request, response) => {
const project_id_str = request.params.project_id;
const {
user_entity,
} = request.body;
const project_id = parseInt(project_id_str, 10);

try {
await dbHandler.deleteUserRatedProject(user_entity, project_id);
response.status(200).send(`Value deleted successfully.`);
} catch (error) {
console.error('Error deleting value:', error);
response.status(500).send('Internal server error');
}
});

router.post('/projects/:project_id/comments', async (request, response) => {
const project_id_ref_str = request.params.project_id;
Expand Down Expand Up @@ -349,12 +451,11 @@ export async function createRouter(
}
});

router.put('/projects/:project_id', async (request, response) => {
router.put('/projects/views/:project_id/:project_views', async (request, response) => {
const project_id_str = request.params.project_id;
const project_id = parseInt(project_id_str, 10);
const {
project_views
} = request.body
const project_views_str = request.params.project_views;
const project_views = parseInt(project_views_str, 10);
try {
await dbHandler.updateProjectViews(project_id, project_views);
response.status(200).send('Project views updated successfully.');
Expand All @@ -364,6 +465,20 @@ export async function createRouter(
}
});

router.put('/projects/rating/:project_id/:project_rating', async (request, response) => {
const project_id_str = request.params.project_id;
const project_id = parseInt(project_id_str, 10);
const project_rating_str = request.params.project_rating;
const project_rating = parseInt(project_rating_str, 10);
try {
await dbHandler.updateProjectRating(project_id, project_rating);
response.status(200).send('Project rating updated successfully.');
} catch (error) {
console.error('Error updating project: rating', error);
response.status(500).send('Internal server error.');
}
});

router.use(errorHandler());
return router;
}
Loading

0 comments on commit 4fd4dba

Please sign in to comment.