11import { Request , Response } from "express" ;
22import { currencyService } from "../services/currencyService" ;
33import { Conversion , Currency } from "../types/currency" ;
4+ import { BadRequestError } from "../utils/apiError" ;
45
56const listCurrencies = async ( _ : Request , res : Response ) => {
6- try {
7- const currencies = await currencyService . getCurrencies ( ) ;
8-
9- return res . status ( 200 ) . json ( currencies ) ;
10- } catch ( error : any ) {
11- return res . status ( 500 ) . json ( { message : error . message } ) ;
12- }
7+ const currencies = await currencyService . getCurrencies ( ) ;
8+ return res . status ( 200 ) . json ( currencies ) ;
139} ;
1410
1511const getCurrency = async ( req : Request , res : Response ) => {
1612 const code = ( req . params . code as string ) . toUpperCase ( ) ;
1713
18- try {
19- const supportedCurrencies =
20- await currencyService . getSupportedCurrencies ( ) ;
14+ const supportedCurrencies = await currencyService . getSupportedCurrencies ( ) ;
2115
22- if ( ! supportedCurrencies . includes ( code ) ) {
23- return res
24- . status ( 400 )
25- . json ( { message : "The currency is not supported" } ) ;
26- }
16+ if ( ! supportedCurrencies . includes ( code ) ) {
17+ throw new BadRequestError ( "The currency is not supported" ) ;
18+ }
2719
28- const currency = await currencyService . getCurrency ( code ) ;
20+ const currency = await currencyService . getCurrency ( code ) ;
2921
30- return res . status ( 200 ) . json ( currency ) ;
31- } catch ( error : any ) {
32- return res . status ( 500 ) . json ( { message : error . message } ) ;
33- }
22+ return res . status ( 200 ) . json ( currency ) ;
3423} ;
3524
3625const convertCurrency = async ( req : Request , res : Response ) => {
@@ -43,36 +32,29 @@ const convertCurrency = async (req: Request, res: Response) => {
4332 const uppercaseFrom = from . toUpperCase ( ) ;
4433 const uppercaseTo = to . toUpperCase ( ) ;
4534
46- try {
47- const supportedCurrencies =
48- await currencyService . getSupportedCurrencies ( ) ;
49-
50- if ( ! supportedCurrencies . includes ( uppercaseFrom ) ) {
51- return res
52- . status ( 400 )
53- . json ( { message : "The currency from is not supported" } ) ;
54- }
55-
56- if ( ! supportedCurrencies . includes ( uppercaseTo ) ) {
57- return res
58- . status ( 400 )
59- . json ( { message : "The currency to is not supported" } ) ;
60- }
61-
62- const data : Conversion = {
63- from : uppercaseFrom ,
64- to : uppercaseTo ,
65- amount,
66- } ;
67-
68- const conversion : Conversion = await currencyService . getConversion (
69- data
35+ const supportedCurrencies = await currencyService . getSupportedCurrencies ( ) ;
36+
37+ if ( ! supportedCurrencies . includes ( uppercaseFrom ) ) {
38+ throw new BadRequestError (
39+ `The currency ${ uppercaseFrom } is not supported`
7040 ) ;
41+ }
7142
72- return res . status ( 200 ) . json ( conversion ) ;
73- } catch ( error : any ) {
74- return res . status ( 500 ) . json ( { message : error . message } ) ;
43+ if ( ! supportedCurrencies . includes ( uppercaseTo ) ) {
44+ throw new BadRequestError (
45+ `The currency ${ uppercaseTo } is not supported`
46+ ) ;
7547 }
48+
49+ const data : Conversion = {
50+ from : uppercaseFrom ,
51+ to : uppercaseTo ,
52+ amount,
53+ } ;
54+
55+ const conversion : Conversion = await currencyService . getConversion ( data ) ;
56+
57+ return res . status ( 200 ) . json ( conversion ) ;
7658} ;
7759
7860const createCurrency = async ( req : Request , res : Response ) => {
@@ -84,13 +66,8 @@ const createCurrency = async (req: Request, res: Response) => {
8466 value,
8567 } ;
8668
87- try {
88- await currencyService . createCurrency ( currency ) ;
89-
90- return res . status ( 201 ) . json ( currency ) ;
91- } catch ( error : any ) {
92- return res . status ( 500 ) . json ( { message : error . message } ) ;
93- }
69+ await currencyService . createCurrency ( currency ) ;
70+ return res . status ( 201 ) . json ( currency ) ;
9471} ;
9572
9673const updateCurrency = async ( req : Request , res : Response ) => {
@@ -103,25 +80,15 @@ const updateCurrency = async (req: Request, res: Response) => {
10380 value,
10481 } ;
10582
106- try {
107- await currencyService . updateCurrency ( codeParams , currency ) ;
108-
109- return res . status ( 204 ) . send ( ) ;
110- } catch ( error : any ) {
111- return res . status ( 500 ) . json ( { message : error . message } ) ;
112- }
83+ await currencyService . updateCurrency ( codeParams , currency ) ;
84+ return res . status ( 204 ) . send ( ) ;
11385} ;
11486
11587const deleteCurrency = async ( req : Request , res : Response ) => {
11688 const code = ( req . params . code as string ) . toUpperCase ( ) ;
11789
118- try {
119- await currencyService . deleteCurrency ( code ) ;
120-
121- return res . status ( 204 ) . send ( ) ;
122- } catch ( error : any ) {
123- return res . status ( 500 ) . json ( { message : error . message } ) ;
124- }
90+ await currencyService . deleteCurrency ( code ) ;
91+ return res . status ( 204 ) . send ( ) ;
12592} ;
12693
12794export const currencyController = {
0 commit comments