Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1]: Water footprint calculator #3

Merged
merged 13 commits into from
Aug 6, 2024
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules/
.env
16 changes: 16 additions & 0 deletions data/dao/user/UserDAO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { User } from '../../db_models/User';
import { Otp } from "../../db_models/Otp";
import { RegisterUserData } from '../../../domain/models/RegisterUserData';

export interface UserDAO {

insertUser(userData: RegisterUserData): Promise<User>;

fetchUserByEmail(email: string): Promise<User>;

getOtp(email: string, timestamp: number): Promise<Otp>;

insertOtp(email: string, otp: string): Promise<Otp>;

updatePassword(email: string, password: string): Promise<Boolean>;
}
119 changes: 119 additions & 0 deletions data/dao/user/UserDAOImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { Otp } from "../../db_models/Otp";
import { RegisterUserData } from "../../../domain/models/RegisterUserData";
import { User } from "../../db_models/User";
import { FetchUserByEmail, GetOTPUserDAO, InsertOTPUserDAO, InsertUserUSerDAO, OTPNotFoundInDB, UnknownDatabaseError, UpdatePasswordUserDao, UserNotFoundInDB } from "../../../routes/auth/errorhandling/ErrorCodes";
import { AuthError } from "../../../routes/auth/errorhandling/ErrorUtils";
import { UserDAO } from "./UserDAO";
import { DatabaseError } from "../../../utils/errors/ErrorUtils";




export class UserDAOImpl implements UserDAO {

async updatePassword(email: string, password: string): Promise<Boolean> {
try {
const [updatedCount] = await User.update({ password_hash: password }, { where: { email: email } });
return updatedCount == 1
} catch (error) {
if (error instanceof Error) {
throw new DatabaseError(error.message, UpdatePasswordUserDao);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- updatePassword", UnknownDatabaseError);
}

}
}

async insertOtp(email: string, otp: string): Promise<Otp> {
try {
const now = new Date()
const otpData = await Otp.create({ email: email, otp_hash: otp, generated_at: now.getTime() })
return otpData
} catch (error) {
if (error instanceof Error) {
throw new DatabaseError(error.message, InsertOTPUserDAO);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- insertOtp", UnknownDatabaseError);
}
}
}

async getOtp(email: string, timestamp: number): Promise<Otp> {
try {


const otpRow = await Otp.findOne({
where: {
email: email,
generated_at: timestamp
}
});

if (otpRow == null) {
throw new AuthError("otp with email: " + email + "doesn't exist in the database", OTPNotFoundInDB)
}

return otpRow
} catch (error) {
if (error instanceof AuthError) {
throw error
} else if (error instanceof Error) {
throw new DatabaseError("error.message", GetOTPUserDAO);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- getOtp", UnknownDatabaseError);
}
}
}


async insertUser(userData: RegisterUserData): Promise<User> {

try {
const user = await User.create({ name: userData.name, email: userData.email, password_hash: userData.password_hash })
return user
} catch (error) {
if (error instanceof AuthError) {
throw error
} else if (error instanceof Error) {
throw new DatabaseError(error.message, InsertUserUSerDAO);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- insertUser", UnknownDatabaseError);
}
}


}

async fetchUserByEmail(email: string): Promise<User> {

try {
const emailToFind = email;
const user = await User.findOne({
where: {
email: emailToFind
}
});
if (user == null) {
throw new AuthError("user with email: " + email + " is not found in the database", UserNotFoundInDB)
}
return user;
} catch (error) {

if (error instanceof AuthError) {
throw error
} else if (error instanceof Error) {
throw new DatabaseError(error.message, FetchUserByEmail);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- fetchUserByEmail", UnknownDatabaseError);
}
}


}


}



52 changes: 52 additions & 0 deletions data/dao/waterft_calculator/WaterFtCalcDAO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { IngredientRow } from "../../db_models/IngredientRowData";
import { IngredientRowItem } from "../../db_models/IngredientRowItem";

export interface WaterFtCalcDAO {

insertIngredientRow(
rowOrder: number
): Promise<IngredientRow>

insertIngredientRowItem(
itemId: number,
rowId: number,
name: string,
amt: number,
unit: string,
waterFootprint: number,
unselectedBgImageUrl: string,
selectedBgImageUrl: string,
sampleImageUrl: string,
sampleImageSize: number,
scaleFactor: number,
iconScalefactor: number,
cornerType: string,
doneXOffSet: number,
doneYOffSet: number,
pluseXOffSet: number,
pluseYOffSet: number,
minusXOffSet: number,
minusYOffSet: number,
xOffset: number,
yOffset: number
): Promise<IngredientRowItem>


getIngredientRow(
rowId: number,
): Promise<IngredientRow>

getIngredientRows(

): Promise<IngredientRow[]>

getIngredientRowItem(
rowId: number,
): Promise<IngredientRowItem>

getIngredientRowItems(
rowId: number,
): Promise<IngredientRowItem[]>

getWaterConsumptionOfIngredient(ingredientId: number): Promise<number>
}
177 changes: 177 additions & 0 deletions data/dao/waterft_calculator/WaterFtCalcDAOImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

