-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Json Converters Utilites * Add some helper class for requests and responses * Add some Exception classes
- Loading branch information
Showing
19 changed files
with
478 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Source/BSN.Commons.PresentationInfrastructure/Requests/AdvancedSearchRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace BSN.Commons.Requests | ||
{ | ||
/// <summary> | ||
/// Advanced search request for pagination and Sieve processing. | ||
/// <see href="https://github.com/Biarity/Sieve"/> | ||
/// </summary> | ||
[DataContract] | ||
public class AdvancedSearchRequest | ||
{ | ||
/// <summary> | ||
/// Encoded sieve filters. | ||
/// </summary> | ||
[DataMember(Order = 1)] | ||
public string Filters { get; set; } | ||
|
||
/// <summary> | ||
/// Encoded sieve sorts. | ||
/// </summary> | ||
[DataMember(Order = 2)] | ||
public string Sorts { get; set; } | ||
|
||
/// <summary> | ||
/// Page number (Start from 1). | ||
/// </summary> | ||
[DataMember(Order = 3)] | ||
public uint PageNumber { get; set; } | ||
|
||
/// <summary> | ||
/// Page size. | ||
/// </summary> | ||
[DataMember(Order = 4)] | ||
public uint PageSize { get; set; } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Source/BSN.Commons.PresentationInfrastructure/Requests/PrimitiveRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace BSN.Commons.Requests | ||
{ | ||
/// <summary> | ||
/// Primitive datatype request aimed to be used on Grpc compatible services. | ||
/// </summary> | ||
/// <remarks> | ||
/// This Wrappers is added due to lake of Primitive type support in Grpc methods | ||
/// </remarks> | ||
/// <typeparam name="T"></typeparam> | ||
[DataContract] | ||
public class PrimitiveRequest<T> | ||
{ | ||
/// <summary> | ||
/// Primitive value. | ||
/// </summary> | ||
[DataMember(Order = 1)] | ||
public T Value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Net; | ||
|
||
namespace BSN.Commons.Exceptions | ||
{ | ||
public class HttpRequestException : System.Net.Http.HttpRequestException | ||
{ | ||
public HttpStatusCode? StatusCode { get; } | ||
|
||
public HttpRequestException(string message) : base(message) { } | ||
|
||
public HttpRequestException(string message, Exception innerException) : base(message, innerException) { } | ||
|
||
public HttpRequestException(string message, Exception innerException, HttpStatusCode? statusCode) : base(message, innerException) | ||
{ | ||
StatusCode = statusCode; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Source/BSN.Commons/Exceptions/InterserviceCommunicationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace BSN.Commons.Exceptions | ||
{ | ||
public class InterserviceCommunicationException : Exception | ||
{ | ||
public InterserviceCommunicationException() : base() { } | ||
|
||
public InterserviceCommunicationException(string message) : base(message) { } | ||
|
||
public InterserviceCommunicationException(string message, Exception innerException) : base(message, innerException) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.Extensions | ||
{ | ||
public static class CsvExtensions | ||
{ | ||
public static string ToCsv<T>(this IEnumerable<T> collection) where T : class | ||
{ | ||
StringBuilder csvContentBuilder = new StringBuilder(); | ||
|
||
Type genericType = Activator.CreateInstance<T>().GetType(); | ||
PropertyInfo[] properties = genericType.GetProperties(); | ||
|
||
StringBuilder csvHeaderBuilder = new StringBuilder(); | ||
for (int i = 0; i < properties.Length; i++) | ||
{ | ||
if (i + 1 < properties.Length) | ||
{ | ||
var hasNameAttribute = properties[i].GetCustomAttribute(typeof(JsonPropertyNameAttribute)) != null; | ||
|
||
if (hasNameAttribute) | ||
{ | ||
var serializationName = properties[i].GetCustomAttribute<JsonPropertyNameAttribute>().Name; | ||
|
||
csvContentBuilder.Append(serializationName); | ||
} | ||
else | ||
{ | ||
csvContentBuilder.Append(properties[i].Name); | ||
} | ||
|
||
csvContentBuilder.Append(","); | ||
} | ||
else | ||
{ | ||
csvContentBuilder.Append(properties[i].Name); | ||
} | ||
} | ||
|
||
csvContentBuilder.AppendLine(csvHeaderBuilder.ToString()); | ||
|
||
foreach (T item in collection) | ||
{ | ||
StringBuilder csvRecordBuilder = new StringBuilder(); | ||
for (int i = 0; i < properties.Length; i++) | ||
{ | ||
object value = genericType.GetProperty(properties[i].Name).GetValue(item, null); | ||
|
||
if (i + 1 < properties.Length) | ||
{ | ||
if (value != null) | ||
{ | ||
csvRecordBuilder.Append(value.ToString()); | ||
csvRecordBuilder.Append(","); | ||
} | ||
else | ||
{ | ||
csvRecordBuilder.Append(","); | ||
} | ||
} | ||
else | ||
{ | ||
if (value != null) | ||
{ | ||
csvRecordBuilder.Append(value.ToString()); | ||
} | ||
} | ||
} | ||
|
||
csvContentBuilder.AppendLine(csvRecordBuilder.ToString()); | ||
} | ||
|
||
return csvContentBuilder.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace BSN.Commons.Extensions | ||
{ | ||
public static class HttpContentExtensions | ||
{ | ||
public static async Task<T> ReadAsAsyncCaseInsensitive<T>(this HttpContent content) where T : class | ||
{ | ||
var options = new JsonSerializerOptions() | ||
{ | ||
PropertyNameCaseInsensitive = true, | ||
Converters = | ||
{ | ||
new JsonStringEnumConverter() | ||
} | ||
}; | ||
|
||
return JsonSerializer.Deserialize<T>(await content.ReadAsStringAsync(), options); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace BSN.Commons.Extensions | ||
{ | ||
public static class IEnumerableExtensions | ||
{ | ||
public static bool HasDuplicates<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
return source.Count() != source.Distinct().Count(); | ||
} | ||
|
||
public static IEnumerable<TSource> FindDuplicates<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
return source?.GroupBy(P => P) | ||
.Where(Q => Q.Count() > 1) | ||
.Select(Z => Z.Key); | ||
} | ||
|
||
public static bool IsNullOrEmpty<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
return source == null || !source.Any(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Text.Json; | ||
|
||
namespace BSN.Commons.Extensions | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
public static string SerializeToJson(this object @object) | ||
{ | ||
return JsonSerializer.Serialize(@object); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Source/BSN.Commons/JsonConverters/JsonDateWithoutTimeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.JsonConverters | ||
{ | ||
public class JsonDateWithoutTimeConverter : JsonConverter<DateTime> | ||
{ | ||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return DateTime.SpecifyKind(DateTime.ParseExact(reader.GetString(), "yyyy-MM-dd", CultureInfo.InvariantCulture), DateTimeKind.Utc); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Source/BSN.Commons/JsonConverters/JsonE164PhoneNumberCollectionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.JsonConverters | ||
{ | ||
public class JsonE164PhoneNumberCollectionConverter : JsonConverter<string[]> | ||
{ | ||
public override string[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var list = JsonSerializer.Deserialize<string[]>(ref reader, options); | ||
|
||
if (list == null) | ||
return null; | ||
|
||
return list.Select(P => JsonE164PhoneNumberConverter.Deserialize(P)).ToArray(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, string[] value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value.Select(P => JsonE164PhoneNumberConverter.Serialize(P)), options); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Source/BSN.Commons/JsonConverters/JsonE164PhoneNumberConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.JsonConverters | ||
{ | ||
public class JsonE164PhoneNumberConverter : JsonConverter<string> | ||
{ | ||
protected const string InternationalPrefixSymbol = "+"; | ||
|
||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return Deserialize(reader.GetString()); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(Serialize(value)); | ||
} | ||
|
||
public static string Serialize(string value) | ||
{ | ||
if (value == null) | ||
return null; | ||
|
||
if (string.IsNullOrWhiteSpace(value)) | ||
return string.Empty; | ||
|
||
return value.StartsWith(InternationalPrefixSymbol) ? value : $"{InternationalPrefixSymbol}{value}"; | ||
} | ||
|
||
public static string Deserialize(string value) | ||
{ | ||
return (value ?? string.Empty).StartsWith(InternationalPrefixSymbol) ? value.Substring(1) : value; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Source/BSN.Commons/JsonConverters/JsonForceDefaultConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.JsonConverters | ||
{ | ||
public class JsonForceDefaultConverter<T> : JsonConverter<T> | ||
{ | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return JsonSerializer.Deserialize<T>(ref reader); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value); | ||
} | ||
} | ||
} |
Oops, something went wrong.