@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
83 lines • 3.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DBAccount = exports.deleteAccount = exports.updateAccount = exports.readAccountByUsername = exports.readAllAccounts = exports.createAccount = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const Account_1 = require("../models/Account");
const Rating_1 = require("../models/Rating");
/**
* Stores user account information.
*/
const createAccount = async (account) => {
const { username, email, hashedPassword, rating } = account;
const user = new exports.DBAccount({
username: username,
email: email,
password: hashedPassword,
rating: {
value: rating.value,
},
});
await user.save();
};
exports.createAccount = createAccount;
/**
* Reads all accounts from the database.
* @returns { Account }[] - List of all accounts
*/
const readAllAccounts = async () => {
const accounts = await exports.DBAccount.find();
return accounts.map(account => Account_1.AccountFactory.create(account.username, account.email, account.password, Rating_1.RatingFactory.create(account.rating.value)));
};
exports.readAllAccounts = readAllAccounts;
/**
* Reads an account by username from the database.
* @param username - The username to search for
* @returns { Account | null } - The account if found, null otherwise
*/
const readAccountByUsername = async (username) => {
const account = await exports.DBAccount.findOne({ username });
if (!account)
return null;
return Account_1.AccountFactory.create(account.username, account.email, account.password, Rating_1.RatingFactory.create(account.rating.value));
};
exports.readAccountByUsername = readAccountByUsername;
/**
* Updates an existing account in the database.
* @param account - The account with updated information
* @returns {boolean} - True if account was updated, false if account not found
*/
const updateAccount = async (account) => {
const { username, email, hashedPassword, rating } = account;
const result = await exports.DBAccount.updateOne({ username }, {
email,
password: hashedPassword,
rating: {
value: rating.value,
},
});
return result.modifiedCount > 0;
};
exports.updateAccount = updateAccount;
/**
* Deletes an account by username.
* @param account - The account to delete
* @returns {Promise<boolean>} - True if deleted, false otherwise.
*/
const deleteAccount = async (account) => {
const result = await exports.DBAccount.deleteOne({ username: account.username });
return result.deletedCount > 0;
};
exports.deleteAccount = deleteAccount;
const accountSchema = new mongoose_1.default.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true },
password: { type: String, required: true },
rating: {
value: { type: Number, required: true },
},
});
exports.DBAccount = mongoose_1.default.model('Account', accountSchema);
//# sourceMappingURL=account.js.map