< Summary

Information
Class: KT.Modules.Report.Core.Application.ReportsService
Assembly: KT.Modules.Report
File(s): G:\NetProjects\KeepTrack\src\Modules\KT.Modules.Report\Core\Application\ReportsService.cs
Line coverage
100%
Covered lines: 109
Uncovered lines: 0
Coverable lines: 109
Total lines: 153
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SaveReport()100%11100%
DeleteReport()100%11100%
GetReport()100%22100%
UpdateReport()100%11100%
GetReportsByUserId()83.33%66100%
GetReports()100%66100%
GetReportsOpenData()100%66100%
_ResponseGetFromDomain(...)100%11100%

File(s)

G:\NetProjects\KeepTrack\src\Modules\KT.Modules.Report\Core\Application\ReportsService.cs

#LineLine coverage
 1using KT.ModularMonolith.Shared.Commons.Utils;
 2using KT.Modules.Report.Core.Domain;
 3using KT.Modules.Report.Core.Domain.Ports;
 4using KT.Modules.Report.Presentation.Contracts;
 5using KT.Modules.Report.Presentation.Dto;
 6using KT.Modules.Report.Presentation.Dto.WebSocket;
 7using Microsoft.Extensions.Configuration;
 8using System.Text.Json;
 9
 10namespace KT.Modules.Report.Core.Application
 11{
 1112    internal class ReportsService (IConfiguration configuration, IReportsRepository reportsRepository, IReportNotifier r
 13    {
 1114        private JsonSerializerOptions options = new JsonSerializerOptions
 1115        {
 1116            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 1117        };
 18
 19        public async Task<ResponseGetReportDto> SaveReport(RequestCreateReportDto requestCreateReport)
 120        {
 121            Domain.Report report = Domain.Report.FromCreateDto(requestCreateReport);
 122            await reportsRepository.SaveAsync(report);
 123            var reportNotification = new ReportNotification {
 124                Type = ReportNotificaionType.CREATE,
 125                Report = ReportDataAdd.fromDomain(report),
 126            };
 127            var rerportJson = JsonSerializer.Serialize(reportNotification, options);
 128            await reportNotifier.NotifyAsync(rerportJson);
 129            return _ResponseGetFromDomain(report);
 130        }
 31
 32        public async Task DeleteReport(string reportId)
 133        {
 134            await GetReport(reportId);
 135            await reportsRepository.DeleteAsync(reportId);
 36
 137            var reportNotification = ReportNotification.DeleteReportNotification(reportId);
 138            var rerportJson = JsonSerializer.Serialize(reportNotification, options);
 139            await reportNotifier.NotifyAsync(rerportJson);
 140        }
 41
 42        public async Task<ResponseGetReportDto> GetReport(string reportId)
 443        {
 444            var report = await reportsRepository.GetReportAsync(reportId);
 45
 446            if (report == null)
 147            {
 148                throw new ReportDoesntExistsException(reportId);
 49            }
 350            var reportDto = _ResponseGetFromDomain(report);
 351            return reportDto;
 352        }
 53
 54
 55        public async Task<ResponseGetReportDto> UpdateReport(RequestUpdateReportDto requestUpdateReport)
 156        {
 157            await GetReport(requestUpdateReport.Id);
 158            var location = new Domain.Location
 159            {
 160                Latitude = requestUpdateReport.Location.Latitude,
 161                Longitude = requestUpdateReport.Location.Longitude,
 162                Locality = requestUpdateReport.Location.Locality,
 163                Neighbourhood = requestUpdateReport.Location.Neighbourhood,
 164
 165            };
 166            var report = new Domain.Report
 167            {
 168                Id = requestUpdateReport.Id,
 169                UserId = requestUpdateReport.UserId,
 170                Location = location,
 171                Type = requestUpdateReport.Type,
 172                Description = requestUpdateReport.Description,
 173                Title = requestUpdateReport.Title,
 174                CreatedAt = requestUpdateReport.CreatedAt,
 175                UpdatedAt = DateTime.UtcNow
 176            };
 77
 178            await reportsRepository.UpdateReportAsync(report);
 79
 180            return _ResponseGetFromDomain(report);
 181        }
 82
 83        public async Task<List<ResponseGetReportDto>> GetReportsByUserId(string userId)
 284        {
 285            var reports = await reportsRepository.GetReportsByUserId(userId);
 286            var reportsDto = new List<ResponseGetReportDto>();
 387            if (reports == null || reports.Count == 0) {
 188                throw new ReportWithUserIdDoesntExistsException(userId);
 89            }
 90
 991            foreach (var report in reports) {
 292                reportsDto.Add(_ResponseGetFromDomain(report));
 293            }
 194            return reportsDto;
 195        }
 96
 97        public async Task<ResponseGetReportsDto> GetReports()
 298        {
 299            var reports = await reportsRepository.GetReportsAsync();
 2100            var reportsDto = new ResponseGetReportsDto();
 2101            if (reports == null || reports.Count == 0)
 1102            {
 1103                throw new ReportsDoesntExistsException();
 104            }
 105
 5106            foreach (var report in reports)
 1107            {
 1108                reportsDto.reports.Add(report.toReportDto());
 1109            }
 1110            return reportsDto;
 1111        }
 112
 113        public async Task<ResponseGetReportsDto> GetReportsOpenData(string? locality, string? type)
 2114        {
 2115            var reports = await reportsRepository.GetReportsOpenData(locality, type);
 116
 2117            var reportsDto = new ResponseGetReportsDto();
 2118            if (reports == null || reports.Count == 0)
 1119            {
 1120                throw new ReportsDoesntExistsException();
 121            }
 122
 5123            foreach (var report in reports)
 1124            {
 1125                reportsDto.reports.Add(report.toReportDto());
 1126            }
 1127            return reportsDto;
 1128        }
 129
 130        public static ResponseGetReportDto _ResponseGetFromDomain(Domain.Report report)
 7131        {
 7132            var location = new ResponseGetLocationDto
 7133            {
 7134                Latitude = report.Location.Latitude,
 7135                Longitude = report.Location.Longitude,
 7136                Locality = report.Location.Locality,
 7137                Neighbourhood = report.Location.Neighbourhood,
 7138            };
 139
 7140            return new ResponseGetReportDto
 7141            {
 7142                Id = report.Id!,
 7143                UserId = report.UserId,
 7144                Location = location,
 7145                Type = report.Type,
 7146                Description = report.Description,
 7147                Title = report.Title,
 7148                CreatedAt = report.CreatedAt,
 7149                UpdatedAt = report.UpdatedAt
 7150            };
 7151        }
 152    }
 153}