2022-10-10 20:44:01 +05:30
|
|
|
|
using Abp.Application.Services;
|
|
|
|
|
using Abp.Domain.Repositories;
|
|
|
|
|
using BCS.BMC.Authorization.Users;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BCS.BMC.BMC.CompanyMasters;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Abp.Application.Services.Dto;
|
|
|
|
|
using BCS.BMC.ClientMasters.Dto;
|
|
|
|
|
using Abp.Extensions;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Security.Policy;
|
|
|
|
|
using Abp.Collections.Extensions;
|
|
|
|
|
using Abp.Linq.Extensions;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using BCS.BMC.Roles.Dto;
|
|
|
|
|
using Abp.AutoMapper;
|
|
|
|
|
using Abp.Domain.Entities;
|
|
|
|
|
using Abp.Runtime.Session;
|
|
|
|
|
using Abp.UI;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using BCS.BMC.CompanyMasters;
|
|
|
|
|
using System.Net;
|
|
|
|
|
//using System.Web.Mvc;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
//using System.Web.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using FromBodyAttribute = System.Web.Http.FromBodyAttribute;
|
|
|
|
|
using BCS.BMC.CompanyMasters.Dto;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Abp.Json;
|
2022-12-16 12:23:24 +05:30
|
|
|
|
using System.Diagnostics;
|
2022-10-19 11:28:11 +05:30
|
|
|
|
|
2022-10-10 20:44:01 +05:30
|
|
|
|
namespace BCS.BMC.CompanyMasters
|
|
|
|
|
{
|
|
|
|
|
public class CompanyMasterAppService : ICompanyMasterAppService
|
|
|
|
|
{
|
|
|
|
|
//private readonly IRepository<ClientMaster> _locationRepository;
|
|
|
|
|
private readonly IRepository<CompanyMaster, int> _companyMaster;
|
|
|
|
|
|
|
|
|
|
public CompanyMasterAppService(IRepository<CompanyMaster, int> companyMaster)
|
|
|
|
|
{
|
|
|
|
|
_companyMaster = companyMaster;
|
|
|
|
|
}
|
|
|
|
|
public async Task<PagedResultDto<CompanyListDto>> GetCompanies(GetCompanyInput input)
|
|
|
|
|
{
|
|
|
|
|
string filter = "";
|
|
|
|
|
if (!input.Filter.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
filter = Regex.Replace(input.Filter.Trim(), @"[^0-9a-zA-Z\s]+", "");
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var linqQuery = from company in _companyMaster.GetAll()
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
company,
|
|
|
|
|
company.CompanyName,
|
|
|
|
|
company.Url,
|
|
|
|
|
company.DomainName,
|
|
|
|
|
company.SubDomainName
|
|
|
|
|
};
|
|
|
|
|
//var query = linqQuery
|
|
|
|
|
// .WhereIf(input.Id.HasValue && input.Id.Value > 0, x => x.zone.Location.ClientId == input.ClientId.Value);
|
|
|
|
|
|
|
|
|
|
var totalCount = await linqQuery.CountAsync();
|
|
|
|
|
var items = await linqQuery.PageBy(input).ToListAsync();
|
|
|
|
|
|
|
|
|
|
var dtos = items.Select(c =>
|
|
|
|
|
{
|
|
|
|
|
var x = c.company;
|
|
|
|
|
var dto = x.MapTo<CompanyListDto>();
|
|
|
|
|
dto.Id = x.Id;
|
|
|
|
|
dto.CompanyName = x.CompanyName != null ? x.CompanyName : string.Empty;
|
|
|
|
|
dto.Url = x.Url != null ? x.Url : string.Empty;
|
|
|
|
|
dto.DomainName = c.DomainName != null ? x.DomainName : String.Empty;
|
|
|
|
|
dto.SubDomainName = c.SubDomainName != null ? x.SubDomainName : String.Empty;
|
|
|
|
|
return dto;
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new PagedResultDto<CompanyListDto>(totalCount, dtos);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<GetCompanyMasterForEditOutput> GetCompanyById(EntityDto input)
|
|
|
|
|
{
|
|
|
|
|
var entity = await _companyMaster.FirstOrDefaultAsync(x => x.Id == input.Id);
|
|
|
|
|
|
|
|
|
|
var dto = entity.MapTo<GetCompanyMasterForEditOutput>();
|
|
|
|
|
//if (entity != null)
|
|
|
|
|
dto.CompanyName = entity.CompanyName != null ? entity.CompanyName : string.Empty;
|
|
|
|
|
dto.Url = entity.Url != null ? entity.Url : string.Empty;
|
|
|
|
|
dto.DomainName = entity.DomainName != null ? entity.DomainName : string.Empty;
|
|
|
|
|
dto.SubDomainName = entity.SubDomainName != null ? entity.SubDomainName : string.Empty;
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
public async Task<int> CreateOrUpdateCompanyMaster(CreateOrUpdateCompanyMasterInput input)
|
|
|
|
|
{
|
|
|
|
|
int getClientId = 0;
|
|
|
|
|
if (input.Id > 0)
|
|
|
|
|
{
|
|
|
|
|
getClientId = (int)input.Id;
|
|
|
|
|
await Update(input);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
getClientId = await Create(input);
|
|
|
|
|
}
|
|
|
|
|
return getClientId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task<int> Create(CreateOrUpdateCompanyMasterInput input)
|
|
|
|
|
{
|
|
|
|
|
int companyId = 0;
|
2022-12-16 12:23:24 +05:30
|
|
|
|
// await GetUrlIsValid(input.Url);
|
2022-10-19 23:33:58 +05:30
|
|
|
|
var company = from m in _companyMaster.GetAllList().Where(m => m.Url.ToLower().Trim() == input.Url.ToLower().Trim())
|
2022-12-16 12:23:24 +05:30
|
|
|
|
|
|
|
|
|
select m;
|
|
|
|
|
|
2022-10-10 20:44:01 +05:30
|
|
|
|
var entity = input.MapTo<CompanyMaster>();
|
|
|
|
|
// entity.TenantId = AbpSession.TenantId;
|
|
|
|
|
|
|
|
|
|
Uri uri = new Uri(input.Url, UriKind.Absolute);
|
|
|
|
|
var host = uri.Host;
|
|
|
|
|
|
|
|
|
|
if (host.Split('.').Length > 2)
|
|
|
|
|
{
|
|
|
|
|
int lastIndex = host.LastIndexOf(".");
|
|
|
|
|
int index = host.LastIndexOf(".", lastIndex - 1);
|
|
|
|
|
var subdomain = host.Substring(0, index);
|
|
|
|
|
if (subdomain != "www")
|
|
|
|
|
{
|
|
|
|
|
entity.SubDomainName = subdomain;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
entity.DomainName = host;
|
2022-12-16 12:23:24 +05:30
|
|
|
|
if (company == null || company.Count() == 0)
|
2022-10-10 20:44:01 +05:30
|
|
|
|
{
|
|
|
|
|
companyId = await _companyMaster.InsertAndGetIdAsync(entity);
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-12-16 12:23:24 +05:30
|
|
|
|
{
|
2022-10-19 23:33:58 +05:30
|
|
|
|
throw new UserFriendlyException("The Input Url Already Exist");
|
2022-10-10 20:44:01 +05:30
|
|
|
|
}
|
|
|
|
|
return companyId;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:23:24 +05:30
|
|
|
|
public async Task<bool> GetUrlIsValid(string url)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
|
|
|
|
|
request.Timeout = 5000; //set the timeout to 5 seconds to keep the user from waiting too long for the page to load
|
|
|
|
|
request.Method = "HEAD"; //Get only the header information -- no need to download any content
|
|
|
|
|
|
|
|
|
|
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
|
|
|
|
|
{
|
|
|
|
|
int statusCode = (int)response.StatusCode;
|
|
|
|
|
if (statusCode >= 100 && statusCode < 400) //Good requests
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (statusCode >= 500 && statusCode <= 510) //Server Errors
|
|
|
|
|
{
|
|
|
|
|
//log.Warn(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
|
|
|
|
|
Debug.WriteLine(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// log.Warn(String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url), ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// log.Error(String.Format("Could not test url {0}.", url), ex);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-10-10 20:44:01 +05:30
|
|
|
|
protected async Task Update(CreateOrUpdateCompanyMasterInput input)
|
|
|
|
|
{
|
|
|
|
|
var entity = await _companyMaster.FirstOrDefaultAsync(x => x.Id == input.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
//throw new UserFriendlyException(NotFoundRecord ("Name", input.ClientName));
|
|
|
|
|
throw new UserFriendlyException("No Record Found");
|
|
|
|
|
}
|
2022-12-16 12:23:24 +05:30
|
|
|
|
|
2022-10-10 20:44:01 +05:30
|
|
|
|
|
|
|
|
|
var getclient = from m in _companyMaster.GetAllList()
|
|
|
|
|
.Where(m => m.Id == input.Id && m.CompanyName.ToLower().Trim() == input.CompanyName.ToLower().Trim() && m.Id != input.Id)
|
|
|
|
|
select m;
|
|
|
|
|
input.MapTo(entity);
|
2022-11-24 11:51:04 +05:30
|
|
|
|
Uri uri = new Uri(input.Url, UriKind.Absolute);
|
|
|
|
|
var host = uri.Host;
|
|
|
|
|
|
|
|
|
|
if (host.Split('.').Length > 2)
|
|
|
|
|
{
|
|
|
|
|
int lastIndex = host.LastIndexOf(".");
|
|
|
|
|
int index = host.LastIndexOf(".", lastIndex - 1);
|
|
|
|
|
var subdomain = host.Substring(0, index);
|
|
|
|
|
if (subdomain != "www")
|
|
|
|
|
{
|
|
|
|
|
entity.SubDomainName = subdomain;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
entity.DomainName = host;
|
2022-10-10 20:44:01 +05:30
|
|
|
|
if (getclient == null || getclient.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
await _companyMaster.UpdateAsync(entity);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new UserFriendlyException(string.Format(("ClientDuplicateMessage"), (input.CompanyName)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteCompanyList(InputCompanyId Ids)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Ids.Id)
|
|
|
|
|
{
|
|
|
|
|
await _companyMaster.DeleteAsync(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public async Task DeleteCompany(EntityDto<int> input)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
await _companyMaster.DeleteAsync(x => x.Id == input.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HttpStatusCodeResult> TokenByCompanyUrl(GetInputUrl input)
|
|
|
|
|
{
|
|
|
|
|
if (input.CompanyUrl == null)
|
|
|
|
|
{
|
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var company = await _companyMaster.FirstOrDefaultAsync(x => x.Url == input.CompanyUrl.ToString());
|
2022-12-16 12:23:24 +05:30
|
|
|
|
if (company == null)
|
2022-10-10 20:44:01 +05:30
|
|
|
|
{
|
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
2022-12-16 12:23:24 +05:30
|
|
|
|
|
2022-10-10 20:44:01 +05:30
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
Uri uri = new Uri(input.CompanyUrl);
|
|
|
|
|
var baseUrl = uri + "api/login";
|
|
|
|
|
//client.BaseAddress = new Uri($"https://{uri.Host}/");
|
|
|
|
|
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
var data = new
|
|
|
|
|
{
|
|
|
|
|
usernameOrEmailAddress = input.UsernameOrEmailAddress,
|
|
|
|
|
password = input.Password
|
|
|
|
|
};
|
|
|
|
|
var requestJson = JsonConvert.SerializeObject(data);
|
|
|
|
|
var requestContent = new StringContent(requestJson.ToString());
|
|
|
|
|
requestContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
|
|
|
|
|
HttpResponseMessage response = await client.PostAsync(baseUrl, requestContent);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
var responseStream = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.OK, responseStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-16 12:23:24 +05:30
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.OK);
|
2022-10-10 20:44:01 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|