ScientificReport
ScientificInternshipController.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using Microsoft.AspNetCore.Authorization;
4 using Microsoft.AspNetCore.Mvc;
11 
12 namespace ScientificReport.Controllers
13 {
14  [Authorize(Roles = UserProfileRole.Any)]
15  public class ScientificInternshipController : Controller
16  {
17  private readonly IScientificInternshipService _scientificInternshipService;
18  private readonly IUserProfileService _userProfileService;
19  private readonly IDepartmentService _departmentService;
20 
22  IScientificInternshipService scientificInternshipService,
23  IUserProfileService userProfileService,
24  IDepartmentService departmentService
25  )
26  {
27  _scientificInternshipService = scientificInternshipService;
28  _userProfileService = userProfileService;
29  _departmentService = departmentService;
30  }
31 
32  // GET: ScientificInternship
33  public IActionResult Index(ScientificInternshipIndexModel model)
34  {
35  model.ScientificInternships = _scientificInternshipService.GetPageByRole(model.CurrentPage, model.PageSize, User);
36  model.Count = _scientificInternshipService.GetCountByRole(User);
37  return View(model);
38  }
39 
40  // GET: ScientificInternship/Details/{id}
41  public IActionResult Details(Guid? id)
42  {
43  if (id == null)
44  {
45  return NotFound();
46  }
47 
48  var scientificInternship = _scientificInternshipService.GetById(id.Value);
49  if (scientificInternship == null)
50  {
51  return NotFound();
52  }
53 
54  if (!UserHasPermission(scientificInternship))
55  {
56  return Forbid();
57  }
58 
59  return View(scientificInternship);
60  }
61 
62  // GET: ScientificInternship/Create
63  public IActionResult Create() => View();
64 
65  // POST: ScientificInternship/Create
66  [HttpPost]
67  [ValidateAntiForgeryToken]
68  public IActionResult Create(ScientificInternshipModel model)
69  {
70  if (!ModelState.IsValid)
71  {
72  return View(model);
73  }
74 
75  _scientificInternshipService.CreateItem(model);
76  _scientificInternshipService.AddUser(
77  _scientificInternshipService.Get(si =>
78  si.Contents == model.Contents && si.PlaceOfInternship == model.PlaceOfInternship),
79  _userProfileService.Get(User));
80 
81  return RedirectToAction(nameof(Index));
82  }
83 
84  // GET: ScientificInternship/Edit/{id}
85  public IActionResult Edit(Guid? id)
86  {
87  if (id == null)
88  {
89  return NotFound();
90  }
91 
92  var scientificInternship = _scientificInternshipService.GetById(id.Value);
93  if (scientificInternship == null)
94  {
95  return NotFound();
96  }
97 
98  if (!UserHasPermission(scientificInternship))
99  {
100  return Forbid();
101  }
102 
103  return View(new ScientificInternshipEditModel(scientificInternship)
104  {
105  Users = _scientificInternshipService.GetUsers(scientificInternship.Id),
106  AllUsers = _userProfileService.GetAll()
107  });
108  }
109 
110  // POST: ScientificInternship/Edit/{id}
111  [HttpPost]
112  [ValidateAntiForgeryToken]
113  public IActionResult Edit(Guid id, ScientificInternshipEditModel model)
114  {
115  if (id != model.Id || !_scientificInternshipService.Exists(id))
116  {
117  return NotFound();
118  }
119 
120  var scientificInternship = _scientificInternshipService.GetById(id);
121  if (!UserHasPermission(scientificInternship))
122  {
123  return Forbid();
124  }
125 
126  if (!ModelState.IsValid)
127  {
128  model.Users = _scientificInternshipService.GetUsers(scientificInternship.Id);
129  model.AllUsers = _userProfileService.GetAll();
130  return View(model);
131  }
132 
133  _scientificInternshipService.UpdateItem(model);
134  return RedirectToAction(nameof(Index));
135  }
136 
137  // GET: ScientificInternship/Delete/{id}
138  public IActionResult Delete(Guid? id)
139  {
140  if (id == null)
141  {
142  return NotFound();
143  }
144 
145  var scientificInternship = _scientificInternshipService.GetById(id.Value);
146  if (scientificInternship == null)
147  {
148  return NotFound();
149  }
150 
151  if (!UserHasPermission(scientificInternship))
152  {
153  return Forbid();
154  }
155 
156  return View(scientificInternship);
157  }
158 
159  // POST: ScientificInternship/Delete/{id}
160  [HttpPost, ActionName("Delete")]
161  [ValidateAntiForgeryToken]
162  public IActionResult DeleteConfirmed(Guid id)
163  {
164  if (!_scientificInternshipService.Exists(id))
165  {
166  return NotFound();
167  }
168 
169  if (!UserHasPermission(_scientificInternshipService.GetById(id)))
170  {
171  return Forbid();
172  }
173 
174  _scientificInternshipService.DeleteById(id);
175  return RedirectToAction(nameof(Index));
176  }
177 
178  // POST: ScientificInternship/AddUser/{scientificInternshipId}
179  [HttpPost]
180  public IActionResult AddUser(Guid? id, [FromBody] UpdateUserRequest request)
181  {
182  if (id == null)
183  {
184  return NotFound();
185  }
186 
187  var user = _userProfileService.GetById(request.UserId);
188  if (user == null)
189  {
190  return Json(ApiResponse.Fail);
191  }
192 
193  var scientificInternship = _scientificInternshipService.GetById(id.Value);
194  if (scientificInternship == null)
195  {
196  return NotFound();
197  }
198 
199  if (!UserHasPermission(scientificInternship))
200  {
201  return Json(ApiResponse.Fail);
202  }
203 
204  if (!_scientificInternshipService.GetUsers(scientificInternship.Id).Contains(user))
205  {
206  _scientificInternshipService.AddUser(scientificInternship, user);
207  }
208 
209  return Json(ApiResponse.Ok);
210  }
211 
212  // POST: ScientificInternship/RemoveUser/{scientificInternshipId}
213  [HttpPost]
214  public IActionResult RemoveUser(Guid? id, [FromBody] UpdateUserRequest request)
215  {
216  if (id == null)
217  {
218  return NotFound();
219  }
220 
221  var user = _userProfileService.GetById(request.UserId);
222  if (user == null)
223  {
224  return Json(ApiResponse.Fail);
225  }
226 
227  var publication = _scientificInternshipService.GetById(id.Value);
228  if (publication == null)
229  {
230  return NotFound();
231  }
232 
233  if (!UserHasPermission(publication))
234  {
235  return Json(ApiResponse.Fail);
236  }
237 
238  if (_scientificInternshipService.GetUsers(publication.Id).Contains(user))
239  {
240  _scientificInternshipService.RemoveUser(publication, user);
241  }
242 
243  return Json(ApiResponse.Ok);
244  }
245 
246  private bool UserHasPermission(ScientificInternship scientificInternship)
247  {
248  var user = _userProfileService.Get(User);
249  var department = _departmentService.Get(d => d.Staff.Contains(user));
250  return PageHelpers.IsAdmin(User) ||
251  PageHelpers.IsHeadOfDepartment(User) &&
252  scientificInternship.UserProfilesScientificInternships.Any(p => department.Staff.Contains(p.UserProfile)) ||
253  scientificInternship.UserProfilesScientificInternships.Any(p => p.UserProfile.Id == user.Id);
254  }
255  }
256 }
ScientificInternshipController(IScientificInternshipService scientificInternshipService, IUserProfileService userProfileService, IDepartmentService departmentService)
IActionResult Index(ScientificInternshipIndexModel model)
virtual ICollection< UserProfilesScientificInternships > UserProfilesScientificInternships
IActionResult RemoveUser(Guid?id, [FromBody] UpdateUserRequest request)
IActionResult Edit(Guid id, ScientificInternshipEditModel model)
IActionResult AddUser(Guid?id, [FromBody] UpdateUserRequest request)