ScientificReport
PostgraduateGuidanceController.cs
Go to the documentation of this file.
1 using System;
2 using Microsoft.AspNetCore.Authorization;
3 using Microsoft.AspNetCore.Mvc;
9 
10 namespace ScientificReport.Controllers
11 {
12  [Authorize(Roles = UserProfileRole.Any)]
13  public class PostgraduateGuidanceController : Controller
14  {
15  private readonly IPostgraduateGuidanceService _postgraduateGuidanceService;
16  private readonly IUserProfileService _userProfileService;
17  private readonly IDepartmentService _departmentService;
18 
20  IPostgraduateGuidanceService postgraduateGuidanceService,
21  IUserProfileService userProfileService,
22  IDepartmentService departmentService
23  )
24  {
25  _postgraduateGuidanceService = postgraduateGuidanceService;
26  _userProfileService = userProfileService;
27  _departmentService = departmentService;
28  }
29 
30  // GET: PostgraduateGuidance
31  public IActionResult Index(PostgraduateGuidanceIndexModel model)
32  {
33  model.PostgraduateGuidances = _postgraduateGuidanceService.GetPageByRole(model.CurrentPage, model.PageSize, User);
34  model.Count = _postgraduateGuidanceService.GetCountByRole(User);
35  return View(model);
36  }
37 
38  // GET: PostgraduateGuidance/Details/{id}
39  public IActionResult Details(Guid? id)
40  {
41  if (id == null)
42  {
43  return NotFound();
44  }
45 
46  var postgraduateGuidance = _postgraduateGuidanceService.GetById(id.Value);
47  if (postgraduateGuidance == null)
48  {
49  return NotFound();
50  }
51 
52  if (!UserHasPermission(postgraduateGuidance))
53  {
54  return Forbid();
55  }
56 
57  return View(postgraduateGuidance);
58  }
59 
60  // GET: PostgraduateGuidance/Create
61  public IActionResult Create() => View();
62 
63  // POST: PostgraduateGuidance/Create
64  [HttpPost]
65  [ValidateAntiForgeryToken]
66  public IActionResult Create(PostgraduateGuidanceModel model)
67  {
68  if (!ModelState.IsValid)
69  {
70  return View(model);
71  }
72 
73  model.Guide = _userProfileService.Get(User);
74  _postgraduateGuidanceService.CreateItem(model);
75  return RedirectToAction(nameof(Index));
76  }
77 
78  // GET: PostgraduateGuidance/Edit/{id}
79  public IActionResult Edit(Guid? id)
80  {
81  if (id == null)
82  {
83  return NotFound();
84  }
85 
86  var postgraduateGuidance = _postgraduateGuidanceService.GetById(id.Value);
87  if (postgraduateGuidance == null)
88  {
89  return NotFound();
90  }
91 
92  if (!UserHasPermission(postgraduateGuidance))
93  {
94  return Forbid();
95  }
96 
97  return View(new PostgraduateGuidanceEditModel(postgraduateGuidance));
98  }
99 
100  // POST: PostgraduateGuidance/Edit/{id}
101  [HttpPost]
102  [ValidateAntiForgeryToken]
103  public IActionResult Edit(Guid id, PostgraduateGuidanceEditModel model)
104  {
105  if (id != model.Id || !_postgraduateGuidanceService.Exists(id))
106  {
107  return NotFound();
108  }
109 
110  if (!UserHasPermission(_postgraduateGuidanceService.GetById(id)))
111  {
112  return Forbid();
113  }
114 
115  if (!ModelState.IsValid)
116  {
117  return View(model);
118  }
119 
120  _postgraduateGuidanceService.UpdateItem(model);
121  return RedirectToAction(nameof(Index));
122  }
123 
124  // GET: PostgraduateGuidance/Delete/{id}
125  public IActionResult Delete(Guid? id)
126  {
127  if (id == null)
128  {
129  return NotFound();
130  }
131 
132  var postgraduateGuidance = _postgraduateGuidanceService.GetById(id.Value);
133  if (postgraduateGuidance == null)
134  {
135  return NotFound();
136  }
137 
138  if (!UserHasPermission(postgraduateGuidance))
139  {
140  return Forbid();
141  }
142 
143  return View(postgraduateGuidance);
144  }
145 
146  // POST: PostgraduateGuidance/Delete/{id}
147  [HttpPost, ActionName("Delete")]
148  [ValidateAntiForgeryToken]
149  public IActionResult DeleteConfirmed(Guid id)
150  {
151  if (!_postgraduateGuidanceService.Exists(id))
152  {
153  return NotFound();
154  }
155 
156  if (!UserHasPermission(_postgraduateGuidanceService.GetById(id)))
157  {
158  return Forbid();
159  }
160 
161  _postgraduateGuidanceService.DeleteById(id);
162  return RedirectToAction(nameof(Index));
163  }
164 
165  private bool UserHasPermission(PostgraduateGuidance guidance)
166  {
167  var user = _userProfileService.Get(User);
168  var department = _departmentService.Get(d => d.Staff.Contains(user));
169  return PageHelpers.IsAdmin(User) ||
170  PageHelpers.IsHeadOfDepartment(User) &&
171  department.Staff.Contains(guidance.Guide) ||
172  guidance.Guide.Id == user.Id;
173  }
174  }
175 }
IActionResult Edit(Guid id, PostgraduateGuidanceEditModel model)
IActionResult Index(PostgraduateGuidanceIndexModel model)
PostgraduateGuidanceController(IPostgraduateGuidanceService postgraduateGuidanceService, IUserProfileService userProfileService, IDepartmentService departmentService)