ScientificReport
ConferenceController.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;
5 using Microsoft.EntityFrameworkCore;
11 
12 namespace ScientificReport.Controllers
13 {
14  [Authorize(Roles = UserProfileRole.Any)]
15  public class ConferenceController : Controller
16  {
17  private readonly IConferenceService _conferenceService;
18  private readonly IDepartmentService _departmentService;
19  private readonly IUserProfileService _userProfileService;
20 
22  IConferenceService conferenceService,
23  IDepartmentService departmentService,
24  IUserProfileService userProfileService
25  )
26  {
27  _conferenceService = conferenceService;
28  _departmentService = departmentService;
29  _userProfileService = userProfileService;
30  }
31 
32  // GET: Conference
33  public IActionResult Index(ConferenceIndexModel model)
34  {
35  model.Conferences = _conferenceService.GetPageByRole(model.CurrentPage, model.PageSize, User);
36  model.Count = _conferenceService.GetCountByRole(User);
37  return View(model);
38  }
39 
40  // GET: Conference/Details/{id}
41  public IActionResult Details(Guid? id)
42  {
43  if (id == null)
44  {
45  return NotFound();
46  }
47 
48  var conference = _conferenceService.GetById(id.Value);
49 
50  if (conference == null)
51  {
52  return NotFound();
53  }
54 
55  if (!UserHasPermission(conference))
56  {
57  return Forbid();
58  }
59 
60  var conferenceDetails = new ConferenceDetails
61  {
62  Conference = conference
63  };
64 
65  return View(conferenceDetails);
66  }
67 
68  // GET: Conference/Create
69  public IActionResult Create() => View();
70 
71  // POST: Conference/Create
72  [HttpPost]
73  [ValidateAntiForgeryToken]
74  public IActionResult Create([Bind("Id,Topic,Date")] Conference conference)
75  {
76  if (!ModelState.IsValid)
77  {
78  return View(conference);
79  }
80 
81  _conferenceService.CreateItem(conference);
82  return RedirectToAction(nameof(Index));
83  }
84 
85  // GET: Conference/Edit/{id}
86  public IActionResult Edit(Guid? id)
87  {
88  if (id == null)
89  {
90  return NotFound();
91  }
92 
93  var conference = _conferenceService.GetById(id.Value);
94  if (conference == null)
95  {
96  return NotFound();
97  }
98 
99  if (!UserHasPermission(conference))
100  {
101  return Forbid();
102  }
103 
104  var conferenceEdit = new ConferenceEdit
105  {
106  Conference = conference
107  };
108 
109  return View(conferenceEdit);
110  }
111 
112  // POST: Conference/Edit/{id}
113  [HttpPost]
114  [ValidateAntiForgeryToken]
115  public IActionResult Edit(Guid id, ConferenceEdit conferenceEdit)
116  {
117  var conference = conferenceEdit.Conference;
118  if (id != conference.Id)
119  {
120  return NotFound();
121  }
122 
123  if (!UserHasPermission(conference))
124  {
125  return Forbid();
126  }
127 
128  if (!ModelState.IsValid)
129  {
130  return View(conferenceEdit);
131  }
132  try
133  {
134  _conferenceService.UpdateItem(conference);
135  }
136  catch (DbUpdateConcurrencyException)
137  {
138  if (!_conferenceService.Exists(conference.Id))
139  {
140  return NotFound();
141  }
142  throw;
143  }
144 
145  return RedirectToAction(nameof(Index));
146  }
147 
148  // GET: Conference/Delete/{id}
149  public IActionResult Delete(Guid? id)
150  {
151  if (id == null)
152  {
153  return NotFound();
154  }
155 
156  var conference = _conferenceService.GetById(id.Value);
157  if (conference == null)
158  {
159  return NotFound();
160  }
161 
162  if (!UserHasPermission(conference))
163  {
164  return Forbid();
165  }
166 
167  return View(conference);
168  }
169 
170  // POST: Conference/Delete/{id}
171  [HttpPost, ActionName("Delete")]
172  [ValidateAntiForgeryToken]
173  public IActionResult DeleteConfirmed(Guid id)
174  {
175  if (!UserHasPermission(_conferenceService.GetById(id)))
176  {
177  return Forbid();
178  }
179 
180  _conferenceService.DeleteById(id);
181  return RedirectToAction(nameof(Index));
182  }
183 
184  private bool UserHasPermission(Conference conference)
185  {
186  var user = _userProfileService.Get(User);
187  var department = _departmentService.Get(d => d.Staff.Contains(user));
188  return PageHelpers.IsAdmin(User) ||
189  PageHelpers.IsHeadOfDepartment(User) &&
190  _conferenceService.GetParticipators(conference.Id).Any(p => department.Staff.Contains(p)) ||
191  _conferenceService.GetParticipators(conference.Id).Contains(user);
192  }
193  }
194 }
IActionResult Create([Bind("Id,Topic,Date")] Conference conference)
IActionResult Edit(Guid id, ConferenceEdit conferenceEdit)
IActionResult Index(ConferenceIndexModel model)
ConferenceController(IConferenceService conferenceService, IDepartmentService departmentService, IUserProfileService userProfileService)