First Initial

This commit is contained in:
Nakorn Rientrakrunchai
2020-02-20 15:02:39 +07:00
commit 8b98125e49
3048 changed files with 760804 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace STAFF_API.Services
{
public interface IMailService
{
Task SendMailMessage(string from, string[] recepients, string[] bccs,
string[] ccs, string subject, string body);
}
}

View File

@@ -0,0 +1,100 @@
using Microsoft.Extensions.Options;
using STAFF_API.Configure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Threading.Tasks;
namespace STAFF_API.Services
{
public class MailService: IMailService
{
private readonly MailConfig _config;
public MailService(IOptions<MailConfig> config)
{
_config = config.Value;
}
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="recepients">Recepient address</param>
/// <param name="bccs">Bcc recepient</param>
/// <param name="ccs">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>
public async Task SendMailMessage(string from, string[] recepients, string[] bccs,
string[] ccs, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(_config.UserName, from);
// Set the recepient address of the mail message
foreach (string recepient in recepients)
{
if (!string.IsNullOrEmpty(recepient))
{
mMailMessage.To.Add(new MailAddress(recepient));
}
}
// Check if the bcc value is nothing or an empty string
foreach (string bcc in bccs)
{
if (!string.IsNullOrEmpty(bcc))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
}
// Check if the cc value is nothing or an empty value
foreach (string cc in ccs)
{
if (!string.IsNullOrEmpty(cc))
{
mMailMessage.CC.Add(new MailAddress(cc));
}
}
// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.EnableSsl = _config.EnableSsl;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(_config.UserName, _config.Password);
mSmtpClient.UseDefaultCredentials = _config.DefaultCredentials;
mSmtpClient.Credentials = basicAuthenticationInfo;
mSmtpClient.Host = _config.Host;
mSmtpClient.Port = _config.Port;
try
{
await mSmtpClient.SendMailAsync(mMailMessage);
}
catch (Exception ex1)
{
throw ex1;
}
}
}
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace STAFF_API.Services
{
public interface IWebAPIClient
{
#region Properties
/// <summary>
/// Specify base address to use on http client
/// </summary>
string BaseAddress { get; set; }
/// <summary>
/// Access token to call web api
/// </summary>
string AccessToken { get; set; }
Dictionary<string, string> Headers { get; set; }
#endregion
#region Methods
/// <summary>
/// Call web api with HTTP GET method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/GetPromotion?promotionId=49
/// </param>
/// <returns>Return as generic class</returns>
Task<T> GetAsync<T>(string actionPart);
/// <summary>
/// Call web api with HTTP POST method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/AddNewGeneralPromotion
/// </param>
/// <param name="inputModel">Input to call web api</param>
/// <returns>Return as generic class</returns>
Task<T> PostAsync<T>(string actionPart, object inputModel);
/// <summary>
/// Call web api with HTTP POST method with file uploading
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/AddNewGeneralPromotion
///</param>
/// <param name="formData">Multipart Form Data Content</param>
/// <returns>Return as generic class</returns>
Task<T> PostFileAsync<T>(string actionPart, MultipartFormDataContent formData);
/// <summary>
/// Call web api with HTTP POST method with file uploading
/// </summary>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/AddNewGeneralPromotion
///</param>
/// <param name="inputModel">Input to call web api</param>
/// <param name="returnPhysicalFilePath">Expected file downloaded path</param>
/// <returns>Return file download path</returns>
Task<string> PostDownloadAsync(string actionPart, object inputModel, string returnPhysicalFilePath);
/// <summary>
/// Call web api with HTTP Put method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/UpdateGeneralPromotion?promotionId=49
/// </param>
/// <param name="inputModel">Input to call web api</param>
/// <returns>Return as generic class</returns>
Task<T> PutAsync<T>(string actionPart, object inputModel);
/// <summary>
/// Call web api with HTTP Delete method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// For example: BookingPromotion/DeleteGeneralPromotion?promotionId=49
/// </param>
/// <returns></returns>
Task<T> DeleteAsync<T>(string actionPart);
#endregion
}
}

View File

@@ -0,0 +1,323 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace STAFF_API.Services
{
public class WebAPIClient : IWebAPIClient
{
#region Private Properties
private HttpClient _client;
// To assign access token & additional headers
private HttpClient APIClient
{
get
{
if(_client == null)
_client = new HttpClient();
if(_client.BaseAddress == null)
_client.BaseAddress = new Uri(this.BaseAddress);
// Clear existing header
_client.DefaultRequestHeaders.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (this.Headers != null)
{
foreach (KeyValuePair<string, string> header in Headers)
_client.DefaultRequestHeaders.Add(header.Key, header.Value.ToString());
}
// Assign token
if (!string.IsNullOrEmpty(this.AccessToken))
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.AccessToken);
return _client;
}
}
#endregion
#region Public Properties
/// <summary>
/// Specify base address to use on http client
/// </summary>
public string BaseAddress { get; set; }
/// <summary>
/// Access token to call web api
/// </summary>
public string AccessToken { get; set; }
public Dictionary<string, string> Headers { get; set; }
#endregion
/// <summary>
///
/// </summary>
public WebAPIClient()
{
}
#region Private Functions
#endregion
#region Public Functions
/// <summary>
/// Call web api with HTTP GET method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// </param>
/// <returns>Return as generic class</returns>
public async Task<T> GetAsync<T>(string actionPart)
{
try
{
var client = this.APIClient;
HttpResponseMessage response = await client.GetAsync(actionPart);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (Exception)
{
// Return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char
return default(T);
}
}
else
throw new InvalidOperationException(response.ReasonPhrase);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Call web api with HTTP POST method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// </param>
/// <param name="inputModel">Input to call web api</param>
/// <returns>Return as generic class</returns>
public async Task<T> PostAsync<T>(string actionPart, object inputModel)
{
try
{
var client = this.APIClient;
var param = Newtonsoft.Json.JsonConvert.SerializeObject(inputModel);
HttpContent contentPost = new StringContent(param, null, "application/json");
HttpResponseMessage response = await client.PostAsync(actionPart, contentPost);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (Exception)
{
// Return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char
return default(T);
}
}
else
throw new InvalidOperationException(response.ReasonPhrase);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Call web api with HTTP POST method with file uploading
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
///</param>
/// <param name="formData">Multipart Form Data Content</param>
/// <returns>Return as generic class</returns>
public async Task<T> PostFileAsync<T>(string actionPart, MultipartFormDataContent formData)
{
var client = this.APIClient;
try
{
HttpResponseMessage response = await client.PostAsync(actionPart, formData);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(jsonString);
}
else
{
string x = await response.Content.ReadAsStringAsync();
throw new InvalidOperationException(x);
}
}
finally
{
// Clear all stream.
formData.Dispose();
}
}
/// <summary>
/// Call web api with HTTP POST method with file uploading
/// </summary>
/// <param name="actionPart">The part of the web api serevice.
///</param>
/// <param name="inputModel">Input to call web api</param>
/// <param name="returnPhysicalFilePath">Expected file downloaded path</param>
/// <returns>Return file download path</returns>
public async Task<string> PostDownloadAsync(string actionPart, object inputModel, string returnPhysicalFilePath)
{
try
{
var client = this.APIClient;
//Set post body parameters
var param = Newtonsoft.Json.JsonConvert.SerializeObject(inputModel);
HttpContent contentPost = new StringContent(param, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(actionPart, contentPost);
if (response.IsSuccessStatusCode)
{
if (File.Exists(returnPhysicalFilePath))
File.Delete(returnPhysicalFilePath);
// Read response asynchronously and save asynchronously to file
using (FileStream fileStream = new FileStream(returnPhysicalFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//copy the content from response to filestream
await response.Content.CopyToAsync(fileStream);
}
return returnPhysicalFilePath;
}
else
{
throw new FileNotFoundException();
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Call web api with HTTP Put method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// </param>
/// <param name="inputModel">Input to call web api</param>
/// <returns>Return as generic class</returns>
public async Task<T> PutAsync<T>(string actionPart, object inputModel)
{
try
{
var client = this.APIClient;
//Set post body parameters
var param = Newtonsoft.Json.JsonConvert.SerializeObject(inputModel);
HttpContent contentPut = new StringContent(param, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PutAsync(actionPart, contentPut);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (Exception)
{
// Return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char
return default(T);
}
}
else
throw new InvalidOperationException(response.ReasonPhrase);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Call web api with HTTP Delete method
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionPart">The part of the web api serevice.
/// </param>
/// <returns></returns>
public async Task<T> DeleteAsync<T>(string actionPart)
{
try
{
var client = this.APIClient;
HttpResponseMessage response = await client.DeleteAsync(actionPart);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (Exception)
{
// Return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char
return default(T);
}
}
else
throw new InvalidOperationException(response.ReasonPhrase);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}