-
Notifications
You must be signed in to change notification settings - Fork 7
/
UserService.cs
114 lines (74 loc) · 2.62 KB
/
UserService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using MvcPagedList.Core.Example.Domain;
using MvcPagedList.Core.Example.Enums;
using MvcPagedList.Core.Example.Service.Users.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcPagedList.Core.Example.Service.Users
{
public class UserService : IUserService
{
private List<User> users;
public UserService()
{
users = new List<User>();
}
/// <summary>
///
/// </summary>
public void Create(UserInput input)
{
users.Add(
new User
{
Id = input.Id,
Family = input.Family,
Name = input.Name,
AddDate = input.AddDate
});
}
/// <summary>
///
/// </summary>
public IEnumerable<UserOutput> Search(int page, int recordsPerPage, string term, SortBy sortBy, SortOrder sortOrder, out int pageSize, out int TotalItemCount)
{
var queryable = users.AsQueryable();
#region by term
if (!string.IsNullOrEmpty(term))
{
queryable = queryable.Where(c => c.Family.Contains(term) || c.Name.Contains(term));
}
#endregion
#region ordering
switch (sortBy)
{
case SortBy.AddDate:
queryable = sortOrder == SortOrder.Asc ? queryable.OrderBy(u => u.AddDate) : queryable.OrderByDescending(u => u.AddDate);
break;
case SortBy.DisplayName:
queryable = sortOrder == SortOrder.Asc ? queryable.OrderBy(u => u.Name).ThenBy(u => u.Family) : queryable.OrderByDescending(u => u.Name).ThenByDescending(u => u.Family);
break;
default:
break;
}
#endregion
#region calculate skipe count
TotalItemCount = queryable.Count();
pageSize = (int)Math.Ceiling((double)TotalItemCount / recordsPerPage);
page = page > pageSize || page < 1 ? 1 : page;
var skiped = (page - 1) * recordsPerPage;
#endregion
#region take records
queryable = queryable.Skip(skiped).Take(recordsPerPage);
#endregion
return queryable.Select(u => new UserOutput
{
Id = u.Id,
AddDate = u.AddDate.ToShortDateString(),
Name = u.Name,
Family = u.Family,
}).ToList();
}
}
}