import { GetIngredientRowDAOError, GetIngredientRowItemDAOError, GetIngredientRowItemsDAOError, GetIngredientRowsDAOError, GetWaterConsumptionOfIngredientDAOError, IngredientNotFoundError, InsertIngredientRowDAOError, InsertIngredientRowItemDAOError, UnknownDatabaseError } from '../../../routes/water_ft_catculator/errorhandling/ErrorCodes';
import { WaterFtCalcError } from '../../../routes/water_ft_catculator/errorhandling/ErrorUtils';
import { WaterFtCalcDAO } from './WaterFtCalcDAO';
import { IngredientRow } from '../../db_models/IngredientRowData';
import { IngredientRowItem } from '../../db_models/IngredientRowItem';
import { ForeignKeyConstraintError, UniqueConstraintError } from 'sequelize';
import { DatabaseError } from "../../../utils/errors/ErrorUtils";


const FileName = "WaterFtCalcDAOImpl"

export class WaterFtCalcDAOImpl implements WaterFtCalcDAO {

async getIngredientRow(rowId: number): Promise<IngredientRow> {
try {
const row = await IngredientRow.findOne({ where: { id: rowId } })
if (row == null) {
throw new WaterFtCalcError("Row not found", GetIngredientRowDAOError)
}
return row
} catch (error) {
if (error instanceof Error) {

throw new DatabaseError(error.message, InsertIngredientRowDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- getIngredientRow`, UnknownDatabaseError);
}
}
}
async getIngredientRows(): Promise<IngredientRow[]> {
try {
const rows = await IngredientRow.findAll()
if (rows == null) {
throw new WaterFtCalcError("Rows not found", GetIngredientRowsDAOError)
}
return rows
} catch (error) {
if (error instanceof Error) {
throw new DatabaseError(error.message, GetIngredientRowsDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- getIngredientRows`, UnknownDatabaseError);
}
}
}
async getIngredientRowItem(rowId: number): Promise<IngredientRowItem> {
try {

const row = await IngredientRowItem.findOne({ where: { id: rowId } })
if (row == null) {
throw new WaterFtCalcError("Row item not found", GetIngredientRowItemDAOError)
}
return row
} catch (error) {
if (error instanceof Error) {

throw new DatabaseError(error.message, InsertIngredientRowDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- getIngredientRowItem`, UnknownDatabaseError);
}
}
}
async getIngredientRowItems(rowId: number): Promise<IngredientRowItem[]> {
try {

const rows = await IngredientRowItem.findAll({ where: { rowId: rowId } })
if (rows == null) {
throw new WaterFtCalcError("Row items not found", GetIngredientRowItemsDAOError)
}
return rows
} catch (error) {
if (error instanceof Error) {

throw new DatabaseError(error.message, GetIngredientRowItemsDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- getIngredientRowItems`, UnknownDatabaseError);
}
}
}
async insertIngredientRow(rowOrder: number): Promise<IngredientRow> {
try {
const row = await IngredientRow.create({ rowOrder: rowOrder })
return row
} catch (error) {
if (error instanceof Error) {

throw new DatabaseError(error.message, InsertIngredientRowDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- insertIngredientRow`, UnknownDatabaseError);
}
}
}
async insertIngredientRowItem(
itemId: number,
rowId: number,
name: string,
amt: number,
unit: string,
waterFootprint: number,
unselectedBgImageUrl: string,
selectedBgImageUrl: string,
sampleImageUrl: string,
sampleImageSize: number,
scaleFactor: number,
iconScalefactor: number,
cornerType: string,
doneXOffSet: number,
doneYOffSet: number,
pluseXOffSet: number,
pluseYOffSet: number,
minusXOffSet: number,
minusYOffSet: number,
xOffset: number,
yOffset: number
): Promise<IngredientRowItem> {
try {
const rowItem = await IngredientRowItem.create(
{
itemId: itemId,
rowId: rowId,
name: name,
amt: amt,
unit: unit,
water_footprint: waterFootprint,
unselectedBgImageUrl: unselectedBgImageUrl,
selectedBgImageUrl: selectedBgImageUrl,
sampleImageUrl: sampleImageUrl,
sampleImageSize: sampleImageSize,
scaleFactor: scaleFactor,
iconScalefactor: iconScalefactor,
cornerType: cornerType,
doneXOffSet: doneXOffSet,
doneYOffSet: doneYOffSet,
pluseXOffSet: pluseXOffSet,
pluseYOffSet: pluseYOffSet,
minusXOffSet: minusXOffSet,
minusYOffSet: minusYOffSet,
xOffset: xOffset,
yOffset: yOffset
}
)
return rowItem
} catch (error) {


if (error instanceof Error) {
throw new DatabaseError(error.message, InsertIngredientRowItemDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- insertIngredientRowItem`, UnknownDatabaseError);
}
}
}
async getWaterConsumptionOfIngredient(ingredientId: number): Promise<number> {
try {

const ingredient = await IngredientRowItem.findByPk(ingredientId);

if (ingredient == null) {
throw new WaterFtCalcError("ingredient is not available", IngredientNotFoundError)
}

return ingredient.water_footprint

} catch (error) {
if (error instanceof WaterFtCalcError) {
throw error
} else if (error instanceof Error) {

throw new DatabaseError(error.message, GetWaterConsumptionOfIngredientDAOError);
} else {
throw new DatabaseError(`e is not a instance of Error: ${FileName} --- getWaterConsumptionOfIngredient`, UnknownDatabaseError);
}
}
}


}
Loading