Skip to content

Commit

Permalink
T #98 added automapper
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Nov 9, 2022
1 parent fe07ae4 commit bdc87c9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public async Task GetStatusAsync(string ticketId)
else
{
// ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc);
DateTime? date = Miscellaneous.UnixTimeStampToUtcDateTime(ticket.ClosedDate);
string closedDate = date is not null ? TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate)).ToString() : "Ticket still open";
DateTime date = Miscellaneous.UnixTimeStampToUtcDateTime(ticket.ClosedDate);
string closedDate = date != default ? TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate)).ToString() : "Ticket still open";

embed = new EmbedBuilder().WithColor(Color.Magenta)
.WithThumbnailUrl(Icons.Huppy1)
Expand Down
2 changes: 0 additions & 2 deletions src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ public ModuleConfigurator AddServices()

// repositories
_services.AddScoped<IUserRepository, UserRepository>();
_services.AddScoped<IReminderRepository, ReminderRepository>();
_services.AddScoped<ITicketRepository, TicketRepository>();

return this;
}
Expand Down
1 change: 1 addition & 0 deletions src/Huppy/Huppy.App/Huppy.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\Huppy.Kernel\Huppy.Kernel.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/HuppyService/HuppyService.Core/Models/Ticket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Ticket : DbModel<string>
public bool IsClosed { get; set; }
public string? TicketAnswer { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? ClosedDate { get; set; }
public DateTime ClosedDate { get; set; }

//[ForeignKey("UserId")]
public ulong UserId { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions src/HuppyService/HuppyService.Core/Utilities/Miscellaneous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ public static ulong DateTimeToUnixTimeStamp(DateTime date)
return (ulong)(TimeZoneInfo.ConvertTimeToUtc(date) -
new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
}

public static bool IsDebug()
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AutoMapper;
using HuppyService.Core.Models;
using HuppyService.Core.Utilities;
using HuppyService.Service.Protos.Models;

namespace HuppyService.Service.Configuration
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<CommandLogModel, CommandLog>()
.ForMember(dest => dest.Date,
opt => opt.MapFrom(src => Miscellaneous.UnixTimeStampToUtcDateTime(src.Date)));

CreateMap<CommandLog, CommandLogModel>()
.ForMember(dest => dest.Date,
opt => opt.MapFrom(src => Miscellaneous.DateTimeToUnixTimeStamp(src.Date)));

CreateMap<ServerModel, Server>()
.ReverseMap();

CreateMap<ServerRoomsModel, ServerRooms>()
.ReverseMap();

CreateMap<ReminderModel, Reminder>()
.ForMember(dest => dest.RemindDate,
opt => opt.MapFrom(src => Miscellaneous.UnixTimeStampToUtcDateTime(src.RemindDate)));

CreateMap<Reminder, ReminderModel>()
.ForMember(dest => dest.RemindDate,
opt => opt.MapFrom(src => Miscellaneous.DateTimeToUnixTimeStamp(src.RemindDate)));

CreateMap<TicketModel, Ticket>()
.ForMember(dest => dest.CreatedDate,
opt => opt.MapFrom(src => Miscellaneous.UnixTimeStampToUtcDateTime(src.CreatedDate)))
.ForMember(dest => dest.ClosedDate,
opt => opt.MapFrom(src => Miscellaneous.UnixTimeStampToUtcDateTime(src.ClosedDate)));

CreateMap<Ticket, TicketModel>()
.ForMember(dest => dest.CreatedDate,
opt => opt.MapFrom(src => Miscellaneous.DateTimeToUnixTimeStamp(src.CreatedDate)))
.ForMember(dest => dest.ClosedDate,
opt => opt.MapFrom(src => Miscellaneous.DateTimeToUnixTimeStamp(src.ClosedDate)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using HuppyService.Core.Interfaces.IRepositories;
using AutoMapper;
using HuppyService.Core.Interfaces.IRepositories;
using HuppyService.Core.Models;
using HuppyService.Core.Utilities;
using HuppyService.Infrastructure;
using HuppyService.Infrastructure.Repositories;
using HuppyService.Service.Protos.Models;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.Extensions.Configuration;

namespace HuppyService.Service.Configuration
Expand All @@ -22,6 +27,23 @@ public ModuleConfiguration AddAppConfigurator()
return this;
}

public ModuleConfiguration ConfigureMappings()
{
var mapperConfig = new MapperConfiguration(conf =>
{
conf.AddProfile(new MappingProfile());
});


if(Miscellaneous.IsDebug()) mapperConfig.AssertConfigurationIsValid();

var mapper = mapperConfig.CreateMapper();

_services.AddSingleton(mapper);

return this;
}

public ModuleConfiguration AddGrpc()
{
_services.AddGrpc();
Expand Down
11 changes: 6 additions & 5 deletions src/HuppyService/HuppyService.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
builder.Host.ConfigureServices(services =>
{
_ = new ModuleConfiguration(builder.Configuration, services)
.AddAppConfigurator()
.AddGrpc()
.AddHttpClients()
.AddDatabase()
.AddRepositories();
.AddAppConfigurator()
.ConfigureMappings()
.AddGrpc()
.AddHttpClients()
.AddDatabase()
.AddRepositories();
});

var app = builder.Build();
Expand Down
21 changes: 5 additions & 16 deletions src/HuppyService/HuppyService.Service/Services/TicketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async Task<TicketModel> AddTicket(AddTicketInput request, Server
ticket.CreatedDate = DateTime.UtcNow;
ticket.TicketAnswer = null;
ticket.IsClosed = false;
ticket.ClosedDate = null;
ticket.ClosedDate = default;

//Ticket ticket = new()
//{
Expand Down Expand Up @@ -113,7 +113,7 @@ public override async Task<TicketModelCollection> GetPaginatedTickets(GetPaginat
Topic = ticket.Topic,
IsClosed = ticket.IsClosed,
TicketAnswer = ticket.TicketAnswer,
ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
ClosedDate = ticket.ClosedDate == default ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate)
}));

Expand All @@ -127,17 +127,6 @@ public override async Task<TicketModel> GetTicket(GetTicketInput request, Server
if (ticket is null) return null!;

return ReflectionMapper.Map<TicketModel>(ticket);
//return new TicketModel
//{
// Id = ticket.Id,
// UserId = ticket.UserId,
// Description = ticket.Description,
// Topic = ticket.Topic,
// IsClosed = ticket.IsClosed,
// TicketAnswer = ticket.TicketAnswer,
// ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
// CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate)
//};
}

public override async Task<TicketModelCollection> GetTickets(Protos.Void request, ServerCallContext context)
Expand All @@ -154,7 +143,7 @@ public override async Task<TicketModelCollection> GetTickets(Protos.Void request
Topic = ticket.Topic,
IsClosed = ticket.IsClosed,
TicketAnswer = ticket.TicketAnswer,
ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
ClosedDate = ticket.ClosedDate == default ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate)
}));

Expand All @@ -180,7 +169,7 @@ public override async Task<TicketModelCollection> GetUserPaginatedTickets(GetUse
Topic = ticket.Topic,
IsClosed = ticket.IsClosed,
TicketAnswer = ticket.TicketAnswer,
ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
ClosedDate = ticket.ClosedDate == default ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate)
}));

Expand All @@ -202,7 +191,7 @@ public override async Task<TicketModelCollection> GetUserTickets(UserId request,
Topic = ticket.Topic,
IsClosed = ticket.IsClosed,
TicketAnswer = ticket.TicketAnswer,
ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
ClosedDate = ticket.ClosedDate == default ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate),
CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate)
}));

Expand Down

0 comments on commit bdc87c9

Please sign in to comment.