using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using xCommons.Extensions;
using xExceptions.Constants;
using xHttpService.Constants;
using xIdentityModels.Constants;
using xIdentityModels.Extensions;
using xIdentityModels.Models;
using xIdentityService.Constants;
namespace xIdentityService.Providers {
public partial class XIdentityProvider {
//
#region Registration ...
///
/// Check a User Selector is Available for Registration or Not
///
/// an string value which reperesent a user
/// a boolean value
public async Task CanRegister (string userSelectByParam) {
//
// Validate ARgs ...
validationProvider.NotEmpty (userSelectByParam);
//
var client = GetRestClient ();
//
// Check Endpoint ...
var endpoint = XApiAccountEndpoint.CanRegisterUserName;
var @params = new Dictionary { { XParam.XUserName.GetStringValue (), userSelectByParam } };
//
// Check UserSelectByType ...
var type = userSelectByParam.GetUserSelectByType ();
switch (type) {
//
case XUserSelectBy.Username:
break;
//
case XUserSelectBy.MobileNumber:
//
endpoint = XApiAccountEndpoint.CanRegisterMobileNumber;
@params = new Dictionary { { XParam.XMobileNumber.GetStringValue (), userSelectByParam } };
break;
//
case XUserSelectBy.Email:
//
endpoint = XApiAccountEndpoint.CanRegisterEmail;
@params = new Dictionary { { XParam.XEmail.GetStringValue (), userSelectByParam } };
break;
//
default:
throw XException.InvalidData.ToException ();
}
//
var request = GetRestRequet (
endpoint,
@params: @params
);
//
var response = await RunRestRequest (
client,
request,
XHttpMethod.GET
);
return response;
}
///
/// Invite a User to Register on Dashboard
///
/// Authentication Tokens
/// an instance of XActionRequest class which provider requirement for action
/// an instance of XActionResponse
public async Task InviteUser (
XTokenResponse tokens,
XActionRequest model
) {
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder ()
.AddNotNull (
tokens,
model,
model.Device)
.AddNotEmpty (
tokens.AccessToken,
model.Lang,
model.Email,
model.ReturnUrl)
.AddEmailAddress (model.Email)
.ValidateGroupAsync ();
//
var client = GetRestClient (tokens);
//
var request = GetRestRequet (XApiAccountEndpoint.InviteUser);
request.AddJsonBody (model);
//
var response = await RunRestRequest (
client,
request,
XHttpMethod.POST
);
return response;
}
///
/// Recieve some Basic Informations and Start Registration Proccess
/// if they Valid
///
/// Registration Proccess Starts with Invoking this Action
///
/// an instance of XActionRequest class which provider requirement for action
/// an instance of XActionResponse
public async Task RequestRegistration (
XActionRequest model
) {
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder ()
.AddNotNull (
model,
model.Device,
model.DateOfBirth)
.AddNotEmpty (
model.Lang,
model.FirstName,
model.LastName)
.ValidateGroupAsync ();
//
var client = GetRestClient ();
//
var request = GetRestRequet (XApiAccountEndpoint.RequestRegistration);
request.AddJsonBody (model);
//
var response = await RunRestRequest (
client,
request,
XHttpMethod.POST
);
return response;
}
///
/// Add User Account Informations
///
/// an instance of XActionRequest class which provider requirement for action
/// an instance of XActionResponse
public async Task AddAccountInfo (
XActionRequest model
) {
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder ()
.AddNotNull (
model,
model.Device)
.AddNotEmpty (
model.Lang,
model.UserName,
model.Password,
model.ActionToken)
.ValidateGroupAsync ();
//
var client = GetRestClient ();
//
var request = GetRestRequet (XApiAccountEndpoint.AddAccountInfo);
request.AddJsonBody (model);
//
var response = await RunRestRequest (
client,
request,
XHttpMethod.POST
);
return response;
}
///
/// Upload and Attach a Profile Image for User. this is an Optional Step
///
/// user's requested action token
/// an instance of IFormFile for user's Avatar
/// an instance of XActionResponse
public async Task AttachProfileImage (
string actionToken,
IFormFile file
) {
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder ()
.AddNotNull (file)
.AddNotEmpty (actionToken)
.ValidateGroupAsync ();
//
var client = GetRestClient ();
//
var request = GetRestRequet (XApiAccountEndpoint.AttachProfileImage);
//
// Prepare Request Data ...
request.AddParameter (XParam.XActionToken.GetStringValue (), actionToken);
request.AddFileBytes (
XParam.XFile.GetStringValue (),
file.ToByteArray (),
file.FileName,
file.ContentType
);
//
var response = await RunRestRequest (
client,
request,
XHttpMethod.POST
);
return response;
}
///
/// Finish Registrationn Proccess
///
/// an instance of XActionRequest class which provider requirement for action
///
public async Task FinishRegistration (XActionRequest model) {
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder ()
.AddNotEmpty (
model.Lang,
model.Password,
model.ReturnUrl)
.AddNotNull (model.Device)
.AddUrl (model.ReturnUrl)
.ValidateGroupAsync ();
//
// Create Http Client ...
var client = GetRestClient ();
//
// Create Request ...
var request = GetRestRequet (XApiAccountEndpoint.FinishRegistration);
//
// Prepare Request Data ...
request.AddJsonBody (model);
//
var response = await RunRestRequest