Skip to content

Commit

Permalink
Polish & comment code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lenoch committed Jun 19, 2019
1 parent e5db69c commit d17d0ce
Show file tree
Hide file tree
Showing 30 changed files with 352 additions and 245 deletions.
4 changes: 2 additions & 2 deletions Business/Business.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@
<Compile Include="Services\Query\IDocumentQueryService.cs" />
<Compile Include="Services\Country\ICountryService.cs" />
<Compile Include="Services\Localization\ILocalizationService.cs" />
<Compile Include="Services\ViewModel\IUserModelService.cs" />
<Compile Include="Services\ViewModel\UserModelService.cs" />
<Compile Include="Services\Model\IUserModelService.cs" />
<Compile Include="Services\Model\UserModelService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
6 changes: 0 additions & 6 deletions Business/Repository/Avatar/AvatarRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ namespace Business.Repository.Avatar
{
public class AvatarRepository : IAvatarRepository
{
/// <summary>
/// Gets a computed unique file name and a binary of a user's avatar.
/// </summary>
/// <param name="user">The user to find an avatar for.</param>
/// <returns>If a user's avatar is found, named tuple with a filename and binary. Otherwise a tuple of <see langword="null"/>.</returns>
public (string fileName, byte[] binary) GetUserAvatar(MedioClinicUser user)
{
var avatarInfo = AvatarInfoProvider.GetAvatarInfo(user.AvatarId);
Expand All @@ -23,7 +18,6 @@ public class AvatarRepository : IAvatarRepository
return (null, null);
}

// TODO: Document.
public void UploadUserAvatar(MedioClinicUser user, byte[] avatarBinary)
{
var avatarInfo = AvatarInfoProvider.GetAvatarInfo(user.AvatarId);
Expand Down
4 changes: 1 addition & 3 deletions Business/Repository/Avatar/IAvatarRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

using Business.Identity.Models;
using Business.Identity.Models;

namespace Business.Repository.Avatar
{
Expand Down
2 changes: 1 addition & 1 deletion Business/Services/Country/CountryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Business.Services.Country
{
class CountryService : BaseService, ICountryService
public class CountryService : BaseService, ICountryService
{
public IEnumerable<CountryDto> GetCountries() =>
CountryInfoProvider.GetCountries()
Expand Down
14 changes: 6 additions & 8 deletions Business/Services/FileManagement/FileManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ public byte[] GetPostedFileBinary(HttpPostedFileBase file)
return data;
}

public string EnsureFilesystemPath(string physicalPath)
public void EnsureFolderExistence(string folderPhysicalPath)
{
if (!Directory.Exists(physicalPath))
if (!Directory.Exists(folderPhysicalPath))
{
DirectoryInfo directoryInfo;

try
{
directoryInfo = Directory.CreateDirectory(physicalPath);
directoryInfo = Directory.CreateDirectory(folderPhysicalPath);
}
catch (Exception ex)
{
ErrorHelper.LogException(nameof(FileManagementService), nameof(EnsureFilesystemPath), ex);
ErrorHelper.LogException(nameof(FileManagementService), nameof(EnsureFolderExistence), ex);

throw;
}
}

return physicalPath;
}

public string MakeStringUrlCompliant(string input)
Expand All @@ -73,7 +71,7 @@ public string MakeStringUrlCompliant(string input)
return stringBuilder.ToString();
}

public void WriteFileIfDoesntExist(string physicalPath, byte[] fileBinary, bool forceOverwrite = false)
public void EnsureFileExistence(string physicalPath, byte[] fileBinary, bool forceOverwrite = false)
{
if (!File.Exists(physicalPath) || forceOverwrite)
{
Expand All @@ -83,7 +81,7 @@ public void WriteFileIfDoesntExist(string physicalPath, byte[] fileBinary, bool
}
catch (Exception ex)
{
ErrorHelper.LogException(nameof(FileManagementService), nameof(WriteFileIfDoesntExist), ex);
ErrorHelper.LogException(nameof(FileManagementService), nameof(EnsureFileExistence), ex);

throw;
}
Expand Down
34 changes: 31 additions & 3 deletions Business/Services/FileManagement/IFileManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,45 @@

namespace Business.Services.FileManagement
{
// TODO: Document.
/// <summary>
/// Handles filesystem-related tasks.
/// </summary>
public interface IFileManagementService : IService
{
/// <summary>
/// Gets a byte array of a posted file.
/// </summary>
/// <param name="file">The posted file.</param>
/// <returns>The byte array of the file.</returns>
byte[] GetPostedFileBinary(HttpPostedFileBase file);

string EnsureFilesystemPath(string subfolder);
/// <summary>
/// Makes sure that a local folder exists.
/// </summary>
/// <param name="folderPhysicalPath">Folder physical path.</param>
void EnsureFolderExistence(string folderPhysicalPath);

/// <summary>
/// Makes a string contain only characters allowed in URLs.
/// </summary>
/// <param name="input">String to check.</param>
/// <returns>String converted to be URL-compliant.</returns>
string MakeStringUrlCompliant(string input);

void WriteFileIfDoesntExist(string path, byte[] fileBinary, bool forceOverwrite = false);
/// <summary>
/// Makes sure that a local file exists.
/// </summary>
/// <param name="path">File physical path.</param>
/// <param name="fileBinary">File byte array.</param>
/// <param name="forceOverwrite">Flag to overwrite an existing file.</param>
void EnsureFileExistence(string path, byte[] fileBinary, bool forceOverwrite = false);

/// <summary>
/// Converts a physical into a server-relative path.
/// </summary>
/// <param name="request">HTTP request.</param>
/// <param name="physicalPath">Physical path.</param>
/// <returns>A server-relative path.</returns>
string GetServerRelativePath(HttpRequestBase request, string physicalPath);
}
}
8 changes: 6 additions & 2 deletions Business/Services/Localization/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ public class LocalizationService : BaseService, ILocalizationService
public string Localize(string resourceKey) =>
ResHelper.GetString(resourceKey);

public string LocalizeFormat(string resourceKey, params object[] args) =>
ResHelper.GetStringFormat(resourceKey, args);
public string LocalizeFormat(string resourceKey, params object[] args)
{
var rawText = ResHelper.GetString(resourceKey);

return string.Format(rawText, args);
}
}
}
32 changes: 32 additions & 0 deletions Business/Services/Model/IUserModelService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;

using Business.Identity.Models;

namespace Business.Services.Model
{
/// <summary>
/// Custom mapper between Identity user models and custom user models.
/// </summary>
public interface IUserModelService : IService
{
/// <summary>
/// Maps properties of a <see cref="MedioClinicUser"/> object to properties with the same name and type in the <paramref name="targetModelType"/> object.
/// </summary>
/// <param name="user">The source user object.</param>
/// <param name="targetModelType">The type of the output object.</param>
/// <param name="customMappings">Custom mappings of properties with different names and/or types.</param>
/// <returns>The <paramref name="targetModelType"/> object with mapped properties.</returns>
object MapToCustomModel(MedioClinicUser user, Type targetModelType, Dictionary<(string propertyName, Type propertyType), object> customMappings = null);

/// <summary>
/// Maps properties of the <paramref name="customModel"/> object to properties of the same name and type in the <see cref="MedioClinicUser"/> object.
/// </summary>
/// <param name="customModel">The source model object.</param>
/// <param name="userToMapTo">The target Identity user object.</param>
/// <param name="customMappings">Custom mappings of properties with different names and/or types.</param>
/// <returns>The mapped <see cref="MedioClinicUser"/> object.</returns>
/// <remarks>Maps properties by reference, does not copy them by value.</remarks>
MedioClinicUser MapToMedioClinicUser(object customModel, MedioClinicUser userToMapTo, Dictionary<(string propertyName, Type propertyType), object> customMappings = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@

using Business.Identity.Models;

namespace Business.Services.ViewModel
namespace Business.Services.Model
{
class UserModelService : BaseService, IUserModelService
public class UserModelService : BaseService, IUserModelService
{
public object MapToViewModel(
public object MapToCustomModel(
MedioClinicUser user,
Type targetViewModelType,
Type targetModelType,
Dictionary<(string propertyName, Type propertyType), object> customMappings = null)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}

if (targetViewModelType == null)
if (targetModelType == null)
{
throw new ArgumentNullException(nameof(targetViewModelType));
throw new ArgumentNullException(nameof(targetModelType));
}

var userProperties = user.GetType().GetProperties();
var targetProperties = targetViewModelType.GetProperties();
var viewModel = Activator.CreateInstance(targetViewModelType);
var targetProperties = targetModelType.GetProperties();
var viewModel = Activator.CreateInstance(targetModelType);

foreach (var targetProperty in targetProperties)
{
Expand All @@ -49,21 +49,21 @@ public object MapToViewModel(
}

public MedioClinicUser MapToMedioClinicUser(
object viewModel,
object customModel,
MedioClinicUser userToMapTo,
Dictionary<(string propertyName, Type propertyType), object> customMappings = null)
{
if (viewModel == null)
if (customModel == null)
{
throw new ArgumentNullException(nameof(viewModel));
throw new ArgumentNullException(nameof(customModel));
}

if (userToMapTo == null)
{
throw new ArgumentNullException(nameof(userToMapTo));
}

var viewModelProperties = viewModel.GetType().GetProperties();
var viewModelProperties = customModel.GetType().GetProperties();
var userProperties = userToMapTo.GetType().GetProperties();

foreach (var userProperty in userProperties)
Expand All @@ -80,7 +80,7 @@ public MedioClinicUser MapToMedioClinicUser(
}
else if (sourceProperty != null)
{
userProperty.SetValue(userToMapTo, sourceProperty.GetValue(viewModel));
userProperty.SetValue(userToMapTo, sourceProperty.GetValue(customModel));
}
}

Expand Down
32 changes: 0 additions & 32 deletions Business/Services/ViewModel/IUserModelService.cs

This file was deleted.

23 changes: 16 additions & 7 deletions MedioClinic/App_Data/Global/Resources/MedioClinic.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<data name="Controllers.Account.CheckEmailResetPassword.Title" xml:space="preserve">
<value>Check email</value>
</data>
<data name="Controllers.Account.CheckEmailResetPassword.ViewbagMessage" xml:space="preserve">
<data name="Controllers.Account.CheckEmailResetPassword.Message" xml:space="preserve">
<value>Please check your email to reset your password.</value>
</data>
<data name="Controllers.Account.ConfirmUser.Success.Title" xml:space="preserve">
Expand All @@ -132,9 +132,6 @@
<data name="Controllers.Account.InvalidAttempt" xml:space="preserve">
<value>Invalid sign in attempt.</value>
</data>
<data name="Controllers.Account.InvalidToken.Title" xml:space="preserve">
<value>Invalid token</value>
</data>
<data name="Controllers.Account.InvalidToken.Message" xml:space="preserve">
<value>The operation cannot be done. The security token is incorrect.</value>
</data>
Expand All @@ -156,13 +153,13 @@
<data name="Controllers.Account.ResetPassword.Success.Message" xml:space="preserve">
<value>Your password was successfully reset. You can now &lt;a href="{0}"&gt;sign in&lt;/a&gt;.</value>
</data>
<data name="Controllers.Profile.Index.SavedChanges" xml:space="preserve">
<data name="Controllers.Profile.Index.UserUpdated.Message" xml:space="preserve">
<value>Your changes were saved.</value>
</data>
<data name="ProfileManager.GetRoleTitle.Title.Doctor" xml:space="preserve">
<data name="ProfileManager.GetRoleTitle.Doctor" xml:space="preserve">
<value>Doctor</value>
</data>
<data name="ProfileManager.GetRoleTitle.Title.Patient" xml:space="preserve">
<data name="ProfileManager.GetRoleTitle.Patient" xml:space="preserve">
<value>Patient</value>
</data>
<data name="Models.Profile.CommonUserViewModel.AvatarFile" xml:space="preserve">
Expand Down Expand Up @@ -249,4 +246,16 @@
<data name="Controllers.Base.InvalidInput.Message" xml:space="preserve">
<value>The information submitted in the form was in invalid. Please review the specific error messages and try again.</value>
</data>
<data name="Controllers.Account.Register.DirectSuccess.Message" xml:space="preserve">
<value>Your registration was successful. You're now logged in.</value>
</data>
<data name="Controllers.Account.Register.DirectSuccess.Title" xml:space="preserve">
<value>You're registered</value>
</data>
<data name="Controllers.Profile.Index.UserNotFound.Message" xml:space="preserve">
<value>There is no matching user object in our database.</value>
</data>
<data name="Controllers.Profile.Index.UserNotUpdated.Message" xml:space="preserve">
<value>Your user profile data were not updated.</value>
</data>
</root>
Loading

0 comments on commit d17d0ce

Please sign in to comment.