Skip to content

Commit

Permalink
Feature/37 merge VNS commons (#38)
Browse files Browse the repository at this point in the history
* Add Json Converters Utilites
* Add some helper class for requests and responses
* Add some Exception classes
  • Loading branch information
Reza-Noei committed Jun 8, 2023
1 parent e3235e4 commit dadb5ed
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -20,4 +20,8 @@
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

<ItemGroup>
<Folder Include="Requests\" />
</ItemGroup>

</Project>
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; }
}
}
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;
}
}
12 changes: 11 additions & 1 deletion Source/BSN.Commons/BSN.Commons.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -17,4 +17,14 @@
<RepositoryUrl>https://github.com/BSVN/Commons.git</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<Compile Remove="JsonConverter\**" />
<EmbeddedResource Remove="JsonConverter\**" />
<None Remove="JsonConverter\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Source/BSN.Commons/Dto/PagedEntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace BSN.Commons
{
/// <summary>
/// Stores the paginated data for the given entity type.
/// Paginated collection of an entity type.
/// </summary>
/// <typeparam name="T">Type of Entity for which pagination is being implemented.</typeparam>
/// <typeparam name="T">Entity type.</typeparam>
public class PagedEntityCollection<T>
{
/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions Source/BSN.Commons/Exceptions/HttpRequestException.cs
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;
}
}
}
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) { }
}
}
80 changes: 80 additions & 0 deletions Source/BSN.Commons/Extensions/CsvExtensions.cs
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();
}
}
}
24 changes: 24 additions & 0 deletions Source/BSN.Commons/Extensions/HttpContentExtensions.cs
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);
}
}
}
25 changes: 25 additions & 0 deletions Source/BSN.Commons/Extensions/IEnumerableExtensions.cs
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();
}
}
}
12 changes: 12 additions & 0 deletions Source/BSN.Commons/Extensions/ObjectExtensions.cs
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 Source/BSN.Commons/JsonConverters/JsonDateWithoutTimeConverter.cs
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));
}
}
}
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 Source/BSN.Commons/JsonConverters/JsonE164PhoneNumberConverter.cs
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 Source/BSN.Commons/JsonConverters/JsonForceDefaultConverter.cs
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);
}
}
}
Loading

0 comments on commit dadb5ed

Please sign in to comment.