Skip to content

Commit ff0221d

Browse files
authored
Merge pull request #27 from FaithBeam/mapperly
Mapperly
2 parents bfc2201 + a8cc1aa commit ff0221d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+881
-1835
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using Riok.Mapperly.Abstractions;
3+
using YMouseButtonControl.Core.ViewModels.Models;
4+
using YMouseButtonControl.DataAccess.Models;
5+
6+
namespace YMouseButtonControl.Core.Mappings;
7+
8+
[Mapper]
9+
public static partial class ButtonMappingMapper
10+
{
11+
[MapDerivedType<DisabledMapping, DisabledMappingVm>]
12+
[MapDerivedType<NothingMapping, NothingMappingVm>]
13+
[MapDerivedType<SimulatedKeystroke, SimulatedKeystrokeVm>]
14+
[MapDerivedType<RightClick, RightClickVm>]
15+
public static partial BaseButtonMappingVm Map(ButtonMapping? buttonMapping);
16+
17+
[MapDerivedType<DisabledMappingVm, DisabledMapping>]
18+
[MapDerivedType<NothingMappingVm, NothingMapping>]
19+
[MapDerivedType<SimulatedKeystrokeVm, SimulatedKeystroke>]
20+
[MapDerivedType<RightClickVm, RightClick>]
21+
public static partial ButtonMapping Map(BaseButtonMappingVm buttonMapping);
22+
23+
[MapDerivedType<DisabledMappingVm, DisabledMapping>]
24+
[MapDerivedType<NothingMappingVm, NothingMapping>]
25+
[MapDerivedType<SimulatedKeystrokeVm, SimulatedKeystroke>]
26+
[MapDerivedType<RightClickVm, RightClick>]
27+
public static partial void Map(BaseButtonMappingVm src, ButtonMapping dst);
28+
29+
public static BaseSimulatedKeystrokeTypeVm MapSimulatedKeystrokeType(
30+
SimulatedKeystrokeType simulatedKeystrokeType
31+
) =>
32+
simulatedKeystrokeType switch
33+
{
34+
SimulatedKeystrokeType.AsMousePressedAndReleasedActionType =>
35+
new AsMousePressedAndReleasedActionTypeVm(),
36+
SimulatedKeystrokeType.DuringMouseActionType => new DuringMouseActionTypeVm(),
37+
SimulatedKeystrokeType.InAnotherThreadPressedActionType =>
38+
new InAnotherThreadPressedActionTypeVm(),
39+
SimulatedKeystrokeType.InAnotherThreadReleasedActionType =>
40+
new InAnotherThreadReleasedActionTypeVm(),
41+
SimulatedKeystrokeType.MouseButtonPressedActionType =>
42+
new MouseButtonPressedActionTypeVm(),
43+
SimulatedKeystrokeType.MouseButtonReleasedActionType =>
44+
new MouseButtonReleasedActionTypeVm(),
45+
SimulatedKeystrokeType.RepeatedlyWhileButtonDownActionType =>
46+
new RepeatedlyWhileButtonDownActionTypeVm(),
47+
SimulatedKeystrokeType.StickyHoldActionType => new StickyHoldActionTypeVm(),
48+
SimulatedKeystrokeType.StickyRepeatActionType => new StickyRepeatActionTypeVm(),
49+
_ => throw new ArgumentOutOfRangeException(
50+
nameof(simulatedKeystrokeType),
51+
simulatedKeystrokeType,
52+
null
53+
),
54+
};
55+
56+
public static SimulatedKeystrokeType? MapSimulatedKeystrokeTypeVm(
57+
BaseSimulatedKeystrokeTypeVm? baseSimulatedKeystrokeVm
58+
) =>
59+
baseSimulatedKeystrokeVm switch
60+
{
61+
null => null,
62+
AsMousePressedAndReleasedActionTypeVm =>
63+
SimulatedKeystrokeType.AsMousePressedAndReleasedActionType,
64+
DuringMouseActionTypeVm => SimulatedKeystrokeType.DuringMouseActionType,
65+
InAnotherThreadPressedActionTypeVm =>
66+
SimulatedKeystrokeType.InAnotherThreadPressedActionType,
67+
InAnotherThreadReleasedActionTypeVm =>
68+
SimulatedKeystrokeType.InAnotherThreadReleasedActionType,
69+
MouseButtonPressedActionTypeVm => SimulatedKeystrokeType.MouseButtonPressedActionType,
70+
MouseButtonReleasedActionTypeVm => SimulatedKeystrokeType.MouseButtonReleasedActionType,
71+
RepeatedlyWhileButtonDownActionTypeVm =>
72+
SimulatedKeystrokeType.RepeatedlyWhileButtonDownActionType,
73+
StickyHoldActionTypeVm => SimulatedKeystrokeType.StickyHoldActionType,
74+
StickyRepeatActionTypeVm => SimulatedKeystrokeType.StickyRepeatActionType,
75+
_ => throw new ArgumentOutOfRangeException(),
76+
};
77+
}

YMouseButtonControl.Core/Mappings/MappingProfile.cs

