Skip to content

Commit

Permalink
Merge pull request #12 from cowienduckie/dev
Browse files Browse the repository at this point in the history
fix: add helpers to convert dtos to domain objects
  • Loading branch information
cowienduckie authored Nov 24, 2023
2 parents 9cf6381 + 6110de1 commit 9175e70
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/BuildingBlocks/Shared/Common/ApiResponse/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ public enum Errors
[Description("Interal server error. Please contact the administrator.")]
COM_000,

[Description("Not found {CustomMessage}.")]
[Description("Not found {0}.")]
COM_001,

[Description("User ID is not a valid Guid.")]
VAL_000,

[Description("Validation failed. Detail messages: \n{CustomMessage}")]
[Description("Validation failed. Detail messages: \n{0}")]
VAL_001
}

public static class ErrorExtensions
{
public static string GetMessages(this Errors error, params string[] messageParams)
public static string GetMessages(this Errors error, params object?[] messageParams)
{
var messageFormat = error.GetDescription();
var messageFormat = error
.GetDescription()
.Replace("\n", Environment.NewLine);
var paramsCount = messageFormat.CountUniqueParams();

if (paramsCount != messageParams.Length)
Expand All @@ -33,7 +35,9 @@ public static string GetMessages(this Errors error, params string[] messageParam
"The number of parameters in the error message does not match the number of parameters passed.");
}

return string.Format(messageFormat, messageParams.ToArray<object>());
messageParams = messageParams.Select(p => p?.ToString()).ToArray<object?>();

return string.Format(messageFormat, messageParams);
}

public static string GetCode(this Errors error)
Expand Down
19 changes: 19 additions & 0 deletions src/BuildingBlocks/Shared/Common/Helpers/DateTimeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Shared.Common.Helpers;

public static class DateTimeHelper
{
public static DateTime ToDateTime(this object obj)
{
if (obj is DateTime dateTime)
{
return dateTime.ToUniversalTime();
}

if (obj is string stringValue && DateTime.TryParse(stringValue, out var parsedDateTime))
{
return parsedDateTime.ToUniversalTime();
}

throw new ArgumentException("Invalid DateTime format");
}
}
44 changes: 44 additions & 0 deletions src/BuildingBlocks/Shared/Common/Helpers/DictionaryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Reflection;

namespace Shared.Common.Helpers;

public static class DictionaryHelper
{
public static IDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this object obj)
{
return (IDictionary<TKey, TValue>)obj;
}

public static T GetObject<T>(this IDictionary<string, object?> dictionary) where T : new()
{
var result = new T();

foreach (var kvp in dictionary)
{
var property = typeof(T).GetProperty(kvp.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

if (property == null || !property.CanWrite)
{
continue;
}

var value = kvp.Value;

if (value is not null)
{
if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
{
value = value.ToDateTime();
}
else
{
value = Convert.ChangeType(value, property.PropertyType);
}
}

property.SetValue(result, value, null);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Portal.Data;
using Portal.UseCases.Responses;
using Shared.Common.ApiResponse;
using Shared.Common.Helpers;

namespace Portal.UseCases.Mutations.Handlers;

Expand Down Expand Up @@ -45,13 +46,18 @@ public async Task<UpdateKanbanProjectResponse> Handle(UpdateKanbanProjectCommand
return new UpdateKanbanProjectResponse
{
StatusCode = ResponseStatuses.NotFound.GetCode(),
ErrorCode = Errors.VAL_001.GetCode(),
ErrorMessage = Errors.VAL_001.GetMessages("ProjectId")
ErrorCode = Errors.COM_001.GetCode(),
ErrorMessage = Errors.COM_001.GetMessages("ProjectId")
};
}

// Update column of tasks
var sectionDict = request.Tasks.ToFrozenDictionary(t => Guid.Parse(t.Key), t => Guid.Parse(((KanbanTaskDto)t.Value).Column));
var sectionDict = request.Tasks
.Select(t => t.Value
.AsDictionary<string, object?>()
.GetObject<KanbanTaskDto>())
.ToFrozenDictionary(t => Guid.Parse(t.Id), t => Guid.Parse(t.Column));

var tasks = await _portalContext.Tasks
.Where(t => t.ProjectId.ToString() == request.ProjectId && t.DeletedOn == null)
.ToListAsync(cancellationToken);
Expand Down

0 comments on commit 9175e70

Please sign in to comment.