profileShare

rasmusjy / splitapp-backend-microservices

Read-only snapshot

No repository description.

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