1
1
package uz .pdp .springsecurityatm .service ;
2
2
3
3
import org .springframework .beans .factory .annotation .Autowired ;
4
- import org .springframework .http .HttpStatus ;
5
4
import org .springframework .http .ResponseEntity ;
5
+ import org .springframework .security .core .context .SecurityContextHolder ;
6
6
import org .springframework .stereotype .Service ;
7
- import uz .pdp .springsecurityatm .entity .Bank ;
7
+ import uz .pdp .springsecurityatm .entity .*;
8
+ import uz .pdp .springsecurityatm .entity .enums .CardName ;
8
9
import uz .pdp .springsecurityatm .entity .enums .USD ;
9
10
import uz .pdp .springsecurityatm .entity .enums .UZS ;
10
- import uz .pdp .springsecurityatm .payload .ATMDTO ;
11
- import uz .pdp .springsecurityatm .repository . ATMRepository ;
12
- import uz .pdp .springsecurityatm .repository .BankRepository ;
11
+ import uz .pdp .springsecurityatm .payload .AtmDTO ;
12
+ import uz .pdp .springsecurityatm .payload . TakeOutDto ;
13
+ import uz .pdp .springsecurityatm .repository .* ;
13
14
15
+ import java .util .HashSet ;
16
+ import java .util .List ;
14
17
import java .util .Optional ;
18
+ import java .util .Set ;
15
19
16
- import static org .springframework .http .HttpStatus .NOT_FOUND ;
20
+ import static org .springframework .http .HttpStatus .* ;
17
21
import static org .springframework .http .ResponseEntity .*;
18
22
19
23
@ Service
20
24
public class AtmService {
21
25
22
26
private final ATMRepository repository ;
23
27
private final BankRepository bankRepository ;
28
+ private final CardTypeRepository cardTypeRepository ;
29
+ private final UserRepository userRepository ;
30
+ private final AddressRepository addressRepository ;
31
+ private final SummaRepository summaRepository ;
32
+ private final DollarRepository dollarRepository ;
24
33
25
34
@ Autowired
26
35
public AtmService (ATMRepository repository ,
27
- BankRepository bankRepository ) {
36
+ BankRepository bankRepository ,
37
+ CardTypeRepository cardTypeRepository ,
38
+ UserRepository userRepository , AddressRepository addressRepository , SummaRepository summaRepository , DollarRepository dollarRepository ) {
28
39
this .repository = repository ;
29
40
this .bankRepository = bankRepository ;
41
+ this .cardTypeRepository = cardTypeRepository ;
42
+ this .userRepository = userRepository ;
43
+ this .addressRepository = addressRepository ;
44
+ this .summaRepository = summaRepository ;
45
+ this .dollarRepository = dollarRepository ;
30
46
}
31
47
32
48
public ResponseEntity <?> getAllATMs () {
@@ -43,18 +59,170 @@ public ResponseEntity<?> getSumma(String type) {
43
59
else return badRequest ().body ("Wrong type" );
44
60
}
45
61
46
- public ResponseEntity <?> saveATM (ATMDTO dto ) {
62
+ public ResponseEntity <?> saveATM (AtmDTO dto ) {
47
63
Optional <Bank > optionalBank = bankRepository .findById (dto .getBankId ());
48
- if (optionalBank .isPresent ()) return status (NOT_FOUND ).body ("Bank not found" );
49
- System .out .println (dto );
64
+ if (optionalBank .isEmpty ()) return status (NOT_FOUND ).body ("Bank not found" );
65
+ Optional <User > optionalUser = userRepository .findById (dto .getUserId ());
66
+ if (optionalUser .isEmpty ()) return status (NOT_FOUND ).body ("User not found" );
67
+ Address address = addressRepository .save (new Address (null , dto .getCity (), dto .getStreet (), dto .getDistrict ()));
68
+ try {
69
+ List <Summa > summas = summaRepository .saveAll (dto .getSums ());
70
+ List <Dollar > dollars = dollarRepository .saveAll (dto .getDollars ());
71
+ ATM atm = new ATM (
72
+ checkCardType (dto .getCardTypes ()),
73
+ optionalBank .get (),
74
+ address ,
75
+ new HashSet <>(summas ),
76
+ new HashSet <>(dollars ),
77
+ optionalUser .get (),
78
+ getBalance (dto .getSums ()),
79
+ dto .getCommission (),
80
+ dto .getTakeOut ()
81
+ );
82
+ repository .save (atm );
83
+ } catch (Exception e ) {
84
+ System .out .println (e .getLocalizedMessage ());
85
+ addressRepository .delete (address );
86
+ return badRequest ().body ("ATM not saved" );
87
+ }
50
88
return ok ("atm successfully saved" );
51
89
}
52
90
53
91
public ResponseEntity <?> deleteAtm (Long id ) {
54
- return null ;
92
+ return repository .findById (id )
93
+ .map (atm -> {
94
+ try {
95
+ addressRepository .delete (atm .getAddress ());
96
+ repository .delete (atm );
97
+ return status (NO_CONTENT ).body ("atm successfully deleted" );
98
+ } catch (Exception e ) {
99
+ return badRequest ().body ("ATM not deleted" );
100
+ }
101
+ }).orElseGet (() -> status (NOT_FOUND ).body ("ATM not found" ));
55
102
}
56
103
57
- public ResponseEntity <?> editAtm (Long id , ATMDTO atmDTO ) {
58
- return null ;
104
+ public ResponseEntity <?> editAtm (Long id , AtmDTO atmDTO ) {
105
+ Optional <ATM > optionalATM = repository .findById (id );
106
+ if (optionalATM .isEmpty ()) return status (NOT_FOUND ).body ("ATM not found" );
107
+ Optional <Bank > optionalBank = bankRepository .findById (atmDTO .getBankId ());
108
+ if (optionalBank .isEmpty ()) return status (NOT_FOUND ).body ("Bank not found" );
109
+ ATM atm = optionalATM .get ();
110
+ Address address = atm .getAddress ();
111
+ address .setCity (atmDTO .getCity ());
112
+ address .setDistrict (atmDTO .getDistrict ());
113
+ address .setStreet (atmDTO .getStreet ());
114
+ try {
115
+ addressRepository .save (address );
116
+ } catch (Exception e ) {
117
+ return status (BAD_REQUEST ).body ("Address not saved" );
118
+ }
119
+ atm .setDollars (atmDTO .getDollars ());
120
+ Optional <User > optionalUser = userRepository .findById (atmDTO .getUserId ());
121
+ optionalUser .ifPresent (atm ::setUser );
122
+ atm .setCardType (checkCardType (atmDTO .getCardTypes ()));
123
+ atm .setCommission (atm .getCommission ());
124
+ atm .setMaxWithdraw (atmDTO .getTakeOut ());
125
+ atm .setSummas (atmDTO .getSums ());
126
+ atm .setBalance (getBalance (atmDTO .getSums ()));
127
+ try {
128
+ repository .save (atm );
129
+ return status (CREATED ).body ("ATM successfully edited" );
130
+ } catch (Exception e ) {
131
+ return status (BAD_REQUEST ).body ("ATM failed to saved" );
132
+ }
59
133
}
134
+
135
+ // Actions
136
+ public Set <CardType > checkCardType (Set <Integer > cardTypes ) {
137
+ return new HashSet <>(cardTypeRepository .findAllById (cardTypes ));
138
+ }
139
+
140
+ public Double getBalance (Set <Summa > summas ) {
141
+ double balance = 0D ;
142
+ for (Summa summa : summas ) {
143
+ balance += Double .parseDouble (summa .getUzs ().name ().substring (1 )) * summa .getCount ();
144
+ }
145
+ return balance ;
146
+ }
147
+
148
+ public ResponseEntity <?> takeOut (Long id , TakeOutDto dto ) {
149
+ if (dto .getMoneyType ().equalsIgnoreCase ("uzs" ) || dto .getMoneyType ().equalsIgnoreCase ("usd" )) {
150
+ Optional <ATM > optionalATM = repository .findById (id );
151
+ if (optionalATM .isEmpty ()) return status (NOT_FOUND ).body ("ATM not found" );
152
+ ATM atm = optionalATM .get ();
153
+ Card card = (Card ) SecurityContextHolder .getContext ().getAuthentication ().getPrincipal ();
154
+ if (dto .getMoneyType ().equals ("uzs" ) && (card .getType ().getType ().equals (CardName .HUMO ) || card .getType ().getType ().equals (CardName .VISA ))) {
155
+ if (card .getBalance () >= dto .getMoney () + (dto .getMoney () * atm .getCommission () / 100 )) {
156
+ return withdrawMoneyForSum (dto .getMoney (), atm , card );
157
+ }
158
+ return badRequest ().body ("Not enough money" );
159
+ } else if (dto .getMoneyType ().equals ("usd" ) && (card .getType ().getType ().equals (CardName .VISA ))) {
160
+ //
161
+ }
162
+ return null ;
163
+ }
164
+ return status (BAD_REQUEST ).body ("Wrong money type" );
165
+ }
166
+
167
+ public ResponseEntity <?> withdrawMoneyForSum (Double money , ATM atm , Card card ) {
168
+ int _hundred = 0 , _fifty = 0 , _five = 0 , _ten = 0 , _one = 0 ;
169
+ Set <Summa > summas = atm .getSummas ();
170
+ for (Summa summa : summas ) {
171
+ if (money >= 100_000 && summa .getUzs ().equals (UZS ._100000 ) && ((int ) (money / 100_000 )) >= summa .getCount ()) {
172
+ _hundred = (int ) (money / 100_000 );
173
+ money = money - _hundred * 100_000 ;
174
+ }
175
+ if (money >= 50_000 && summa .getUzs ().equals (UZS ._50000 ) && ((int ) (money / 50_000 )) >= summa .getCount ()) {
176
+ _fifty = (int ) (money / 50_000 );
177
+ money = money - _fifty * 50_000 ;
178
+ }
179
+ if (money >= 10_000 && summa .getUzs ().equals (UZS ._10000 ) && ((int ) (money / 10_000 )) >= summa .getCount ()) {
180
+ _ten = (int ) (money / 10_000 );
181
+ money = money - _ten * 10_000 ;
182
+ }
183
+ if (money >= 5_000 && summa .getUzs ().equals (UZS ._5000 ) && ((int ) (money / 5_000 )) >= summa .getCount ()) {
184
+ _five = (int ) (money / 5_000 );
185
+ money = money - _five * 5_000 ;
186
+ }
187
+ if (money >= 1_000 && summa .getUzs ().equals (UZS ._1000 ) && ((int ) (money / 1_000 )) >= summa .getCount ()) {
188
+ _five = (int ) (money / 1_000 );
189
+ money = money - _five * 1_000 ;
190
+ }
191
+ }
192
+ System .out .println (money );
193
+ return ok (money );
194
+ }
195
+
196
+
197
+ // public static void main(String[] args) {
198
+ // Scanner scanner = new Scanner(System.in);
199
+ // System.out.print("Enter amount: ");
200
+ // double money = scanner.nextDouble();
201
+ // int _hundred = 0, _fifty = 0, _five = 0, _ten = 0, _one = 0;
202
+ // if (money >= 100_000) {
203
+ // _hundred = (int) money / 100_000;
204
+ // money = money - _hundred * 100_000;
205
+ // }
206
+ // if (money >= 50_000) {
207
+ // _fifty = (int) money / 50_000;
208
+ // money = money - _fifty * 50_000;
209
+ // }
210
+ // if (money >= 10_000) {
211
+ // _ten = (int) money / 10_000;
212
+ // money = money - _ten * 10_000;
213
+ // }
214
+ // if (money >= 5_000) {
215
+ // _five = (int) money / 5_000;
216
+ // money = money - _five * 5_000;
217
+ // }
218
+ // if (money >= 1_000) {
219
+ // _five = (int) money / 1_000;
220
+ // money = money - _five * 1_000;
221
+ // }
222
+ // System.out.println("yuz ming so'm donasi: " + _hundred);
223
+ // System.out.println("ellik ming so'm donasi: " + _fifty);
224
+ // System.out.println("o'n ming so'm donasi: " + _ten);
225
+ // System.out.println("besh ming so'm donasi: " + _five);
226
+ // System.out.println("ming so'm donasi: " + _one);
227
+ // }
60
228
}
0 commit comments