Skip to content

Commit

Permalink
Merge pull request #27 from FaithBeam/mapperly
Browse files Browse the repository at this point in the history
Mapperly
  • Loading branch information
FaithBeam authored Oct 18, 2024
2 parents bfc2201 + a8cc1aa commit ff0221d
Show file tree
Hide file tree
Showing 41 changed files with 881 additions and 1,835 deletions.
77 changes: 77 additions & 0 deletions YMouseButtonControl.Core/Mappings/ButtonMappingMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using Riok.Mapperly.Abstractions;
using YMouseButtonControl.Core.ViewModels.Models;
using YMouseButtonControl.DataAccess.Models;

namespace YMouseButtonControl.Core.Mappings;

[Mapper]
public static partial class ButtonMappingMapper
{
[MapDerivedType<DisabledMapping, DisabledMappingVm>]
[MapDerivedType<NothingMapping, NothingMappingVm>]
[MapDerivedType<SimulatedKeystroke, SimulatedKeystrokeVm>]
[MapDerivedType<RightClick, RightClickVm>]
public static partial BaseButtonMappingVm Map(ButtonMapping? buttonMapping);

[MapDerivedType<DisabledMappingVm, DisabledMapping>]
[MapDerivedType<NothingMappingVm, NothingMapping>]
[MapDerivedType<SimulatedKeystrokeVm, SimulatedKeystroke>]
[MapDerivedType<RightClickVm, RightClick>]
public static partial ButtonMapping Map(BaseButtonMappingVm buttonMapping);

[MapDerivedType<DisabledMappingVm, DisabledMapping>]
[MapDerivedType<NothingMappingVm, NothingMapping>]
[MapDerivedType<SimulatedKeystrokeVm, SimulatedKeystroke>]
[MapDerivedType<RightClickVm, RightClick>]
public static partial void Map(BaseButtonMappingVm src, ButtonMapping dst);

public static BaseSimulatedKeystrokeTypeVm MapSimulatedKeystrokeType(
SimulatedKeystrokeType simulatedKeystrokeType
) =>
simulatedKeystrokeType switch
{
SimulatedKeystrokeType.AsMousePressedAndReleasedActionType =>
new AsMousePressedAndReleasedActionTypeVm(),
SimulatedKeystrokeType.DuringMouseActionType => new DuringMouseActionTypeVm(),
SimulatedKeystrokeType.InAnotherThreadPressedActionType =>
new InAnotherThreadPressedActionTypeVm(),
SimulatedKeystrokeType.InAnotherThreadReleasedActionType =>
new InAnotherThreadReleasedActionTypeVm(),
SimulatedKeystrokeType.MouseButtonPressedActionType =>
new MouseButtonPressedActionTypeVm(),
SimulatedKeystrokeType.MouseButtonReleasedActionType =>
new MouseButtonReleasedActionTypeVm(),
SimulatedKeystrokeType.RepeatedlyWhileButtonDownActionType =>
new RepeatedlyWhileButtonDownActionTypeVm(),
SimulatedKeystrokeType.StickyHoldActionType => new StickyHoldActionTypeVm(),
SimulatedKeystrokeType.StickyRepeatActionType => new StickyRepeatActionTypeVm(),
_ => throw new ArgumentOutOfRangeException(
nameof(simulatedKeystrokeType),
simulatedKeystrokeType,
null
),
};

public static SimulatedKeystrokeType? MapSimulatedKeystrokeTypeVm(
BaseSimulatedKeystrokeTypeVm? baseSimulatedKeystrokeVm
) =>
baseSimulatedKeystrokeVm switch
{
null => null,
AsMousePressedAndReleasedActionTypeVm =>
SimulatedKeystrokeType.AsMousePressedAndReleasedActionType,
DuringMouseActionTypeVm => SimulatedKeystrokeType.DuringMouseActionType,
InAnotherThreadPressedActionTypeVm =>
SimulatedKeystrokeType.InAnotherThreadPressedActionType,
InAnotherThreadReleasedActionTypeVm =>
SimulatedKeystrokeType.InAnotherThreadReleasedActionType,
MouseButtonPressedActionTypeVm => SimulatedKeystrokeType.MouseButtonPressedActionType,
MouseButtonReleasedActionTypeVm => SimulatedKeystrokeType.MouseButtonReleasedActionType,
RepeatedlyWhileButtonDownActionTypeVm =>
SimulatedKeystrokeType.RepeatedlyWhileButtonDownActionType,
StickyHoldActionTypeVm => SimulatedKeystrokeType.StickyHoldActionType,
StickyRepeatActionTypeVm => SimulatedKeystrokeType.StickyRepeatActionType,
_ => throw new ArgumentOutOfRangeException(),
};
}
151 changes: 0 additions & 151 deletions YMouseButtonControl.Core/Mappings/MappingProfile.cs

This file was deleted.

