HRM_RabbitMQ_Consumer/JSONParser/Structure/Login.cs

111 lines
3.0 KiB
C#

using Simple.OData.Client;
using System.Dynamic;
using System.Text.Json;
namespace JSONParser.Structure
{
public class Login : Entity
{
public string LoginName { get; set; }
public string TypeAuthentication { get; set; }
public bool Status { get; set; }
public override string ToString()
{
return LoginName;
}
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client)
{
var newPerson = new
{
LoginName = $"{LoginName}",
TypeAuthentication = TypeAuthentication,
Status = "Active"
};
var created = await client
.For("ILogins")
.Set(newPerson)
.InsertEntryAsync();
this.DirectumId = (long)created["Id"];
return (long)created["Id"];
}
public async override Task<long> Update(ODataClient client, long id)
{
var newPerson = new
{
LoginName = LoginName,
TypeAuthentication = TypeAuthentication,
Status = "Active"
};
var created = await client
.For("ILogins")
.Key(id)
.Set(newPerson)
.UpdateEntryAsync();
this.DirectumId = (long)created["Id"];
return (long)created["Id"];
}
public bool IsEqual(IDictionary<string, dynamic> log)
{
bool result = false;
if (LoginName == log["LoginName"] &&
TypeAuthentication == log["TypeAuthentication"])
{
result = true;
}
return result;
}
public async Task<long> GetIdBySid(ODataClient client)
{
long result = 0;
if (!string.IsNullOrEmpty(LoginName))
{
var log = await client
.For("ILogins")
.Filter($"tolower(LoginName) eq '{LoginName.ToLower()}'")
.FindEntryAsync();
if (log != null)
{
result = (long)log["Id"];
}
}
return result;
}
public async void CloseLogin(ODataClient client, long id)
{
var closedLogin = new
{
Status = "Closed"
};
await client
.For("ILogins")
.Key(id)
.Set(closedLogin)
.UpdateEntryAsync();
}
public async void OpenLogin(ODataClient client, long id)
{
var openedLogin = new
{
Status = "Active"
};
await client
.For("ILogins")
.Key(id)
.Set(openedLogin)
.UpdateEntryAsync();
}
}
}