Skip to content

Commit

Permalink
feat(issue-213): add unittests and cover 50 percent of c-gen
Browse files Browse the repository at this point in the history
Merge pull request #214 from live-dev999/live-dev999/issue213
  • Loading branch information
live-dev999 committed Feb 27, 2022
2 parents 6f1586c + 446dc7d commit f952361
Show file tree
Hide file tree
Showing 48 changed files with 1,412 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<IActionResult> Detail(long id)
[HttpPost]
[Route("id")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(long id, MailRequestViewModel model)
public async Task<IActionResult> Edit(long id, EmailRequestViewModel model)
{
var certificate = await _emailSenderService.GetByIdAsync(id, CancellationToken.None);
if (certificate == null)
Expand All @@ -65,7 +65,7 @@ public IActionResult Create()

[HttpPost]
[Route("")]
public async Task<IActionResult> CreateReally(MailRequestViewModel model)
public async Task<IActionResult> CreateReally(EmailRequestViewModel model)
{
var emailRequest = await _emailSenderService.AddAsync(model.ToModel(), CancellationToken.None);
await _emailSender.Send(model.To, model.Subject, model.Body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<IActionResult> GetByIdAsync(long id, CancellationToken ct)

[HttpPut]
[Route("id")]
public async Task<IActionResult> UpdateAsync(long id, [FromBody]MailRequestViewModel model, CancellationToken ct)
public async Task<IActionResult> UpdateAsync(long id, [FromBody]EmailRequestViewModel model, CancellationToken ct)
{
var certificate = await _emailSenderService.UpdateAsync(model.ToModel(), ct);
return Ok(certificate.ToViewModel());
Expand All @@ -59,7 +59,7 @@ public async Task<IActionResult> UpdateAsync(long id, [FromBody]MailRequestViewM
[HttpPost]
[HttpPut]
[Route("")]
public async Task<IActionResult> AddAsync([FromBody]MailRequestViewModel model, CancellationToken ct)
public async Task<IActionResult> AddAsync([FromBody]EmailRequestViewModel model, CancellationToken ct)
{
var emailRequest = await _emailSenderService.AddAsync(model.ToModel(), ct);
await _emailSender.Send(model.To, model.Subject, model.Body);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;

namespace O2NextGen.ESender.Api.Extensions
{
public static class KeyGenerator
{
// private const string ValidChars = "abcdefghjkmnprstwxz2345789"; // Letters and numbers that are not easily mixed with others when reading
private const string ValidChars = "ABCDEFHJKLMNPRSTUWXYZ012345789";
private static readonly Dictionary<long, bool> ValidCharLookup = new Dictionary<long, bool>();
private static readonly Random Rnd = new Random();

static KeyGenerator()
{
// Set up a quick lookup dictionary for all valid characters
foreach (var c in ValidChars.ToUpperInvariant())
ValidCharLookup.Add(c, true);
}

public static string Generate(int length)
{
var ret = new char[length];
for (var i = 0; i < length; i++)
{
int c;
lock (Rnd)
{
c = Rnd.Next(0, ValidChars.Length);
}

ret[i] = ValidChars[c];
}

return new string(ret);
}

public static bool Validate(int maxLength, string key)
{
if (key.Length > maxLength)
return false;

foreach (var c in key.ToUpperInvariant())
if (!ValidCharLookup.ContainsKey(c))
return false;
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace O2NextGen.ESender.Api.Extensions
{
public static class UnixDateExtensions
{
private static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();

public static DateTime ConvertToDateTime(this long seconds, bool isSeconds = true)
{
return isSeconds ? UnixEpoch.AddSeconds(seconds) : UnixEpoch.AddMinutes(seconds);
}

public static long ConvertToUnixTime(this DateTime datetime, bool isSeconds = true)
{
return isSeconds ? (long)(datetime - UnixEpoch).TotalSeconds : (long)(datetime - UnixEpoch).TotalMinutes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using O2NextGen.ESender.Api.Extensions;
using O2NextGen.ESender.Api.Models;
using O2NextGen.ESender.Business.Models;

namespace O2NextGen.ESender.Api.Mappings
{
public class BaseMappings<TViewModel, TModel>
where TViewModel : class, IViewModel
where TModel : class, IBaseModel
{
public TViewModel ToViewModel(TModel model)
{
if (model == null)
return null;

var returnViewModel = Activator.CreateInstance<TViewModel>();

returnViewModel.Id = model.Id;
returnViewModel.ExternalId = model.ExternalId;
returnViewModel.AddedDate = model.AddedDate;
returnViewModel.ModifiedDate = model.ModifiedDate;
returnViewModel.DeletedDate = model.DeletedDate;
returnViewModel.IsDeleted = model.IsDeleted;

return returnViewModel;
}

public TModel ToServiceModel(TViewModel viewModel)
{
//Todo: return not null
if (viewModel == null)
return null;

var model = Activator.CreateInstance<TModel>();

model.Id = viewModel.Id;
model.ExternalId = viewModel.ExternalId;
model.AddedDate = viewModel.AddedDate ?? default(long);
model.ModifiedDate = viewModel.ModifiedDate ?? default(long);
model.DeletedDate = viewModel.DeletedDate ?? DateTime.Now.ConvertToUnixTime();
model.IsDeleted = viewModel.IsDeleted ?? default(bool);

return model;
}

public IReadOnlyCollection<TViewModel> ToViewModel(IReadOnlyCollection<TModel> models)
{
if (models.Count == 0)
{
return Array.Empty<TViewModel>();
}

var subscription = new TViewModel[models.Count];
var i = 0;
foreach (var model in models)
{
subscription[i] = ToViewModel(model);
++i;
}

return new ReadOnlyCollection<TViewModel>(subscription);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ namespace O2NextGen.ESender.Api.Mappings
{
public static class EmailRequestMappings
{
public static MailRequestViewModel ToViewModel(this EmailRequest model)
private static readonly
BaseMappings<EmailRequestViewModel, EmailRequestModel> BaseMappings;

static EmailRequestMappings()
{
BaseMappings =
new BaseMappings<EmailRequestViewModel, EmailRequestModel>();
}

public static EmailRequestViewModel ToViewModel(this EmailRequestModel model)
{
if (model == null)
return null;

var viewModel = new MailRequestViewModel();
var viewModel = BaseMappings.ToViewModel(model);

//Bindings
viewModel.Id = model.Id;
viewModel.From = model.From;
viewModel.To = model.To;
viewModel.Subject = model.Subject;
Expand All @@ -25,40 +33,55 @@ public static MailRequestViewModel ToViewModel(this EmailRequest model)
return viewModel;
}

public static EmailRequest ToModel(this MailRequestViewModel requestViewModel)
public static EmailRequestModel ToModel(this EmailRequestViewModel viewModel)
{
if (requestViewModel == null)
if (viewModel == null)
return null;

var model = new EmailRequest();
var model = BaseMappings.ToServiceModel(viewModel);

//Bindings
model.Id = requestViewModel.Id;
model.From = requestViewModel.From;
model.To = requestViewModel.To;
model.Subject = requestViewModel.Subject;
model.Body = requestViewModel.Body;
model.From = viewModel.From;
model.To = viewModel.To;
model.Subject = viewModel.Subject;
model.Body = viewModel.Body;

return model;
}

public static IReadOnlyCollection<MailRequestViewModel> ToViewModel(
this IReadOnlyCollection<EmailRequest> models)
public static IReadOnlyCollection<EmailRequestViewModel> ToViewModel(
this IReadOnlyCollection<EmailRequestModel> models)
{
if (models.Count == 0)
{
return Array.Empty<MailRequestViewModel>();
return Array.Empty<EmailRequestViewModel>();
}

var subscription = new MailRequestViewModel[models.Count];
var subscription = new EmailRequestViewModel[models.Count];
var i = 0;
foreach (var model in models)
{
subscription[i] = ToViewModel(model);
++i;
}

return new ReadOnlyCollection<MailRequestViewModel>(subscription);
return new ReadOnlyCollection<EmailRequestViewModel>(subscription);
}

public static IReadOnlyCollection<EmailRequestModel> ToModel(
this IReadOnlyCollection<EmailRequestViewModel> models)
{
if (models.Count == 0) return Array.Empty<EmailRequestModel>();

var subscription = new EmailRequestModel[models.Count];
var i = 0;
foreach (var model in models)
{
subscription[i] = ToModel(model);
++i;
}

return new ReadOnlyCollection<EmailRequestModel>(subscription);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
namespace O2NextGen.ESender.Api.Models
{
public interface IViewModel
{
long Id { get; set; }
string ExternalId { get; set; }
long? ModifiedDate { get; set; }
long? AddedDate { get; set; }
long? DeletedDate { get; set; }
bool? IsDeleted { get; set; }
}
public class EmailRequestViewModel: IViewModel
{
public long Id { get; set; }
public string ExternalId { get; set; }
public long? ModifiedDate { get; set; }
public long? AddedDate { get; set; }
public long? DeletedDate { get; set; }
public bool? IsDeleted { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Body { get; set; }
public string Subject { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model MailRequestViewModel
@model EmailRequestViewModel

<form asp-controller="Board" asp-action="CreateReally" method="post">
<label asp-for="From">From</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

@model MailRequestViewModel
@model EmailRequestViewModel

<form asp-controller="Board" asp-action="Edit" asp-route-id="@Model.Id">
<label asp-for="From">From</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model IEnumerable<MailRequestViewModel>
@model IEnumerable<EmailRequestViewModel>
<h1> E-Sender Service</h1>
<h2>Administration board v1.0.0.0</h2>
<style>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace O2NextGen.ESender.Business.Models
{
public class EmailRequestModel: IBaseModel
{
public long Id { get; set; }
public string ExternalId { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public long AddedDate { get; set; }
public long DeletedDate { get; set; }
public bool IsDeleted { get; set; }
public long ModifiedDate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace O2NextGen.ESender.Business.Models
{
public interface IBaseModel
{
long Id { get; set; }
string ExternalId { get; set; }
long ModifiedDate { get; set; }
long AddedDate { get; set; }
long DeletedDate { get; set; }
bool IsDeleted { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace O2NextGen.ESender.Business.Services
{
public interface IEmailSenderService
{
Task<IReadOnlyCollection<EmailRequest>> GetAllAsync(CancellationToken ct);
Task<IReadOnlyCollection<EmailRequestModel>> GetAllAsync(CancellationToken ct);

Task<EmailRequest> GetByIdAsync(long id, CancellationToken ct);
Task<EmailRequestModel> GetByIdAsync(long id, CancellationToken ct);

Task<EmailRequest> UpdateAsync(EmailRequest emailRequest, CancellationToken ct);
Task<EmailRequestModel> UpdateAsync(EmailRequestModel emailRequestModel, CancellationToken ct);

Task<EmailRequest> AddAsync(EmailRequest emailRequest, CancellationToken ct);
Task<EmailRequestModel> AddAsync(EmailRequestModel emailRequestModel, CancellationToken ct);

Task RemoveAsync(long id, CancellationToken ct);
}
Expand Down
Loading

0 comments on commit f952361

Please sign in to comment.