25 changes: 25 additions & 0 deletions YMouseButtonControl.Core/Mappings/ProfileMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using Riok.Mapperly.Abstractions;
using YMouseButtonControl.Core.ViewModels.Models;
using YMouseButtonControl.DataAccess.Models;

namespace YMouseButtonControl.Core.Mappings;

[Mapper]
public static partial class ProfileMapper
{
public static partial ProfileVm Map(Profile? profile);

public static partial Profile Map(ProfileVm vm);

public static partial void Map(ProfileVm src, Profile dst);

private static List<BaseButtonMappingVm> MapButtonMapping(
ICollection<ButtonMapping> buttonMappings
) => buttonMappings.Select(ButtonMappingMapper.Map).ToList();

private static ICollection<ButtonMapping> MapButtonMappingVms(
List<BaseButtonMappingVm> buttonMappings
) => buttonMappings.Select(ButtonMappingMapper.Map).ToList();
}
24 changes: 24 additions & 0 deletions YMouseButtonControl.Core/Mappings/SettingMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Riok.Mapperly.Abstractions;
using YMouseButtonControl.Core.ViewModels.Models;
using YMouseButtonControl.DataAccess.Models;

namespace YMouseButtonControl.Core.Mappings;

[Mapper]
public static partial class SettingMapper
{
[MapDerivedType<SettingString, SettingStringVm>]
[MapDerivedType<SettingBool, SettingBoolVm>]
[MapDerivedType<SettingInt, SettingIntVm>]
public static partial BaseSettingVm Map(Setting? setting);

[MapDerivedType<SettingStringVm, SettingString>]
[MapDerivedType<SettingBoolVm, SettingBool>]
[MapDerivedType<SettingIntVm, SettingInt>]
public static partial Setting Map(BaseSettingVm baseSettingVm);

[MapDerivedType<SettingStringVm, SettingString>]
[MapDerivedType<SettingBoolVm, SettingBool>]
[MapDerivedType<SettingIntVm, SettingInt>]
public static partial void Map(BaseSettingVm src, Setting dst);
}
99 changes: 99 additions & 0 deletions YMouseButtonControl.Core/Repositories/ButtonMappingRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using YMouseButtonControl.Core.Mappings;
using YMouseButtonControl.Core.ViewModels.Models;
using YMouseButtonControl.DataAccess.Context;
using YMouseButtonControl.DataAccess.Models;
using YMouseButtonControl.DataAccess.Queries;

namespace YMouseButtonControl.Core.Repositories;

public class ButtonMappingRepository(YMouseButtonControlDbContext ctx, ButtonMappingQueries queries)
: IRepository<ButtonMapping, BaseButtonMappingVm>
{
private readonly YMouseButtonControlDbContext _ctx = ctx;
private const string TblName = "ButtonMappings";

public int Add(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
var ent = ButtonMappingMapper.Map(vm);
ent.ButtonMappingType = ent switch
{
DisabledMapping => ButtonMappingType.Disabled,
NothingMapping => ButtonMappingType.Nothing,
SimulatedKeystroke => ButtonMappingType.SimulatedKeystroke,
RightClick => ButtonMappingType.RightClick,
_ => throw new System.NotImplementedException(),
};
return conn.Execute(queries.Add(), ent);
}

public async Task<int> AddAsync(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
return await conn.ExecuteAsync(queries.Add(), vm);
}

public BaseButtonMappingVm? GetById(int id)
{
using var conn = _ctx.CreateConnection();
return ButtonMappingMapper.Map(
conn.QueryFirstOrDefault<ButtonMapping>(queries.GetById(TblName), id)
);
}

public async Task<BaseButtonMappingVm?> GetByIdAsync(int id)
{
using var conn = _ctx.CreateConnection();
return ButtonMappingMapper.Map(
await conn.QueryFirstOrDefaultAsync<ButtonMapping>(queries.GetById(TblName), id)
);
}

public IEnumerable<BaseButtonMappingVm> GetAll()
{
using var conn = _ctx.CreateConnection();
return conn.Query<ButtonMapping>(queries.GetAll(TblName)).Select(ButtonMappingMapper.Map);
}

public async Task<IEnumerable<BaseButtonMappingVm>> GetAllAsync()
{
using var conn = _ctx.CreateConnection();
return (await conn.QueryAsync<ButtonMapping>(queries.GetAll(TblName))).Select(
ButtonMappingMapper.Map
);
}

public int Update(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
return conn.Execute(queries.Update(), vm);
}

public async Task<int> UpdateAsync(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
return await conn.ExecuteAsync(queries.Update(), vm);
}

public int Delete(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
return conn.Execute(queries.DeleteById(TblName), vm.Id);
}

public Task<int> DeleteAsync(BaseButtonMappingVm vm)
{
using var conn = _ctx.CreateConnection();
return conn.ExecuteAsync(queries.DeleteById(TblName), vm.Id);
}

public BaseButtonMappingVm? GetByName(string name)
{
throw new System.NotImplementedException();
}
}
Loading

0 comments on commit ff0221d

Please sign in to comment.