|
6 | 6 | using LibraryManagement.Api.Brokers.Loggings; |
7 | 7 | using LibraryManagement.Api.Brokers.Storages; |
8 | 8 | using LibraryManagement.Api.Models.Foundations.Readers; |
9 | | -using LibraryManagement.Api.Models.Foundations.Readers.Exceptions; |
10 | | -using Microsoft.Data.SqlClient; |
11 | 9 |
|
12 | 10 | namespace LibraryManagement.Api.Services.Foundations.Readers |
13 | 11 | { |
@@ -35,61 +33,17 @@ public ValueTask<Reader> AddReaderAsync(Reader reader) => |
35 | 33 | public IQueryable<Reader> RetrieveAllReaders() => |
36 | 34 | TryCatch(() => this.storageBroker.SelectAllReaders()); |
37 | 35 |
|
38 | | - public async ValueTask<Reader> RetrieveReaderByIdAsync(Guid readerId) |
| 36 | + public ValueTask<Reader> RetrieveReaderByIdAsync(Guid readerId) => |
| 37 | + TryCatch(async () => |
39 | 38 | { |
40 | | - try |
41 | | - { |
42 | | - ValidateReaderId(readerId); |
43 | | - |
44 | | - Reader maybeReader = |
45 | | - await this.storageBroker.SelectReaderByIdAsync(readerId); |
46 | | - |
47 | | - ValidateStorageReader(maybeReader, readerId); |
48 | | - |
49 | | - return maybeReader; |
50 | | - } |
51 | | - catch (InvalidReaderException invalidReaderException) |
52 | | - { |
53 | | - var readerValidationException = |
54 | | - new ReaderValidationException(invalidReaderException); |
55 | | - |
56 | | - this.loggingBroker.LogError(readerValidationException); |
57 | | - |
58 | | - throw readerValidationException; |
59 | | - } |
60 | | - catch (NotFoundReaderException notFoundReaderException) |
61 | | - { |
62 | | - var readerValidationException = |
63 | | - new ReaderValidationException(notFoundReaderException); |
64 | | - |
65 | | - this.loggingBroker.LogError(readerValidationException); |
| 39 | + ValidateReaderId(readerId); |
66 | 40 |
|
67 | | - throw readerValidationException; |
68 | | - } |
69 | | - catch (SqlException sqlException) |
70 | | - { |
71 | | - var failedReaderStorageException = |
72 | | - new FailedReaderStorageException(sqlException); |
| 41 | + Reader maybeReader = |
| 42 | + await this.storageBroker.SelectReaderByIdAsync(readerId); |
73 | 43 |
|
74 | | - var readerDependencyException = |
75 | | - new ReaderDependencyException(failedReaderStorageException); |
| 44 | + ValidateStorageReader(maybeReader, readerId); |
76 | 45 |
|
77 | | - this.loggingBroker.LogCritical(readerDependencyException); |
78 | | - |
79 | | - throw readerDependencyException; |
80 | | - } |
81 | | - catch (Exception exception) |
82 | | - { |
83 | | - var failedReaderServiceException = |
84 | | - new FailedReaderServiceException(exception); |
85 | | - |
86 | | - var readerServiceException = |
87 | | - new ReaderServiceException(failedReaderServiceException); |
88 | | - |
89 | | - this.loggingBroker.LogError(readerServiceException); |
90 | | - |
91 | | - throw readerServiceException; |
92 | | - } |
93 | | - } |
| 46 | + return maybeReader; |
| 47 | + }); |
94 | 48 | } |
95 | 49 | } |
0 commit comments