-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for a circular issue for Alignment Enum • building out the cart e…
…ndpoint • directory/code clean up
- Loading branch information
1 parent
4b6e8dd
commit efabf05
Showing
37 changed files
with
422 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import { errFormatResponseUtil } from '../../../../utils/err.format.response.util'; | ||
import { clearCartService, getCartService, updateCartService } from '../services/cart.service'; | ||
|
||
const getCartController = async (req: Request<null, null, null, null>, res: Response) => { | ||
try { | ||
const pokemonDetail = await getCartService(); | ||
|
||
res.status(200).json(pokemonDetail); | ||
} catch (err) { | ||
res.status(err?.status).json(errFormatResponseUtil(err)); | ||
} | ||
}; | ||
|
||
interface CartBodyProps { | ||
id: any; | ||
} | ||
|
||
const updateCartController = async ( | ||
req: Request<null, null, CartBodyProps, null>, | ||
res: Response | ||
) => { | ||
const { body } = req; | ||
|
||
try { | ||
const pokemonDetail = await updateCartService(body); | ||
|
||
res.status(200).json(pokemonDetail); | ||
} catch (err) { | ||
res.status(err?.status).json(errFormatResponseUtil(err)); | ||
} | ||
}; | ||
|
||
const clearCartController = async (req: Request<null, null, null, null>, res: Response) => { | ||
try { | ||
const pokemonDetail = await clearCartService(); | ||
|
||
res.status(200).json(pokemonDetail); | ||
} catch (err) { | ||
res.status(err?.status).json(errFormatResponseUtil(err)); | ||
} | ||
}; | ||
|
||
export { getCartController, updateCartController, clearCartController }; |
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,27 @@ | ||
export let cartDatabase = []; | ||
|
||
export const getCartDataCallUtil = async () => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(cartDatabase); | ||
}, 500); | ||
}); | ||
}; | ||
|
||
export const updateCartDataCallUtil = async (data) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
cartDatabase = data; | ||
resolve(cartDatabase); | ||
}, 500); | ||
}); | ||
}; | ||
|
||
export const clearCartDataCallUtil = async () => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
cartDatabase = []; | ||
resolve(cartDatabase); | ||
}, 500); | ||
}); | ||
}; |
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 @@ | ||
export * from './routes/cart.route'; |
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,20 @@ | ||
export interface PokemonListQueryProps { | ||
offset?: string | string[]; | ||
limit?: string | string[]; | ||
} | ||
|
||
export interface PokemonDetailQueryProps { | ||
id: string; | ||
} | ||
|
||
export interface PokemonSpeciesParamsProps { | ||
id: string; | ||
} | ||
|
||
export interface PokemonDetailsParamsProps { | ||
id: string; | ||
} | ||
|
||
export interface PokemonEvolutionChainParamsProps { | ||
id: string; | ||
} |
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,16 @@ | ||
import express from 'express'; | ||
|
||
import { | ||
clearCartController, | ||
getCartController, | ||
updateCartController, | ||
} from '../controllers/cart.controller'; | ||
|
||
const cartRoute = express.Router(); | ||
|
||
cartRoute.get('/', getCartController); | ||
cartRoute.put('/update/:id', updateCartController); | ||
cartRoute.put('/clear', clearCartController); | ||
// cartRoute.delete('/item/:id', clearCartController); | ||
|
||
export { cartRoute }; |
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,82 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { getPokemonSpeciesService } from '../../pokemon'; | ||
import { getPokemonDetailService } from '../../pokemon/services/pokemon.detail.service'; | ||
import { | ||
clearCartDataCallUtil, | ||
getCartDataCallUtil, | ||
updateCartDataCallUtil, | ||
} from '../database/cart.database'; | ||
|
||
interface CartDataProps { | ||
id: string; | ||
name: string; | ||
price: number; | ||
quantity: number; | ||
image: string; | ||
isLegendary: boolean; | ||
isMythical: boolean; | ||
} | ||
|
||
const doesItemExistKeyFn = (cartData, id) => { | ||
const cartLength = cartData.length; | ||
let key = null; | ||
|
||
for (let loop = 0; loop < cartLength; loop++) { | ||
if (cartData[loop]?.id == id) { | ||
key = loop; | ||
break; | ||
} | ||
} | ||
return key; | ||
}; | ||
|
||
const getCartService = async () => { | ||
return await getCartDataCallUtil(); | ||
}; | ||
|
||
const getSpeciesDetail = async (id: string) => { | ||
return Promise.all([ | ||
await getPokemonSpeciesService({ id }).catch(() => 'err'), | ||
await getPokemonDetailService({ id }).catch(() => 'err'), | ||
]).then((data) => { | ||
if (data[0] === 'err' || data[1] === 'err') { | ||
return 'err'; | ||
} | ||
return { ...data[0], ...data[1] }; | ||
}); | ||
}; | ||
|
||
const updateCartService = async (payload) => { | ||
const currentCartData: any = await getCartDataCallUtil(); | ||
const doesItemExistKey = doesItemExistKeyFn(currentCartData, payload?.id); | ||
const finalRes = currentCartData; | ||
|
||
if (doesItemExistKey !== null) { | ||
const currentObj = currentCartData[doesItemExistKey]; | ||
currentObj.quantity = currentObj.quantity + 1; | ||
|
||
return currentObj; | ||
} | ||
|
||
const additionalData = await getSpeciesDetail(payload?.id); | ||
|
||
const finalPayload = { | ||
id: payload?.id, | ||
name: additionalData?.name, | ||
price: 500, | ||
quantity: 1, | ||
image: additionalData?.sprites?.other?.['official-artwork']?.front_default, | ||
isLegendary: additionalData?.is_legendary, | ||
isMythical: additionalData?.is_mythical, | ||
}; | ||
finalRes.push(finalPayload); | ||
|
||
return finalPayload; | ||
}; | ||
|
||
const clearCartService = async () => { | ||
const res = await clearCartDataCallUtil(); | ||
return res; | ||
}; | ||
|
||
export { getCartService, updateCartService, clearCartService }; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './routes/pokemon.route'; | ||
export * from './services/pokemon.species.service'; |
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
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
import UiPokemonDetailModal from './pokemon.detail.modal/pokemon.detail.modal.view'; | ||
import UiPokemonList from './pokemon.list/pokemon.list.view'; | ||
|
||
export { UiPokemonList, UiPokemonDetailModal }; | ||
export * from './pokemon.list'; | ||
export * from './pokemon.detail.modal'; |
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,3 @@ | ||
import UiPokemonDetailModal from './pokemon.detail.modal.view'; | ||
|
||
export { UiPokemonDetailModal }; |
Oops, something went wrong.