ScientificReport
ScientificInternshipService.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 {
16  {
17  private readonly ScientificInternshipRepository _scientificInternshipRepository;
18  private readonly DepartmentRepository _departmentRepository;
19  private readonly UserProfileRepository _userProfileRepository;
20 
22  {
23  _scientificInternshipRepository = new ScientificInternshipRepository(context);
24  _departmentRepository = new DepartmentRepository(context);
25  _userProfileRepository = new UserProfileRepository(context);
26  }
27 
28  public virtual IEnumerable<ScientificInternship> GetAll()
29  {
30  return _scientificInternshipRepository.All();
31  }
32 
33  public virtual IEnumerable<ScientificInternship> GetAllWhere(Func<ScientificInternship, bool> predicate)
34  {
35  return GetAll().Where(predicate);
36  }
37 
38  public virtual IEnumerable<ScientificInternship> GetItemsByRole(ClaimsPrincipal userClaims)
39  {
40  IEnumerable<ScientificInternship> items;
41  if (UserHelpers.IsAdmin(userClaims))
42  {
43  items = _scientificInternshipRepository.All();
44  }
45  else if (UserHelpers.IsHeadOfDepartment(userClaims))
46  {
47  var department = _departmentRepository.Get(r => r.Head.UserName == userClaims.Identity.Name);
48  items = _scientificInternshipRepository.AllWhere(m => m.UserProfilesScientificInternships.Any(p => department.Staff.Contains(p.UserProfile)));
49  }
50  else
51  {
52  var user = _userProfileRepository.Get(u => u.UserName == userClaims.Identity.Name);
53  items = _scientificInternshipRepository.AllWhere(m => m.UserProfilesScientificInternships.Any(p => p.UserProfile.Id == user.Id));
54  }
55 
56  return items;
57  }
58 
59  public virtual IEnumerable<ScientificInternship> GetPageByRole(int page, int count, ClaimsPrincipal userClaims)
60  {
61  return GetItemsByRole(userClaims).Skip((page - 1) * count).Take(count).ToList();
62  }
63 
64  public virtual int GetCountByRole(ClaimsPrincipal userClaims)
65  {
66  return GetItemsByRole(userClaims).Count();
67  }
68 
69  public virtual ScientificInternship GetById(Guid id)
70  {
71  return _scientificInternshipRepository.Get(id);
72  }
73 
74  public virtual ScientificInternship Get(Func<ScientificInternship, bool> predicate)
75  {
76  return _scientificInternshipRepository.Get(predicate);
77  }
78 
79  public virtual void CreateItem(ScientificInternshipModel model)
80  {
81  _scientificInternshipRepository.Create(new ScientificInternship
82  {
83  PlaceOfInternship = model.PlaceOfInternship,
84  Contents = model.Contents,
85  Started = model.Started,
86  Ended = model.Ended
87  });
88  }
89 
90  public virtual void UpdateItem(ScientificInternshipEditModel model)
91  {
92  var scientificInternship = GetById(model.Id);
93  if (scientificInternship == null)
94  {
95  return;
96  }
97 
98  scientificInternship.PlaceOfInternship = model.PlaceOfInternship;
99  scientificInternship.Contents = model.Contents;
100  scientificInternship.Started = model.Started;
101  scientificInternship.Ended = model.Ended;
102  _scientificInternshipRepository.Update(scientificInternship);
103  }
104 
105  public virtual void DeleteById(Guid id)
106  {
107  _scientificInternshipRepository.Delete(id);
108  }
109 
110  public virtual bool Exists(Guid id)
111  {
112  return _scientificInternshipRepository.Get(id) != null;
113  }
114 
115  public void AddUser(ScientificInternship scientificInternship, UserProfile user)
116  {
117  if (scientificInternship == null || user == null)
118  {
119  return;
120  }
121 
123  {
124  UserProfile = user,
125  UserProfileId = user.Id,
126  ScientificInternship = scientificInternship,
127  ScientificInternshipId = scientificInternship.Id
128  });
129  _scientificInternshipRepository.Update(scientificInternship);
130  }
131 
132  public void RemoveUser(ScientificInternship scientificInternship, UserProfile user)
133  {
134  if (scientificInternship == null || user == null)
135  {
136  return;
137  }
138 
139  scientificInternship.UserProfilesScientificInternships.Remove(
140  scientificInternship.UserProfilesScientificInternships.First(p => p.UserProfile.Id == user.Id));
141  _scientificInternshipRepository.Update(scientificInternship);
142  }
143 
144  public virtual IEnumerable<UserProfile> GetUsers(Guid id)
145  {
146  var scientificInternship = _scientificInternshipRepository.Get(id);
147  IEnumerable<UserProfile> users = null;
148  if (scientificInternship != null)
149  {
150  users = scientificInternship.UserProfilesScientificInternships.Select(u => u.UserProfile);
151  }
152 
153  return users;
154  }
155  }
156 }
virtual void CreateItem(ScientificInternshipModel model)
virtual IEnumerable< ScientificInternship > GetPageByRole(int page, int count, ClaimsPrincipal userClaims)
void AddUser(ScientificInternship scientificInternship, UserProfile user)
virtual ScientificInternship Get(Func< ScientificInternship, bool > predicate)
virtual ICollection< UserProfilesScientificInternships > UserProfilesScientificInternships
virtual IEnumerable< ScientificInternship > GetItemsByRole(ClaimsPrincipal userClaims)
static bool IsHeadOfDepartment(ClaimsPrincipal user)
Definition: UserHelpers.cs:13
static bool IsAdmin(ClaimsPrincipal user)
Definition: UserHelpers.cs:8
void RemoveUser(ScientificInternship scientificInternship, UserProfile user)
virtual IEnumerable< ScientificInternship > GetAllWhere(Func< ScientificInternship, bool > predicate)
virtual void UpdateItem(ScientificInternshipEditModel model)