project structure and namespaces

This commit is contained in:
Stoyan Zlatev
2023-06-19 15:43:55 +02:00
parent 2624dd7c87
commit 5446342fbd
198 changed files with 310 additions and 528 deletions
@@ -0,0 +1,21 @@
namespace LaaProduction.Web.Models
{
/// <summary>
/// C# class representation of the appsettings.json file.
/// </summary>
public class ApplicationSettings
{
/// <summary>
/// Default defined ConnectionStrings section for the appsettings.json
/// </summary>
public ConnectionStrings ConnectionStrings { get; set; }
}
/// <summary>
/// Default defined ConnectionStrings section for the appsettings.json
/// </summary>
public class ConnectionStrings
{
public string DefaultConnection { get; set; }
}
}
@@ -0,0 +1,16 @@
namespace LaaProduction.Web.Models
{
public class ModelResult
{
private bool state;
private ModelResult(bool state)
=> this.state = state;
public static implicit operator ModelResult(bool state)
=> new ModelResult(state);
public static implicit operator bool(ModelResult modelResult)
=> modelResult?.state ?? false;
}
}
@@ -0,0 +1,73 @@
namespace LaaProduction.Web.Models
{
using System.Collections.Generic;
using System.Web.Http.ModelBinding;
public class ModelResult<T>
{
private readonly Dictionary<string, string> errors;
private ModelResult(bool succeeded)
{
this.Succeeded = succeeded;
this.errors = new Dictionary<string, string>();
}
private ModelResult(T value)
{
this.Succeeded = true;
this.Value = value;
}
public T Value { get; }
public bool Succeeded { get; }
public IReadOnlyDictionary<string, string> ModelErrors
=> this.errors;
public void AddModelError(string key, string value)
{
if (key != null)
{
this.errors[key] = value;
}
}
public static implicit operator ModelResult<T>(T value)
=> new ModelResult<T>(value);
public static implicit operator ModelResult<T>(KeyValuePair<string, string> error)
{
var validationResults = new ModelResult<T>(false);
validationResults.AddModelError(error.Key, error.Value);
return validationResults;
}
public static implicit operator ModelResult<T>(ModelStateDictionary modelState)
{
var validationResults = new ModelResult<T>(false);
if (modelState != null)
{
foreach (var entry in modelState)
{
var errorKey = entry.Key;
var errorValue = entry.Value;
foreach (var error in errorValue.Errors)
{
if (!string.IsNullOrWhiteSpace(error.ErrorMessage))
{
validationResults.AddModelError(errorKey, error.ErrorMessage);
}
}
}
}
return validationResults;
}
}
}
@@ -0,0 +1,52 @@
namespace LaaProduction.Web.Models.SMTP
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Threading.Tasks;
public class SMTPClient
{
private readonly SMTPSettings settings;
public SMTPClient(SMTPSettings settings)
=> this.settings = settings;
public async Task<bool> SendAsync(string subject, string body, string from, IEnumerable<string> to)
{
try
{
using (var client = new SmtpClient(settings.Host, settings.Port))
{
client.Host = this.settings.Host;
client.Port = this.settings.Port;
client.UseDefaultCredentials = true;
var message = new MailMessage
{
From = new MailAddress(from),
Subject = subject,
Body = body,
IsBodyHtml = true,
};
foreach (var email in to)
{
message.To.Add(email);
}
await client.SendMailAsync(message);
}
return true;
}
catch (Exception e)
{
File.AppendAllLines("smtp.log", new[] { $"[SMTP_ERROR] {e.Message}" });
return false;
}
}
}
}
@@ -0,0 +1,15 @@
namespace LaaProduction.Web.Models.SMTP
{
using System.Collections.Generic;
public class SMTPEmail
{
public string From { get; set; }
public IEnumerable<string> To { get; set; }
public IEnumerable<string> Cc { get; set; }
public string Subject { get; set; }
}
}
@@ -0,0 +1,13 @@
namespace LaaProduction.Web.Models.SMTP
{
public class SMTPSettings
{
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}