-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apis for water-footprint-calculator added. Co-authored-by: arpankumarde <[email protected]>
- Loading branch information
1 parent
10b56e0
commit 2eeb06c
Showing
33 changed files
with
1,053 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |
Oops, something went wrong.