profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
PollsController.cs 4,138 bytes
1 using App.BLL.DTO;
2 using App.BLL.Services.Admin;
3 using App.Domain;
4 using Microsoft.AspNetCore.Authorization;
5 using Microsoft.AspNetCore.Mvc;
6 using Microsoft.AspNetCore.Mvc.Rendering;
7 using Microsoft.Extensions.Localization;
8 using WebApp.Areas.Admin.Models;
9
10 namespace WebApp.Areas.Admin.Controllers;
11
12 [Area("Admin")]
13 [Authorize(Roles = "admin")]
14 public class PollsController : Controller
15 {
16 private readonly IPollAdminService _service;
17 private readonly IStringLocalizer<App.Resources.Views.Shared> _l;
18
19 public PollsController(IPollAdminService service, IStringLocalizer<App.Resources.Views.Shared> l)
20 {
21 _service = service;
22 _l = l;
23 }
24
25 public async Task<IActionResult> Index(string? search)
26 {
27 var polls = await _service.GetAllAsync(search);
28
29 var vm = new AdminPollIndexViewModel
30 {
31 Title = _l["Polls"].Value,
32 Items = polls.OrderByDescending(p => p.Id).ToList(),
33 CurrentSearch = search
34 };
35 return View(vm);
36 }
37
38 public async Task<IActionResult> Details(Guid id)
39 {
40 var poll = await _service.GetByIdAsync(id);
41 if (poll == null) return NotFound();
42
43 return View(new AdminDetailsViewModel<TripPollBllDto>
44 {
45 Title = _l["Poll details"].Value,
46 Item = poll
47 });
48 }
49
50 public async Task<IActionResult> Create()
51 {
52 var vm = new AdminPollFormViewModel
53 {
54 Title = _l["New poll"].Value,
55 TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"),
56 UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name")
57 };
58 return View(vm);
59 }
60
61 [HttpPost]
62 [ValidateAntiForgeryToken]
63 public async Task<IActionResult> Create(AdminPollFormViewModel vm)
64 {
65 if (ModelState.IsValid)
66 {
67 await _service.CreateAsync(vm.Poll);
68 return RedirectToAction(nameof(Index));
69 }
70 vm.Title = _l["New poll"].Value;
71 vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name");
72 vm.UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name");
73 return View(vm);
74 }
75
76 public async Task<IActionResult> Edit(Guid id)
77 {
78 var poll = await _service.GetByIdAsync(id);
79 if (poll == null) return NotFound();
80 var vm = new AdminPollFormViewModel
81 {
82 Title = _l["Edit poll"].Value,
83 Poll = poll,
84 TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"),
85 UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name")
86 };
87 return View(vm);
88 }
89
90 [HttpPost]
91 [ValidateAntiForgeryToken]
92 public async Task<IActionResult> Edit(Guid id, AdminPollFormViewModel vm)
93 {
94 if (id != vm.Poll.Id) return NotFound();
95 if (ModelState.IsValid)
96 {
97 await _service.UpdateAsync(vm.Poll);
98 return RedirectToAction(nameof(Index));
99 }
100 vm.Title = _l["Edit poll"].Value;
101 vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name");
102 vm.UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name");
103 return View(vm);
104 }
105
106 public async Task<IActionResult> Delete(Guid id)
107 {
108 var poll = await _service.GetByIdAsync(id);
109 if (poll == null) return NotFound();
110
111 return View(new AdminDeleteViewModel<TripPollBllDto>
112 {
113 Title = _l["Delete poll"].Value,
114 Item = poll
115 });
116 }
117
118 [HttpPost, ActionName("Delete")]
119 [ValidateAntiForgeryToken]
120 public async Task<IActionResult> DeleteConfirmed(Guid id)
121 {
122 await _service.DeleteAsync(id);
123 return RedirectToAction(nameof(Index));
124 }
125 }
126