11using AutoMapper ;
2+ using FinancialHub . Common . Extensions ;
23using FinancialHub . Core . Domain . DTOS . Balances ;
34using FinancialHub . Core . Domain . Interfaces . Validators ;
5+ using Microsoft . Extensions . Logging ;
46
57namespace FinancialHub . Core . Application . Services
68{
@@ -9,98 +11,144 @@ public class BalancesService : IBalancesService
911 private readonly IBalancesProvider balancesProvider ;
1012 private readonly IBalancesValidator balancesValidator ;
1113 private readonly IAccountsValidator accountsValidator ;
14+ private readonly ILogger < BalancesService > logger ;
1215 private readonly IMapper mapper ;
1316
1417 public BalancesService (
1518 IBalancesProvider balancesProvider ,
1619 IBalancesValidator balancesValidator , IAccountsValidator accountsValidator ,
17- IMapper mapper
20+ IMapper mapper , ILogger < BalancesService > logger
1821 )
1922 {
2023 this . balancesProvider = balancesProvider ;
2124 this . balancesValidator = balancesValidator ;
2225 this . accountsValidator = accountsValidator ;
2326 this . mapper = mapper ;
27+ this . logger = logger ;
2428 }
2529
2630 public async Task < ServiceResult < BalanceDto > > CreateAsync ( CreateBalanceDto balance )
2731 {
28- var validationResult = await this . balancesValidator . ValidateAsync ( balance ) ;
29- if ( validationResult . HasError )
30- return validationResult . Error ;
32+ this . logger . LogInformation ( "Creating balance {name} in account {id}" , balance . Name , balance . AccountId ) ;
33+ this . logger . LogTrace ( "Balance data : {balance}" , balance . ToJson ( ) ) ;
3134
32- validationResult = await this . accountsValidator . ExistsAsync ( balance . AccountId ) ;
35+ var validationResult = await this . balancesValidator . ValidateAsync ( balance ) ;
3336 if ( validationResult . HasError )
37+ {
38+ this . logger . LogTrace ( "Balance creation Validation result : {validationResult}" , validationResult . ToJson ( ) ) ;
39+ this . logger . LogInformation ( "Failed creating account {name}" , balance . Name ) ;
3440 return validationResult . Error ;
41+ }
3542
3643 var balanceModel = this . mapper . Map < BalanceModel > ( balance ) ;
3744
3845 var createdBalance = await this . balancesProvider . CreateAsync ( balanceModel ) ;
3946
40- return this . mapper . Map < BalanceDto > ( createdBalance ) ;
47+ var result = this . mapper . Map < BalanceDto > ( createdBalance ) ;
48+ this . logger . LogTrace ( "Balance creation result : {result}" , result . ToJson ( ) ) ;
49+ this . logger . LogInformation ( "Balance {name} Sucessfully created in account {id}" , result . Name , result . Account ? . Id ) ;
50+ return result ;
4151 }
4252
4353 public async Task < ServiceResult < int > > DeleteAsync ( Guid id )
4454 {
45- return await this . balancesProvider . DeleteAsync ( id ) ;
55+ this . logger . LogInformation ( "Removing balance {id}" , id ) ;
56+ var amount = await this . balancesProvider . DeleteAsync ( id ) ;
57+ this . logger . LogInformation ( "Balance {id} {removed}" , id , amount > 0 ? "removed" : "not removed" ) ;
58+ return amount ;
4659 }
4760
4861 public async Task < ServiceResult < BalanceDto > > GetByIdAsync ( Guid id )
4962 {
63+ this . logger . LogInformation ( "Getting balance {id}" , id ) ;
5064 var validationResult = await this . balancesValidator . ExistsAsync ( id ) ;
5165 if ( validationResult . HasError )
5266 {
67+ this . logger . LogTrace ( "Balance get by id result : {validationResult}" , validationResult ) ;
68+ this . logger . LogInformation ( "Failed getting balance {id}" , id ) ;
5369 return validationResult . Error ;
5470 }
5571
56- var balance = await this . balancesProvider . GetByIdAsync ( id ) ;
72+ var existingBalance = await this . balancesProvider . GetByIdAsync ( id ) ;
73+
74+ var balance = this . mapper . Map < BalanceDto > ( existingBalance ) ;
5775
58- return this . mapper . Map < BalanceDto > ( balance ) ;
76+ this . logger . LogTrace ( "Balance result {balance}" , balance ) ;
77+ this . logger . LogInformation ( "Balance {id} found" , id ) ;
78+ return balance ;
5979 }
6080
6181 public async Task < ServiceResult < ICollection < BalanceDto > > > GetAllByAccountAsync ( Guid accountId )
6282 {
83+ this . logger . LogInformation ( "Getting balances from account {accountId}" , accountId ) ;
6384 var validationResult = await this . accountsValidator . ExistsAsync ( accountId ) ;
6485 if ( validationResult . HasError )
6586 {
87+ this . logger . LogTrace ( "Balances get by account result : {validationResult}" , validationResult . ToJson ( ) ) ;
88+ this . logger . LogInformation ( "Account {AccountId} not found" , accountId ) ;
6689 return validationResult . Error ;
6790 }
6891
6992 var balances = await this . balancesProvider . GetAllByAccountAsync ( accountId ) ;
7093
71- return this . mapper . Map < ICollection < BalanceDto > > ( balances ) . ToArray ( ) ;
94+ var result = this . mapper . Map < ICollection < BalanceDto > > ( balances ) . ToArray ( ) ;
95+
96+ this . logger . LogTrace ( "Balance result : {balances}" , balances . ToJson ( ) ) ;
97+ this . logger . LogInformation (
98+ "{amount} returned from account {accountId}" ,
99+ result . Length > 0 ? $ "{ result . Length } balances" : "no balances" ,
100+ accountId
101+ ) ;
102+
103+ return result ;
72104 }
73105
74106 public async Task < ServiceResult < BalanceDto > > UpdateAsync ( Guid id , UpdateBalanceDto balance )
75107 {
108+ this . logger . LogInformation ( "Updating balance {name} in account {id}" , balance . Name , balance . AccountId ) ;
109+ this . logger . LogTrace ( "Balance data : {balance}" , balance . ToJson ( ) ) ;
76110 var validationResult = await this . balancesValidator . ValidateAsync ( balance ) ;
77111 if ( validationResult . HasError )
112+ {
113+ this . logger . LogTrace ( "Balance update validation result : {validationResult}" , validationResult . ToJson ( ) ) ;
114+ this . logger . LogInformation ( "Failed updating balance {id}" , id ) ;
78115 return validationResult . Error ;
116+ }
79117
80118 validationResult = await this . balancesValidator . ExistsAsync ( id ) ;
81119 if ( validationResult . HasError )
120+ {
121+ this . logger . LogTrace ( "Balance update validation result : {validationResult}" , validationResult . ToJson ( ) ) ;
122+ this . logger . LogInformation ( "Balance {id} not found" , id ) ;
82123 return validationResult . Error ;
83-
84- validationResult = await this . accountsValidator . ExistsAsync ( balance . AccountId ) ;
85- if ( validationResult . HasError )
86- return validationResult . Error ;
124+ }
87125
88126 var balanceModel = this . mapper . Map < BalanceModel > ( balance ) ;
89127
90128 var updatedBalance = await this . balancesProvider . UpdateAsync ( id , balanceModel ) ;
91129
92- return this . mapper . Map < BalanceDto > ( updatedBalance ) ;
130+ var result = this . mapper . Map < BalanceDto > ( updatedBalance ) ;
131+ this . logger . LogTrace ( "Balance update result : {result}" , result . ToJson ( ) ) ;
132+ this . logger . LogInformation ( "Balance {name} Sucessfully created" , result . Name ) ;
133+ return result ;
93134 }
94135
95136 public async Task < ServiceResult < BalanceModel > > UpdateAmountAsync ( Guid id , decimal newAmount )
96137 {
97- var oldBalance = await this . balancesValidator . ExistsAsync ( id ) ;
98- if ( oldBalance . HasError )
138+ this . logger . LogInformation ( "Getting balance {id}" , id ) ;
139+ var validationResult = await this . balancesValidator . ExistsAsync ( id ) ;
140+ if ( validationResult . HasError )
99141 {
100- return oldBalance . Error ;
142+ this . logger . LogTrace ( "Balance get by id result : {validationResult}" , validationResult . ToJson ( ) ) ;
143+ this . logger . LogInformation ( "Failed getting balance {id}" , id ) ;
144+ return validationResult . Error ;
101145 }
102146
103- return await balancesProvider . UpdateAmountAsync ( id , newAmount ) ;
147+ var updatedBalance = await balancesProvider . UpdateAmountAsync ( id , newAmount ) ;
148+
149+ this . logger . LogTrace ( "Update Balance amount result : {updatedBalance}" , updatedBalance . ToJson ( ) ) ;
150+ this . logger . LogInformation ( "Update Balance amount {balanceId} in account {accountId}" , updatedBalance . Id , updatedBalance . AccountId ) ;
151+ return updatedBalance ;
104152 }
105153 }
106154}
0 commit comments