11using System . Collections . Concurrent ;
22using bet_blocker . Business . Interfaces ;
3+ using Infrastructure . Repositories . Interfaces ;
4+ using MongoDB . Bson ;
5+ using System . Collections . Concurrent ;
36using Infrastructure . Services . Interfaces ;
47using System . Text . Json ;
58using static bet_blocker . DTOs . ResponseHostDto ;
@@ -11,58 +14,42 @@ namespace bet_blocker.Business
1114 public class BetBusiness : IBetBusiness
1215 {
1316 private readonly ICaller _caller ;
17+ private readonly IMongoDbRepository _mongoDbRepository ;
1418 private readonly string _endpoint ;
15- private readonly string _storagePath ;
1619 private static readonly object LockObject = new object ( ) ;
1720
18- public BetBusiness ( ICaller caller , IConfiguration configuration )
21+ public BetBusiness ( ICaller caller , IMongoDbRepository mongoDbRepository , IConfiguration configuration )
1922 {
2023 _caller = caller ;
24+ _mongoDbRepository = mongoDbRepository ;
2125 _endpoint = configuration . GetSection ( "blockList" ) . Value ;
22- _storagePath = configuration . GetSection ( "StoragePath" ) . Value ;
23-
24- if ( ! Directory . Exists ( _storagePath ) )
25- {
26- Directory . CreateDirectory ( _storagePath ) ;
27- }
2826 }
2927
3028 public void StartResolutionProcess ( CancellationToken cancellationToken )
3129 {
3230 lock ( LockObject )
3331 {
3432 var currentDate = DateTime . UtcNow . ToString ( "dd-MM-yyyy" ) ;
35- var filePath = Path . Combine ( _storagePath , $ "{ currentDate } .json") ;
36-
37- if ( File . Exists ( filePath ) )
33+ var existingDocuments = _mongoDbRepository . GetAllDocumentsAsync ( ) . Result ;
34+ if ( existingDocuments . Any ( doc => doc [ "Date" ] == currentDate ) )
3835 {
3936 throw new InvalidOperationException ( "A resolução de DNS já foi gerada para hoje. Tente novamente amanhã." ) ;
4037 }
4138 }
42-
4339 _ = Task . Run ( async ( ) =>
4440 {
4541 try
4642 {
4743 var resolvedHosts = await GetList ( cancellationToken ) ;
4844 var currentDate = DateTime . UtcNow . ToString ( "dd-MM-yyyy" ) ;
49-
50- var filePath = Path . Combine ( _storagePath , $ "{ currentDate } .json") ;
51-
52- var json = JsonSerializer . Serialize ( new
45+ var document = new BsonDocument
5346 {
54- Date = currentDate ,
55- ResolvedHosts = resolvedHosts
56- } , new JsonSerializerOptions { WriteIndented = true } ) ;
47+ { "Date" , currentDate } ,
48+ { "ResolvedHosts" , new BsonArray ( resolvedHosts . Select ( host => host . ToBsonDocument ( ) ) ) }
5749
58- File . WriteAllText ( filePath , json ) ;
59-
60- if ( ! Directory . Exists ( _storagePath ) )
61- {
62- Directory . CreateDirectory ( _storagePath ) ;
63- }
50+ } ;
6451
65- File . WriteAllText ( filePath , json ) ;
52+ await _mongoDbRepository . InsertDocumentAsync ( document ) ;
6653 }
6754 catch ( Exception ex )
6855 {
@@ -152,13 +139,12 @@ private static async Task<ResponseHostsDTO> ResolveDomainInfo(string domain)
152139 public object GetResolutionStatus ( )
153140 {
154141 var currentDate = DateTime . UtcNow . ToString ( "dd-MM-yyyy" ) ;
155- var filePath = Path . Combine ( _storagePath , $ " { currentDate } .json" ) ;
142+ var documents = _mongoDbRepository . GetAllDocumentsAsync ( ) . Result ;
156143
157- if ( File . Exists ( filePath ) )
144+ var currentDocument = documents . FirstOrDefault ( doc => doc [ "Date" ] == currentDate ) ;
145+ if ( currentDocument != null )
158146 {
159- var json = File . ReadAllText ( filePath ) ;
160- var result = JsonSerializer . Deserialize < object > ( json ) ;
161- return result ;
147+ return currentDocument ;
162148 }
163149
164150 return "Nenhum processo iniciado ou resolução não encontrada para hoje." ;
0 commit comments