Lines changed: 0 additions & 151 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Riok.Mapperly.Abstractions;
4+
using YMouseButtonControl.Core.ViewModels.Models;
5+
using YMouseButtonControl.DataAccess.Models;
6+
7+
namespace YMouseButtonControl.Core.Mappings;
8+
9+
[Mapper]
10+
public static partial class ProfileMapper
11+
{
12+
public static partial ProfileVm Map(Profile? profile);
13+
14+
public static partial Profile Map(ProfileVm vm);
15+
16+
public static partial void Map(ProfileVm src, Profile dst);
17+
18+
private static List<BaseButtonMappingVm> MapButtonMapping(
19+
ICollection<ButtonMapping> buttonMappings
20+
) => buttonMappings.Select(ButtonMappingMapper.Map).ToList();
21+
22+
private static ICollection<ButtonMapping> MapButtonMappingVms(
23+
List<BaseButtonMappingVm> buttonMappings
24+
) => buttonMappings.Select(ButtonMappingMapper.Map).ToList();
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Riok.Mapperly.Abstractions;
2+
using YMouseButtonControl.Core.ViewModels.Models;
3+
using YMouseButtonControl.DataAccess.Models;
4+
5+
namespace YMouseButtonControl.Core.Mappings;
6+
7+
[Mapper]
8+
public static partial class SettingMapper
9+
{
10+
[MapDerivedType<SettingString, SettingStringVm>]
11+
[MapDerivedType<SettingBool, SettingBoolVm>]
12+
[MapDerivedType<SettingInt, SettingIntVm>]
13+
public static partial BaseSettingVm Map(Setting? setting);
14+
15+
[MapDerivedType<SettingStringVm, SettingString>]
16+
[MapDerivedType<SettingBoolVm, SettingBool>]
17+
[MapDerivedType<SettingIntVm, SettingInt>]
18+
public static partial Setting Map(BaseSettingVm baseSettingVm);
19+
20+
[MapDerivedType<SettingStringVm, SettingString>]
21+
[MapDerivedType<SettingBoolVm, SettingBool>]
22+
[MapDerivedType<SettingIntVm, SettingInt>]
23+
public static partial void Map(BaseSettingVm src, Setting dst);
24+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Dapper;
6+
using YMouseButtonControl.Core.Mappings;
7+
using YMouseButtonControl.Core.ViewModels.Models;
8+
using YMouseButtonControl.DataAccess.Context;
9+
using YMouseButtonControl.DataAccess.Models;
10+
using YMouseButtonControl.DataAccess.Queries;
11+
12+
namespace YMouseButtonControl.Core.Repositories;
13+
14+
public class ButtonMappingRepository(YMouseButtonControlDbContext ctx, ButtonMappingQueries queries)
15+
: IRepository<ButtonMapping, BaseButtonMappingVm>
16+
{
17+
private readonly YMouseButtonControlDbContext _ctx = ctx;
18+
private const string TblName = "ButtonMappings";
19+
20+
public int Add(BaseButtonMappingVm vm)
21+
{
22+
using var conn = _ctx.CreateConnection();
23+
var ent = ButtonMappingMapper.Map(vm);
24+
ent.ButtonMappingType = ent switch
25+
{
26+
DisabledMapping => ButtonMappingType.Disabled,
27+
NothingMapping => ButtonMappingType.Nothing,
28+
SimulatedKeystroke => ButtonMappingType.SimulatedKeystroke,
29+
RightClick => ButtonMappingType.RightClick,
30+
_ => throw new System.NotImplementedException(),
31+
};
32+
return conn.Execute(queries.Add(), ent);
33+
}
34+
35+
public async Task<int> AddAsync(BaseButtonMappingVm vm)
36+
{
37+
using var conn = _ctx.CreateConnection();
38+
return await conn.ExecuteAsync(queries.Add(), vm);
39+
}
40+
41+
public BaseButtonMappingVm? GetById(int id)
42+
{
43+
using var conn = _ctx.CreateConnection();
44+
return ButtonMappingMapper.Map(
45+
conn.QueryFirstOrDefault<ButtonMapping>(queries.GetById(TblName), id)
46+
);
47+
}
48+
49+
public async Task<BaseButtonMappingVm?> GetByIdAsync(int id)
50+
{
51+
using var conn = _ctx.CreateConnection();
52+
return ButtonMappingMapper.Map(
53+
await conn.QueryFirstOrDefaultAsync<ButtonMapping>(queries.GetById(TblName), id)
54+
);
55+
}
56+
57+
public IEnumerable<BaseButtonMappingVm> GetAll()
58+
{
59+
using var conn = _ctx.CreateConnection();
60+
return conn.Query<ButtonMapping>(queries.GetAll(TblName)).Select(ButtonMappingMapper.Map);
61+
}
62+
63+
public async Task<IEnumerable<BaseButtonMappingVm>> GetAllAsync()
64+
{
65+
using var conn = _ctx.CreateConnection();
66+
return (await conn.QueryAsync<ButtonMapping>(queries.GetAll(TblName))).Select(
67+
ButtonMappingMapper.Map
68+
);
69+
}
70+
71+
public int Update(BaseButtonMappingVm vm)
72+
{
73+
using var conn = _ctx.CreateConnection();
74+
return conn.Execute(queries.Update(), vm);
75+
}
76+
77+
public async Task<int> UpdateAsync(BaseButtonMappingVm vm)
78+
{
79+
using var conn = _ctx.CreateConnection();
80+
return await conn.ExecuteAsync(queries.Update(), vm);
81+
}
82+
83+
public int Delete(BaseButtonMappingVm vm)
84+
{
85+
using var conn = _ctx.CreateConnection();
86+
return conn.Execute(queries.DeleteById(TblName), vm.Id);
87+
}
88+
89+
public Task<int> DeleteAsync(BaseButtonMappingVm vm)
90+
{
91+
using var conn = _ctx.CreateConnection();
92+
return conn.ExecuteAsync(queries.DeleteById(TblName), vm.Id);
93+
}
94+
95+
public BaseButtonMappingVm? GetByName(string name)
96+
{
97+
throw new System.NotImplementedException();
98+
}
99+
}

0 commit comments

Comments
 (0)