Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharepoint synchronization functionality #763

Merged
merged 16 commits into from
Feb 13, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void Main()
builder.RegisterServiceLogging(new LoggerSettings(configurationPackage.Settings.Sections["Logging"]));

using var container = builder.Build();
logger = container.TryResolve<ILogger>(out ILogger val) ? val : null;
logger = container.ResolveOptional<ILogger>();
logger?.LogInformation($"Service type '{typeof(AppCenterBuilds).Name}' registered. Process: {Process.GetCurrentProcess().Id}.");
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static void Main()
builder.RegisterStatelessService<Manager>("Arcadia.Assistant.Avatars.ManagerType");

using var container = builder.Build();
logger = container.TryResolve<ILogger>(out ILogger val) ? val : null;
logger = container.ResolveOptional<ILogger>();
logger?.LogInformation($"Service type '{typeof(Manager).Name}' registered. Process: {Process.GetCurrentProcess().Id}.");
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static void Main()
builder.RegisterServiceLogging(new LoggerSettings(configurationPackage.Settings.Sections["Logging"]));

using var container = builder.Build();
logger = container.Resolve<ILogger>();
logger = container.ResolveOptional<ILogger>();
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public EmployeeMetadata(EmployeeId employeeId, string email)
return CalculateYearsFromDate(this.BirthDate, date);
}

public string Name => $"{this.FirstName ?? string.Empty} {this.LastName ?? string.Empty}".Trim();

public int? YearsServedAt(DateTime date)
{
DateTime toDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static void Main()
builder.RegisterServiceLogging(new LoggerSettings(configurationPackage.Settings.Sections["Logging"]));

using var container = builder.Build();
logger = container.TryResolve<ILogger>(out ILogger val) ? val : null;
logger = container.ResolveOptional<ILogger>();
logger?.LogInformation($"Service type '{typeof(Employees).Name}' registered. Process: {Process.GetCurrentProcess().Id}.");
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
ShilovSS marked this conversation as resolved.
Show resolved Hide resolved
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System;
using System.Linq.Expressions;

public abstract class BaseCondition : ICondition
{
protected BaseCondition(Expression<Func<StorageItem, object>> property, object value)
{
new PropertyNameParser().EnsureExpressionIsProperty(property);

this.Property = property;
this.Value = value;
}

public Expression<Func<StorageItem, object>> Property { get; }

public object Value { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System;
using System.Linq.Expressions;

public class EqualCondition : BaseCondition
{
public EqualCondition(Expression<Func<StorageItem, object>> property, object value)
: base(property, value)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
public interface ICondition
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public interface IExternalStorage : IDisposable
{
Task<IEnumerable<StorageItem>> GetItems(string list, IEnumerable<ICondition>? conditions = null, CancellationToken cancellationToken = default);

Task<StorageItem> AddItem(string list, StorageItem item, CancellationToken cancellationToken = default);

Task UpdateItem(string list, StorageItem item, CancellationToken cancellationToken = default);

Task DeleteItem(string list, string itemId, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System;
using System.Linq.Expressions;
using System.Reflection;

public class PropertyNameParser
{
public void EnsureExpressionIsProperty<T>(Expression<Func<T, object>> expression)
{
this.GetName(expression);
}

public string GetName<T>(Expression<Func<T, object>> expression)
{
switch (expression.Body)
{
case MemberExpression member when member.Member.MemberType == MemberTypes.Property:
return member.Member.Name;

case UnaryExpression unary when unary.Operand is MemberExpression operand && operand.Member.MemberType == MemberTypes.Property:
return operand.Member.Name;

default:
throw new ArgumentException("Expression is not a property", nameof(expression));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System.Collections.Generic;

public sealed class SharepointStorageItemComparer : IEqualityComparer<StorageItem>
{
public bool Equals(StorageItem x, StorageItem y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(y, null))
{
return false;
}

if (x.GetType() != y.GetType())
{
return false;
}

return
x.Id.Equals(y.Id) &&
x.Title.Equals(y.Title) &&
x.Description.Equals(y.Description) &&
x.StartDate.Date.Equals(y.StartDate.Date) &&
x.EndDate.Date.Equals(y.EndDate.Date) &&
x.Category.Equals(y.Category) &&
x.AllDayEvent == y.AllDayEvent &&
x.CalendarEventId.Equals(y.CalendarEventId);
}

public int GetHashCode(StorageItem obj)
{
unchecked
{
var hashCode = obj.Id.GetHashCode();
hashCode = (hashCode * 397) ^ obj.Title.GetHashCode();
hashCode = (hashCode * 397) ^ obj.Description.GetHashCode();
hashCode = (hashCode * 397) ^ obj.StartDate.Date.GetHashCode();
hashCode = (hashCode * 397) ^ obj.EndDate.Date.GetHashCode();
hashCode = (hashCode * 397) ^ obj.Category.GetHashCode();
hashCode = (hashCode * 397) ^ obj.AllDayEvent.GetHashCode();
hashCode = (hashCode * 397) ^ obj.CalendarEventId.GetHashCode();
return hashCode;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Arcadia.Assistant.ExternalStorages.Abstractions
{
using System;
using System.Text.Json.Serialization;

public class StorageItem
{
[JsonPropertyName("Id")]
ShilovSS marked this conversation as resolved.
Show resolved Hide resolved
public int Id { get; set; }

[JsonPropertyName("Title")]
public string Title { get; set; } = string.Empty;

[JsonPropertyName("Description")]
public string Description { get; set; } = string.Empty;

[JsonPropertyName("StartDate")]
public DateTime StartDate { get; set; }

[JsonPropertyName("EndDate")]
public DateTime EndDate { get; set; }

[JsonPropertyName("Category")]
public string Category { get; set; } = string.Empty;

[JsonPropertyName("AllDayEvent")]
public bool AllDayEvent { get; set; }

[JsonPropertyName("CalendarEventId")]
public string CalendarEventId { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Microsoft.ServiceFabric" Version="6.5.677" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Arcadia.Assistant.ExternalStorages.Abstractions\Arcadia.Assistant.ExternalStorages.Abstractions.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Arcadia.Assistant.ExternalStorages.SharepointOnline.Contracts
{
using System;
using System.Threading;
using System.Threading.Tasks;

public interface ISharepointAuthTokenService : IDisposable
{
Task<string> GetAccessToken(string sharepointUrl, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Arcadia.Assistant.ExternalStorages.SharepointOnline.Contracts
{
using System.Collections.Generic;

using Abstractions;

public interface ISharepointConditionsCompiler
{
string? CompileConditions(IEnumerable<ICondition>? conditions = null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Arcadia.Assistant.ExternalStorages.SharepointOnline.Contracts
{
using System;
using System.Linq.Expressions;

using Abstractions;

public interface ISharepointFieldsMapper
{
string GetSharepointField(Expression<Func<StorageItem, object>> property);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Arcadia.Assistant.ExternalStorages.SharepointOnline.Contracts
{
public interface ISharepointOnlineConfiguration
{
string ServerUrl { get; }

string ClientId { get; }

string ClientSecret { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Arcadia.Assistant.ExternalStorages.SharepointOnline.Contracts
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public interface ISharepointRequestExecutor : IDisposable
{
Task<T> ExecuteSharepointRequest<T>(SharepointRequest request, CancellationToken cancellationToken = default);

Task<HttpResponseMessage> ExecuteSharepointRequest(SharepointRequest request, CancellationToken cancellationToken = default);
}
}
Loading