Skip to content

Commit 1828e93

Browse files
committed
Split SubscribeListViewModel to separate files
1 parent c24f125 commit 1828e93

File tree

3 files changed

+100
-94
lines changed

3 files changed

+100
-94
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using JoinRpg.Data.Interfaces;
5+
using JoinRpg.Data.Interfaces.Subscribe;
6+
using JoinRpg.DataModel;
7+
using JoinRpg.Services.Interfaces;
8+
9+
namespace JoinRpg.Web.Models.Subscribe
10+
{
11+
public static class Builders
12+
{
13+
public static SubscribeListViewModel ToSubscribeListViewModel(
14+
this (User User, UserSubscriptionDto[] UserSubscriptions) data,
15+
User currentUser,
16+
IUriService uriService,
17+
int projectId,
18+
List<PaymentTypeDto> paymentTypes)
19+
{
20+
return new SubscribeListViewModel()
21+
{
22+
User = new UserProfileDetailsViewModel(data.User, currentUser),
23+
AllowChanges = data.User == currentUser, //TODO allow project admins to setup subscribe for other masters
24+
Items = data.UserSubscriptions.Select(x => x.ToViewModel(uriService)).ToArray(),
25+
ProjectId = projectId,
26+
PaymentTypeNames = paymentTypes.Select(pt => pt.ToPaymentTypeName()).ToArray(),
27+
};
28+
}
29+
30+
private static string ToPaymentTypeName(this PaymentTypeDto dto)
31+
{
32+
return dto.TypeKind switch
33+
{
34+
PaymentTypeKind.Custom => dto.Name,
35+
PaymentTypeKind.Cash => "Наличные",
36+
PaymentTypeKind.Online => "Онлайн",
37+
_ => throw new ArgumentOutOfRangeException("dto.TypeKind", (object)dto.TypeKind, "Wrong type"),
38+
};
39+
}
40+
41+
private class AbstractLinkable : ISubscribeTarget
42+
{
43+
public LinkType LinkType { get; set; }
44+
45+
public string Identification { get; set; }
46+
47+
public int? ProjectId { get; set; }
48+
49+
public string Name { get; set; }
50+
}
51+
52+
public static SubscribeListItemViewModel ToViewModel(this UserSubscriptionDto dto, IUriService uriService)
53+
{
54+
var link = dto.ToSubscribeTargetLink();
55+
return new SubscribeListItemViewModel
56+
{
57+
Name = link.Name,
58+
Link = uriService.GetUri(link),
59+
UserSubscriptionId = dto.UserSubscriptionId,
60+
Options = new SubscribeOptionsViewModel()
61+
{
62+
AccommodationChange = dto.Options.AccommodationChange,
63+
ClaimStatusChange = dto.Options.ClaimStatusChange,
64+
Comments = dto.Options.Comments,
65+
FieldChange = dto.Options.FieldChange,
66+
MoneyOperation = dto.Options.MoneyOperation,
67+
},
68+
};
69+
}
70+
71+
public static ISubscribeTarget ToSubscribeTargetLink(this UserSubscriptionDto dto)
72+
{
73+
(LinkType type, string name, int id) link = dto switch
74+
{
75+
{ CharacterId: int id, CharacterNames: string name } => (LinkType.ResultCharacter, name, id),
76+
{ CharacterGroupId: int id, CharacterGroupName: string name } => (LinkType.ResultCharacterGroup, name, id),
77+
{ ClaimId: int id, ClaimName: string name } => (LinkType.Claim, name, id),
78+
_ => throw new InvalidOperationException(),
79+
};
80+
81+
return new AbstractLinkable
82+
{
83+
LinkType = link.type,
84+
Identification = link.id.ToString(),
85+
ProjectId = dto.ProjectId,
86+
Name = link.name,
87+
};
88+
}
89+
90+
}
91+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using JoinRpg.DataModel;
2+
3+
namespace JoinRpg.Web.Models.Subscribe
4+
{
5+
public interface ISubscribeTarget : ILinkable
6+
{
7+
public string Name { get; }
8+
}
9+
}
Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using JoinRpg.Data.Interfaces;
5-
using JoinRpg.Data.Interfaces.Subscribe;
6-
using JoinRpg.DataModel;
7-
using JoinRpg.Services.Interfaces;
8-
91
namespace JoinRpg.Web.Models.Subscribe
102
{
113
public class SubscribeListViewModel
@@ -19,90 +11,4 @@ public class SubscribeListViewModel
1911

2012
public int ProjectId { get; set; }
2113
}
22-
23-
public interface ISubscribeTarget : ILinkable
24-
{
25-
public string Name { get; }
26-
}
27-
28-
public static class Builders
29-
{
30-
public static SubscribeListViewModel ToSubscribeListViewModel(
31-
this (User User, UserSubscriptionDto[] UserSubscriptions) data,
32-
User currentUser,
33-
IUriService uriService,
34-
int projectId,
35-
List<PaymentTypeDto> paymentTypes)
36-
{
37-
return new SubscribeListViewModel()
38-
{
39-
User = new UserProfileDetailsViewModel(data.User, currentUser),
40-
AllowChanges = data.User == currentUser, //TODO allow project admins to setup subscribe for other masters
41-
Items = data.UserSubscriptions.Select(x => x.ToViewModel(uriService)).ToArray(),
42-
ProjectId = projectId,
43-
PaymentTypeNames = paymentTypes.Select(pt => pt.ToPaymentTypeName()).ToArray(),
44-
};
45-
}
46-
47-
private static string ToPaymentTypeName(this PaymentTypeDto dto)
48-
{
49-
return dto.TypeKind switch
50-
{
51-
PaymentTypeKind.Custom => dto.Name,
52-
PaymentTypeKind.Cash => "Наличные",
53-
PaymentTypeKind.Online => "Онлайн",
54-
_ => throw new ArgumentOutOfRangeException("dto.TypeKind", (object)dto.TypeKind, "Wrong type"),
55-
};
56-
}
57-
58-
private class AbstractLinkable : ISubscribeTarget
59-
{
60-
public LinkType LinkType { get; set; }
61-
62-
public string Identification { get; set; }
63-
64-
public int? ProjectId { get; set; }
65-
66-
public string Name { get; set; }
67-
}
68-
69-
public static SubscribeListItemViewModel ToViewModel(this UserSubscriptionDto dto, IUriService uriService)
70-
{
71-
var link = dto.ToSubscribeTargetLink();
72-
return new SubscribeListItemViewModel
73-
{
74-
Name = link.Name,
75-
Link = uriService.GetUri(link),
76-
UserSubscriptionId = dto.UserSubscriptionId,
77-
Options = new SubscribeOptionsViewModel()
78-
{
79-
AccommodationChange = dto.Options.AccommodationChange,
80-
ClaimStatusChange = dto.Options.ClaimStatusChange,
81-
Comments = dto.Options.Comments,
82-
FieldChange = dto.Options.FieldChange,
83-
MoneyOperation = dto.Options.MoneyOperation,
84-
},
85-
};
86-
}
87-
88-
public static ISubscribeTarget ToSubscribeTargetLink(this UserSubscriptionDto dto)
89-
{
90-
(LinkType type, string name, int id) link = dto switch
91-
{
92-
{ CharacterId: int id, CharacterNames: string name } => (LinkType.ResultCharacter, name, id),
93-
{ CharacterGroupId: int id, CharacterGroupName: string name } => (LinkType.ResultCharacterGroup, name, id),
94-
{ ClaimId: int id, ClaimName: string name } => (LinkType.Claim, name, id),
95-
_ => throw new InvalidOperationException(),
96-
};
97-
98-
return new AbstractLinkable
99-
{
100-
LinkType = link.type,
101-
Identification = link.id.ToString(),
102-
ProjectId = dto.ProjectId,
103-
Name = link.name,
104-
};
105-
}
106-
107-
}
10814
}

0 commit comments

Comments
 (0)