From 7fe4697e013a1c63d5cb0772c1def799846251a4 Mon Sep 17 00:00:00 2001 From: Rene Evertz Date: Tue, 27 May 2025 09:24:37 +0200 Subject: [PATCH] Admin Service in ApiService und ApiStreamService ausgelagert --- Host/ApiService.svc | 1 + Host/ApiStreamService.svc | 1 + Host/Host.csproj | 2 + Host/Web.config | 12 +++++ Service/Service.csproj | 4 ++ Service/ServiceContracts/IApiService.cs | 27 ++++++++++ Service/ServiceContracts/IApiStreamService.cs | 22 ++++++++ .../ServiceImplementations/ApiServiceImp.cs | 49 +++++++++++++++++ .../ApiStreamServiceImp.cs | 38 +++++++++++++ Shared/AppSender/ApiSender.cs | 12 +++++ Shared/Core/AppError.cs | 7 ++- .../MikePHPContracts/ApiResponse.cs | 54 +++++++++++++++++++ Shared/Shared.csproj | 3 +- 13 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 Host/ApiService.svc create mode 100644 Host/ApiStreamService.svc create mode 100644 Service/ServiceContracts/IApiService.cs create mode 100644 Service/ServiceContracts/IApiStreamService.cs create mode 100644 Service/ServiceImplementations/ApiServiceImp.cs create mode 100644 Service/ServiceImplementations/ApiStreamServiceImp.cs create mode 100644 Shared/AppSender/ApiSender.cs create mode 100644 Shared/DataContracts/MikePHPContracts/ApiResponse.cs diff --git a/Host/ApiService.svc b/Host/ApiService.svc new file mode 100644 index 000000000..b00105fe0 --- /dev/null +++ b/Host/ApiService.svc @@ -0,0 +1 @@ +<%@ ServiceHost Language="C#" Debug="true" Service="BeWo.Service.ServiceImplementations.ApiServiceImp" %> diff --git a/Host/ApiStreamService.svc b/Host/ApiStreamService.svc new file mode 100644 index 000000000..354fa3962 --- /dev/null +++ b/Host/ApiStreamService.svc @@ -0,0 +1 @@ +<%@ ServiceHost Language="C#" Debug="true" Service="BeWo.Service.ServiceImplementations.ApiStreamServiceImp" %> diff --git a/Host/Host.csproj b/Host/Host.csproj index 86018603a..4951d8731 100644 --- a/Host/Host.csproj +++ b/Host/Host.csproj @@ -175,6 +175,8 @@ + + diff --git a/Host/Web.config b/Host/Web.config index 603bd746c..eba3a24ad 100644 --- a/Host/Web.config +++ b/Host/Web.config @@ -178,6 +178,12 @@ + + + + + + @@ -207,6 +213,12 @@ + + + + + + diff --git a/Service/Service.csproj b/Service/Service.csproj index d3c677ca3..6fcc96971 100644 --- a/Service/Service.csproj +++ b/Service/Service.csproj @@ -523,6 +523,8 @@ + + @@ -537,6 +539,8 @@ + + diff --git a/Service/ServiceContracts/IApiService.cs b/Service/ServiceContracts/IApiService.cs new file mode 100644 index 000000000..26538dd54 --- /dev/null +++ b/Service/ServiceContracts/IApiService.cs @@ -0,0 +1,27 @@ +using BS.Shared.Attributes; +using BS.Shared.DataContracts.AdminService; +using System; +using System.Collections.Generic; +using System.Linq; +using System.ServiceModel.Web; +using System.ServiceModel; +using System.Text; +using System.Threading.Tasks; +using BS.Shared.DataContracts.MikePHPContracts; +using System.IO; + +namespace BeWo.Service.ServiceContracts +{ + [ServiceContract] + public interface IApiService + { + [OperationContract] + [WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json)] + [RequireNoTenant] + ApiResponse Test(); + + [OperationContract] + [WebInvoke(UriTemplate = "/GetGkvSum", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] + ApiResponse GetGkvAbrechnungSumme(AdminServiceSumReqDC request); + } +} diff --git a/Service/ServiceContracts/IApiStreamService.cs b/Service/ServiceContracts/IApiStreamService.cs new file mode 100644 index 000000000..e92ac7cf1 --- /dev/null +++ b/Service/ServiceContracts/IApiStreamService.cs @@ -0,0 +1,22 @@ +using BS.Shared.Attributes; +using BS.Shared.DataContracts.AdminService; +using BS.Shared.DataContracts.MikePHPContracts; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.ServiceModel.Web; +using System.ServiceModel; +using System.Text; +using System.Threading.Tasks; + +namespace BeWo.Service.ServiceContracts +{ + [ServiceContract] + public interface IApiStreamService + { + [OperationContract] + [WebInvoke(UriTemplate = "/DownloadReport", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] + Stream DownloadReport(); + } +} diff --git a/Service/ServiceImplementations/ApiServiceImp.cs b/Service/ServiceImplementations/ApiServiceImp.cs new file mode 100644 index 000000000..06b74d431 --- /dev/null +++ b/Service/ServiceImplementations/ApiServiceImp.cs @@ -0,0 +1,49 @@ +using BeWo.Data.Access; +using BeWo.Service.ServiceContracts; +using BS.Shared.Core; +using BS.Shared.DataContracts.AdminService; +using BS.Shared.DataContracts.MikePHPContracts; +using DevExpress.Pdf.Native.BouncyCastle.Ocsp; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.ServiceModel; +using System.ServiceModel.Web; +using System.Text; +using System.Threading.Tasks; + +namespace BeWo.Service.ServiceImplementations +{ + [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] + public class ApiServiceImp : IApiService + { + public ApiResponse Test() + { + return ApiResponse.SuccessResponse( "Api Service Test erfolgreich!"); + } + + public ApiResponse GetGkvAbrechnungSumme(AdminServiceSumReqDC request) + { + if (request == null) + { + throw new ArgumentException("request darf nicht null sein"); + } + + if(!Utils.TryParseISO8601(request.Start, out var start)) + { + throw new ArgumentException("start entspricht nicht ISO8601 Norm."); + } + + if (!Utils.TryParseISO8601(request.End, out var end)) + { + throw new ArgumentException("end entspricht nicht ISO8601 Norm."); + } + + var sum = DAOFactory.ScriptDAO.GetGkvSumByDateRange(start, end); + + return ApiResponse.SuccessResponse(sum ?? 0); + } + } +} diff --git a/Service/ServiceImplementations/ApiStreamServiceImp.cs b/Service/ServiceImplementations/ApiStreamServiceImp.cs new file mode 100644 index 000000000..88166944b --- /dev/null +++ b/Service/ServiceImplementations/ApiStreamServiceImp.cs @@ -0,0 +1,38 @@ +using BeWo.Data.Access; +using BeWo.Service.ServiceContracts; +using BS.Shared.Core; +using BS.Shared.DataContracts.AdminService; +using BS.Shared.DataContracts.MikePHPContracts; +using DevExpress.Pdf.Native.BouncyCastle.Ocsp; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.ServiceModel; +using System.ServiceModel.Web; +using System.Text; +using System.Threading.Tasks; + +namespace BeWo.Service.ServiceImplementations +{ + [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] + public class ApiStreamServiceImp : IApiStreamService + { + public Stream DownloadReport() + { + string filename = "xrechnung.pdf"; + + string filePath = Path.Combine(@"C:\Users\ownSoft\Desktop\XRechnung\", filename); + if (!File.Exists(filePath)) + throw new FileNotFoundException("Datei nicht gefunden"); + + // Setze die Header + WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf"; + WebOperationContext.Current.OutgoingResponse.Headers.Add( + "Content-Disposition", $"attachment; filename=\"{filename}\""); + + return new FileStream(filePath, FileMode.Open, FileAccess.Read); + } + } +} diff --git a/Shared/AppSender/ApiSender.cs b/Shared/AppSender/ApiSender.cs new file mode 100644 index 000000000..d0da333b5 --- /dev/null +++ b/Shared/AppSender/ApiSender.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BS.Shared.AppSender +{ + public static class ApiSender + { + } +} diff --git a/Shared/Core/AppError.cs b/Shared/Core/AppError.cs index 0c7825d60..e4263fc58 100644 --- a/Shared/Core/AppError.cs +++ b/Shared/Core/AppError.cs @@ -12,13 +12,18 @@ namespace BS.Shared.Core { public string Code { get; set; } public string Message { get; set; } + public Dictionary Arguments { get; set; } + public List InnerErrors { get; set; } + public string Displayname => $"AppError {Code}: {Message}"; // Private damit zentral alle Errors - private AppError(string code, string message) + private AppError(string code, string message, Dictionary arguments = null, List innerErrors = null) { Code = code; Message = message; + Arguments = arguments; + InnerErrors = innerErrors; } //public static readonly AppError Example = new AppError("Code", "Message"); diff --git a/Shared/DataContracts/MikePHPContracts/ApiResponse.cs b/Shared/DataContracts/MikePHPContracts/ApiResponse.cs new file mode 100644 index 000000000..abfb8fdef --- /dev/null +++ b/Shared/DataContracts/MikePHPContracts/ApiResponse.cs @@ -0,0 +1,54 @@ +using BS.Shared.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BS.Shared.DataContracts.MikePHPContracts +{ + public class ApiResponse + { + public T Data { get; set; } + + public List Errors { get; set; } + + public List Messages { get; set; } + + public Dictionary Meta { get; set; } + + public string TraceId { get; set; } + + public DateTime Timestamp { get; set; } = DateTime.Now; + + public int StatusCode { get; set; } = 200; + + public bool Success => Errors.Count == 0; + + //Factory Methods + public static ApiResponse SuccessResponse(T data, List messages = null, Dictionary meta = null) + { + return new ApiResponse + { + Data = data, + Messages = messages ?? new List(), + Meta = meta ?? new Dictionary(), + }; + } + + public static ApiResponse FailureResponse(List errors, int statusCode = 400, string traceId = null) + { + return new ApiResponse + { + Errors = errors, + StatusCode = statusCode, + TraceId = traceId + }; + } + + public static ApiResponse FailureResponse(AppError error, int statusCode = 400, string traceId = null) + { + return FailureResponse(new List { error }, statusCode, traceId); + } + } +} diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index 26ebef32e..0e36e520b 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -134,8 +134,8 @@ + - @@ -213,6 +213,7 @@ +