ScientificReport
GrantService.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Security.Claims;
12 
13 namespace ScientificReport.BLL.Services
14 {
15  public class GrantService : IGrantService
16  {
17  private readonly GrantRepository _grantRepository;
18  private readonly DepartmentRepository _departmentRepository;
19  private readonly UserProfileRepository _userProfileRepository;
20 
22  {
23  _grantRepository = new GrantRepository(context);
24  _departmentRepository = new DepartmentRepository(context);
25  _userProfileRepository = new UserProfileRepository(context);
26  }
27 
28  public virtual IEnumerable<Grant> GetAll()
29  {
30  return _grantRepository.All();
31  }
32 
33  public virtual IEnumerable<Grant> GetAllWhere(Func<Grant, bool> predicate)
34  {
35  return GetAll().Where(predicate);
36  }
37 
38  public virtual IEnumerable<Grant> GetItemsByRole(ClaimsPrincipal userClaims)
39  {
40  IEnumerable<Grant> items;
41  if (UserHelpers.IsAdmin(userClaims))
42  {
43  items = _grantRepository.All();
44  }
45  else if (UserHelpers.IsHeadOfDepartment(userClaims))
46  {
47  var department = _departmentRepository.Get(r => r.Head.UserName == userClaims.Identity.Name);
48  items = _grantRepository.AllWhere(
49  a => a.UserProfilesGrants.Any(u => department.Staff.Contains(u.UserProfile))
50  );
51  }
52  else
53  {
54  var user = _userProfileRepository.Get(u => u.UserName == userClaims.Identity.Name);
55  items = _grantRepository.AllWhere(
56  a => a.UserProfilesGrants.Any(u => u.UserProfile.Id == user.Id)
57  );
58  }
59 
60  return items;
61  }
62 
63  public virtual IEnumerable<Grant> GetPageByRole(int page, int count, ClaimsPrincipal userClaims)
64  {
65  return GetItemsByRole(userClaims).Skip((page - 1) * count).Take(count).ToList();
66  }
67 
68  public virtual int GetCountByRole(ClaimsPrincipal userClaims)
69  {
70  return GetItemsByRole(userClaims).Count();
71  }
72 
73  public virtual Grant GetById(Guid id)
74  {
75  return _grantRepository.Get(id);
76  }
77 
78  public virtual Grant Get(Func<Grant, bool> predicate)
79  {
80  return _grantRepository.Get(predicate);
81  }
82 
83  public virtual void CreateItem(GrantModel model)
84  {
85  _grantRepository.Create(new Grant()
86  {
87  Info = model.Info
88  });
89  }
90 
91  public virtual void UpdateItem(GrantEditModel model)
92  {
93  var grant = GetById(model.Id);
94  if (grant == null)
95  {
96  return;
97  }
98 
99  grant.Info = model.Info;
100  _grantRepository.Update(grant);
101  }
102 
103  public virtual void DeleteById(Guid id)
104  {
105  _grantRepository.Delete(id);
106  }
107 
108  public virtual bool Exists(Guid id)
109  {
110  return _grantRepository.Get(id) != null;
111  }
112 
113  public void AddUser(Grant grant, UserProfile userProfile)
114  {
115  if (grant == null || userProfile == null)
116  {
117  return;
118  }
119 
121  {
122  Grant = grant,
123  GrantId = grant.Id,
124  UserProfile = userProfile,
125  UserProfileId = userProfile.Id
126  });
127  _grantRepository.Update(grant);
128  }
129 
130  public void RemoveUser(Grant grant, UserProfile userProfile)
131  {
132  if (grant == null || userProfile == null)
133  {
134  return;
135  }
136 
137  grant.UserProfilesGrants.Remove(grant.UserProfilesGrants.First(u => u.UserProfile.Id == userProfile.Id));
138  _grantRepository.Update(grant);
139  }
140 
141  public virtual IEnumerable<UserProfile> GetUsers(Guid id)
142  {
143  var grant = _grantRepository.Get(id);
144  IEnumerable<UserProfile> users = null;
145  if (grant != null)
146  {
147  users = grant.UserProfilesGrants.Select(u => u.UserProfile);
148  }
149 
150  return users;
151  }
152  }
153 }
virtual void CreateItem(GrantModel model)
Definition: GrantService.cs:83
virtual int GetCountByRole(ClaimsPrincipal userClaims)
Definition: GrantService.cs:68
virtual IEnumerable< Grant > GetAll()
Definition: GrantService.cs:28
virtual IEnumerable< Grant > GetAllWhere(Func< Grant, bool > predicate)
Definition: GrantService.cs:33
virtual IEnumerable< Grant > GetPageByRole(int page, int count, ClaimsPrincipal userClaims)
Definition: GrantService.cs:63
static bool IsHeadOfDepartment(ClaimsPrincipal user)
Definition: UserHelpers.cs:13
static bool IsAdmin(ClaimsPrincipal user)
Definition: UserHelpers.cs:8
GrantService(ScientificReportDbContext context)
Definition: GrantService.cs:21
virtual void UpdateItem(GrantEditModel model)
Definition: GrantService.cs:91
virtual Grant Get(Func< Grant, bool > predicate)
Definition: GrantService.cs:78
virtual ICollection< UserProfilesGrants > UserProfilesGrants
Definition: Grant.cs:15
virtual IEnumerable< UserProfile > GetUsers(Guid id)
void AddUser(Grant grant, UserProfile userProfile)
void RemoveUser(Grant grant, UserProfile userProfile)
virtual IEnumerable< Grant > GetItemsByRole(ClaimsPrincipal userClaims)
Definition: GrantService.cs:38