src/database/TherapistsDAO.js
//Functions that retrieve and process data from therapists-database.
import therapists from '../database/Therapists';
/**
* Check if there is a therapist with the given username in the database.
* @param {string} username - therapist username
* @returns {boolean} True if there is a therapist with the given username.
*/
function isValidUsername(username) {
const therapist = findTherapistFromUsername(username);
if (typeof therapist !== 'undefined') {
return true;
} else {
return false;
}
}
/**
* Checks if the given password is the correct password for the given therapist id.
* @param {number} therapistId - The id of the therapist;
* @param {string} password The password to be checked;
* @returns {boolean} True if the password is correct. Otherwise false.
*/
function isValidPassword(therapistId, password) {
const therapist = findTherapistFromId(therapistId);
return password === therapist.password;
}
/**
* Looks for the therapist that has the given username and returns it.
* @param {string} username - username of the therapist
* @returns {Object} Therapist which match the given username. Otherwise 'undefined'
*/
function findTherapistFromUsername(username) {
const t = getTherapists();
const therapist = t.find(obj => obj.username === username);
return therapist;
}
/**
* Looks for the given username in the database and returns the id if found.
* @param {string} username - username of therapist
* @returns {number} the id number if the therapist is found.
* -1 if username is empty string
* -2 if username is not found in the database.
*/
function getIdFromUsername(username) {
let id = -1; // No entry from user
if (typeof username !== 'undefined') {
// Check if the username is in the database
if (isValidUsername(username)) {
// Retrieves id
const therapist = findTherapistFromUsername(username);
id = therapist.id;
} else {
id = -2; // Not valid username
}
}
return id;
}
/**
* Looks for the therapist that has the given id number and returns it.
* @param {number} therapist - therapist id
* @returns {Object} Therapist which match the given username. Otherwise 'undefined'
*/
function findTherapistFromId(therapistId) {
const t = getTherapists();
const therapist = t.find(obj => obj.id === therapistId);
return therapist;
}
/**
* Fetch data on therapists from the .js file representing the database.
* @returns {Object[]} An array containing all therapists
*/
function getTherapists() {
return therapists;
}
/**
* Gets the name of a therapist with given id
* @param {number} id - therapist id
* @returns {string} name of therapist
*/
function getTherapistNameFromId(id) {
const therapist = findTherapistFromId(id);
return therapist.name;
}
/**
* Gets the image from the therapist object with given id
* @param {number} id - therapist id
* @returns {Object} the image of the therapist
*/
function getTherapistImageFromId(id) {
const therapist = findTherapistFromId(id);
return therapist.image;
}
export {
getIdFromUsername,
isValidPassword,
getTherapistNameFromId,
getTherapistImageFromId,
};