ScientificReport
ReviewController.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 ReviewController : Controller
14  {
15  private readonly IReviewService _reviewService;
16  private readonly IUserProfileService _userProfileService;
17  private readonly IDepartmentService _departmentService;
18  private readonly IPublicationService _publicationService;
19 
21  IReviewService reviewService,
22  IUserProfileService userProfileService,
23  IDepartmentService departmentService,
24  IPublicationService publicationService
25  )
26  {
27  _reviewService = reviewService;
28  _userProfileService = userProfileService;
29  _departmentService = departmentService;
30  _publicationService = publicationService;
31  }
32 
33  // GET: Review
34  public IActionResult Index(ReviewIndexModel model)
35  {
36  model.Reviews = _reviewService.GetPageByRole(model.CurrentPage, model.PageSize, User);
37  model.Count = _reviewService.GetCountByRole(User);
38  return View(model);
39  }
40 
41  // GET: Review/Details/{id}
42  public IActionResult Details(Guid? id)
43  {
44  if (id == null)
45  {
46  return NotFound();
47  }
48 
49  var review = _reviewService.GetById(id.Value);
50  if (review == null)
51  {
52  return NotFound();
53  }
54 
55  if (!UserHasPermission(review))
56  {
57  return Forbid();
58  }
59 
60  return View(review);
61  }
62 
63  // GET: Review/Create
64  public IActionResult Create()
65  {
66  return View(new ReviewModel
67  {
68  Publications = _publicationService.GetAll()
69  });
70  }
71 
72  // POST: Review/Create
73  [HttpPost]
74  [ValidateAntiForgeryToken]
75  public IActionResult Create(ReviewModel model)
76  {
77  if (!ModelState.IsValid)
78  {
79  model.Publications = _publicationService.GetAll();
80  return View(model);
81  }
82 
83  if (!_publicationService.PublicationExists(model.WorkId))
84  {
85  return NotFound();
86  }
87 
88  model.Work = _publicationService.GetById(model.WorkId);
89  model.Reviewer = _userProfileService.Get(User);
90  _reviewService.CreateItem(model);
91  return RedirectToAction(nameof(Index));
92  }
93 
94  // GET: Review/Edit/{id}
95  public IActionResult Edit(Guid? id)
96  {
97  if (id == null)
98  {
99  return NotFound();
100  }
101 
102  var review = _reviewService.GetById(id.Value);
103  if (review == null)
104  {
105  return NotFound();
106  }
107 
108  if (!UserHasPermission(review))
109  {
110  return Forbid();
111  }
112 
113  return View(new ReviewEditModel(review)
114  {
115  Publications = _publicationService.GetAll()
116  });
117  }
118 
119  // POST: Review/Edit/{id}
120  [HttpPost]
121  [ValidateAntiForgeryToken]
122  public IActionResult Edit(Guid id, ReviewEditModel model)
123  {
124  if (id != model.Id || !_reviewService.Exists(id))
125  {
126  return NotFound();
127  }
128 
129  if (!UserHasPermission(_reviewService.GetById(id)))
130  {
131  return Forbid();
132  }
133 
134  if (!ModelState.IsValid)
135  {
136  model.Publications = _publicationService.GetAll();
137  return View(model);
138  }
139 
140  if (!_publicationService.PublicationExists(model.WorkId))
141  {
142  return NotFound();
143  }
144 
145  model.Work = _publicationService.GetById(model.WorkId);
146  _reviewService.UpdateItem(model);
147  return RedirectToAction(nameof(Index));
148  }
149 
150  // GET: Review/Delete/{id}
151  public IActionResult Delete(Guid? id)
152  {
153  if (id == null)
154  {
155  return NotFound();
156  }
157 
158  var review = _reviewService.GetById(id.Value);
159  if (review == null)
160  {
161  return NotFound();
162  }
163 
164  if (!UserHasPermission(review))
165  {
166  return Forbid();
167  }
168 
169  return View(review);
170  }
171 
172  // POST: Review/Delete/{id}
173  [HttpPost, ActionName("Delete")]
174  [ValidateAntiForgeryToken]
175  public IActionResult DeleteConfirmed(Guid id)
176  {
177  if (!_reviewService.Exists(id))
178  {
179  return NotFound();
180  }
181 
182  if (!UserHasPermission(_reviewService.GetById(id)))
183  {
184  return Forbid();
185  }
186 
187  _reviewService.DeleteById(id);
188  return RedirectToAction(nameof(Index));
189  }
190 
191  private bool UserHasPermission(Review guidance)
192  {
193  var user = _userProfileService.Get(User);
194  var department = _departmentService.Get(d => d.Staff.Contains(user));
195  return PageHelpers.IsAdmin(User) ||
196  PageHelpers.IsHeadOfDepartment(User) &&
197  department.Staff.Contains(guidance.Reviewer) ||
198  guidance.Reviewer.Id == user.Id;
199  }
200  }
201 }
IActionResult Index(ReviewIndexModel model)
ReviewController(IReviewService reviewService, IUserProfileService userProfileService, IDepartmentService departmentService, IPublicationService publicationService)
IEnumerable< DAL.Entities.Review > Reviews
DAL.Entities.Publications.Publication Work
Definition: ReviewModel.cs:12
IEnumerable< DAL.Entities.Publications.Publication > Publications
Definition: ReviewModel.cs:14
IActionResult Edit(Guid id, ReviewEditModel model)
IActionResult Create(ReviewModel model)
virtual UserProfile.UserProfile Reviewer
Definition: Review.cs:17
DAL.Entities.UserProfile.UserProfile Reviewer
Definition: ReviewModel.cs:16