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
2 changes: 0 additions & 2 deletions data/dao/user/UserDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export interface UserDAO {

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

fetchUser(userId: number): Promise<User>;

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

getOtp(email: string, timestamp: number): Promise<Otp>;
Expand Down
42 changes: 11 additions & 31 deletions data/dao/user/UserDAOImpl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Otp } from "../../db_models/Otp";
import { RegisterUserData } from "../../../domain/models/RegisterUserData";
import { User } from "../../db_models/User";
import { FetchUserByEmail, FetchUserUserDAO, InsertOTPUserDAO, InsertUserUSerDAO, IsEmailExistUserDAO, OTPNotFoundInDB, UnknownDatabaseError, UserNotFoundInDB } from "../../../routes/auth/errorhandling/ErrorCodes";
import { AuthError, DatabaseError } from "../../../routes/auth/errorhandling/ErrorUtils";
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";



Expand All @@ -15,7 +16,12 @@ export class UserDAOImpl implements UserDAO {
const [updatedCount] = await User.update({ password_hash: password }, { where: { email: email } });
return updatedCount == 1
} catch (error) {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- updatePassword", UnknownDatabaseError);
if (error instanceof Error) {
throw new DatabaseError(error.message, UpdatePasswordUserDao);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- updatePassword", UnknownDatabaseError);
}

}
}

Expand All @@ -25,9 +31,7 @@ export class UserDAOImpl implements UserDAO {
const otpData = await Otp.create({ email: email, otp_hash: otp, generated_at: now.getTime() })
return otpData
} catch (error) {
if (error instanceof AuthError) {
throw error
} else if (error instanceof 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);
Expand Down Expand Up @@ -55,16 +59,14 @@ export class UserDAOImpl implements UserDAO {
if (error instanceof AuthError) {
throw error
} else if (error instanceof Error) {
throw new DatabaseError("error.message", IsEmailExistUserDAO);
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 {
Expand All @@ -81,28 +83,6 @@ export class UserDAOImpl implements UserDAO {
}


}

async fetchUser(userId: number): Promise<User> {

try {
const user = await User.findByPk(userId);

if (user == null) {
throw new AuthError("user with id: " + userId + "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, FetchUserUserDAO);
} else {
throw new DatabaseError("e is not a instance of Error: UserDAOImpl --- fetchUser", UnknownDatabaseError);
}
}


}

async fetchUserByEmail(email: string): Promise<User> {
Expand Down
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);
}
}
}


}
45 changes: 45 additions & 0 deletions data/db_models/IngredientRowData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Optional, Model, DataTypes, Sequelize } from "sequelize";
import { IngredientRowItem } from "./IngredientRowItem";


interface IngredientRowAttributes {
id: number;
rowOrder: number;
}

interface IngredientRowCreationAttributes extends Optional<IngredientRowAttributes, 'id'> {}

export class IngredientRow extends Model<IngredientRowAttributes, IngredientRowCreationAttributes> {
static hasMany(IngredientRowItem: IngredientRowItem, arg1: { foreignKey: string; }) {
throw new Error('Method not implemented.');
}
declare id: number;
declare rowOrder: number;

public readonly createdAt!: Date;
public readonly updatedAt!: Date;

}

export function initIngredientRow(sequelize: Sequelize) {

IngredientRow.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
rowOrder: {
type: DataTypes.INTEGER,
allowNull: false,
unique: true,
}
},
{
sequelize: sequelize,
tableName: 'ingredient_rows',
timestamps: true
}
).sync({ alter: true })
}
Loading