| | | 1 | | using KT.ModularMonolith.Shared.Commons.Utils; |
| | | 2 | | using KT.Modules.Report.Core.Domain; |
| | | 3 | | using KT.Modules.Report.Core.Domain.Ports; |
| | | 4 | | using KT.Modules.Report.Presentation.Contracts; |
| | | 5 | | using KT.Modules.Report.Presentation.Dto; |
| | | 6 | | using KT.Modules.Report.Presentation.Dto.WebSocket; |
| | | 7 | | using Microsoft.Extensions.Configuration; |
| | | 8 | | using System.Text.Json; |
| | | 9 | | |
| | | 10 | | namespace KT.Modules.Report.Core.Application |
| | | 11 | | { |
| | 11 | 12 | | internal class ReportsService (IConfiguration configuration, IReportsRepository reportsRepository, IReportNotifier r |
| | | 13 | | { |
| | 11 | 14 | | private JsonSerializerOptions options = new JsonSerializerOptions |
| | 11 | 15 | | { |
| | 11 | 16 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 11 | 17 | | }; |
| | | 18 | | |
| | | 19 | | public async Task<ResponseGetReportDto> SaveReport(RequestCreateReportDto requestCreateReport) |
| | 1 | 20 | | { |
| | 1 | 21 | | Domain.Report report = Domain.Report.FromCreateDto(requestCreateReport); |
| | 1 | 22 | | await reportsRepository.SaveAsync(report); |
| | 1 | 23 | | var reportNotification = new ReportNotification { |
| | 1 | 24 | | Type = ReportNotificaionType.CREATE, |
| | 1 | 25 | | Report = ReportDataAdd.fromDomain(report), |
| | 1 | 26 | | }; |
| | 1 | 27 | | var rerportJson = JsonSerializer.Serialize(reportNotification, options); |
| | 1 | 28 | | await reportNotifier.NotifyAsync(rerportJson); |
| | 1 | 29 | | return _ResponseGetFromDomain(report); |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task DeleteReport(string reportId) |
| | 1 | 33 | | { |
| | 1 | 34 | | await GetReport(reportId); |
| | 1 | 35 | | await reportsRepository.DeleteAsync(reportId); |
| | | 36 | | |
| | 1 | 37 | | var reportNotification = ReportNotification.DeleteReportNotification(reportId); |
| | 1 | 38 | | var rerportJson = JsonSerializer.Serialize(reportNotification, options); |
| | 1 | 39 | | await reportNotifier.NotifyAsync(rerportJson); |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<ResponseGetReportDto> GetReport(string reportId) |
| | 4 | 43 | | { |
| | 4 | 44 | | var report = await reportsRepository.GetReportAsync(reportId); |
| | | 45 | | |
| | 4 | 46 | | if (report == null) |
| | 1 | 47 | | { |
| | 1 | 48 | | throw new ReportDoesntExistsException(reportId); |
| | | 49 | | } |
| | 3 | 50 | | var reportDto = _ResponseGetFromDomain(report); |
| | 3 | 51 | | return reportDto; |
| | 3 | 52 | | } |
| | | 53 | | |
| | | 54 | | |
| | | 55 | | public async Task<ResponseGetReportDto> UpdateReport(RequestUpdateReportDto requestUpdateReport) |
| | 1 | 56 | | { |
| | 1 | 57 | | await GetReport(requestUpdateReport.Id); |
| | 1 | 58 | | var location = new Domain.Location |
| | 1 | 59 | | { |
| | 1 | 60 | | Latitude = requestUpdateReport.Location.Latitude, |
| | 1 | 61 | | Longitude = requestUpdateReport.Location.Longitude, |
| | 1 | 62 | | Locality = requestUpdateReport.Location.Locality, |
| | 1 | 63 | | Neighbourhood = requestUpdateReport.Location.Neighbourhood, |
| | 1 | 64 | | |
| | 1 | 65 | | }; |
| | 1 | 66 | | var report = new Domain.Report |
| | 1 | 67 | | { |
| | 1 | 68 | | Id = requestUpdateReport.Id, |
| | 1 | 69 | | UserId = requestUpdateReport.UserId, |
| | 1 | 70 | | Location = location, |
| | 1 | 71 | | Type = requestUpdateReport.Type, |
| | 1 | 72 | | Description = requestUpdateReport.Description, |
| | 1 | 73 | | Title = requestUpdateReport.Title, |
| | 1 | 74 | | CreatedAt = requestUpdateReport.CreatedAt, |
| | 1 | 75 | | UpdatedAt = DateTime.UtcNow |
| | 1 | 76 | | }; |
| | | 77 | | |
| | 1 | 78 | | await reportsRepository.UpdateReportAsync(report); |
| | | 79 | | |
| | 1 | 80 | | return _ResponseGetFromDomain(report); |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<List<ResponseGetReportDto>> GetReportsByUserId(string userId) |
| | 2 | 84 | | { |
| | 2 | 85 | | var reports = await reportsRepository.GetReportsByUserId(userId); |
| | 2 | 86 | | var reportsDto = new List<ResponseGetReportDto>(); |
| | 3 | 87 | | if (reports == null || reports.Count == 0) { |
| | 1 | 88 | | throw new ReportWithUserIdDoesntExistsException(userId); |
| | | 89 | | } |
| | | 90 | | |
| | 9 | 91 | | foreach (var report in reports) { |
| | 2 | 92 | | reportsDto.Add(_ResponseGetFromDomain(report)); |
| | 2 | 93 | | } |
| | 1 | 94 | | return reportsDto; |
| | 1 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<ResponseGetReportsDto> GetReports() |
| | 2 | 98 | | { |
| | 2 | 99 | | var reports = await reportsRepository.GetReportsAsync(); |
| | 2 | 100 | | var reportsDto = new ResponseGetReportsDto(); |
| | 2 | 101 | | if (reports == null || reports.Count == 0) |
| | 1 | 102 | | { |
| | 1 | 103 | | throw new ReportsDoesntExistsException(); |
| | | 104 | | } |
| | | 105 | | |
| | 5 | 106 | | foreach (var report in reports) |
| | 1 | 107 | | { |
| | 1 | 108 | | reportsDto.reports.Add(report.toReportDto()); |
| | 1 | 109 | | } |
| | 1 | 110 | | return reportsDto; |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | public async Task<ResponseGetReportsDto> GetReportsOpenData(string? locality, string? type) |
| | 2 | 114 | | { |
| | 2 | 115 | | var reports = await reportsRepository.GetReportsOpenData(locality, type); |
| | | 116 | | |
| | 2 | 117 | | var reportsDto = new ResponseGetReportsDto(); |
| | 2 | 118 | | if (reports == null || reports.Count == 0) |
| | 1 | 119 | | { |
| | 1 | 120 | | throw new ReportsDoesntExistsException(); |
| | | 121 | | } |
| | | 122 | | |
| | 5 | 123 | | foreach (var report in reports) |
| | 1 | 124 | | { |
| | 1 | 125 | | reportsDto.reports.Add(report.toReportDto()); |
| | 1 | 126 | | } |
| | 1 | 127 | | return reportsDto; |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | public static ResponseGetReportDto _ResponseGetFromDomain(Domain.Report report) |
| | 7 | 131 | | { |
| | 7 | 132 | | var location = new ResponseGetLocationDto |
| | 7 | 133 | | { |
| | 7 | 134 | | Latitude = report.Location.Latitude, |
| | 7 | 135 | | Longitude = report.Location.Longitude, |
| | 7 | 136 | | Locality = report.Location.Locality, |
| | 7 | 137 | | Neighbourhood = report.Location.Neighbourhood, |
| | 7 | 138 | | }; |
| | | 139 | | |
| | 7 | 140 | | return new ResponseGetReportDto |
| | 7 | 141 | | { |
| | 7 | 142 | | Id = report.Id!, |
| | 7 | 143 | | UserId = report.UserId, |
| | 7 | 144 | | Location = location, |
| | 7 | 145 | | Type = report.Type, |
| | 7 | 146 | | Description = report.Description, |
| | 7 | 147 | | Title = report.Title, |
| | 7 | 148 | | CreatedAt = report.CreatedAt, |
| | 7 | 149 | | UpdatedAt = report.UpdatedAt |
| | 7 | 150 | | }; |
| | 7 | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |