yavsc/src/Yavsc.Org/Controllers/Accounting/UsersController.cs

126 lines
4 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Yavsc.Models;
namespace Yavsc.Controllers
{
[Authorize("AdministratorOnly")]
public class UsersController : Controller
{
2020-09-12 01:11:30 +01:00
private readonly ApplicationDbContext _context;
public UsersController(ApplicationDbContext context)
{
_context = context;
}
// GET: Users
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.ApplicationUser.Include(a => a.PostalAddress);
return View(await applicationDbContext.ToListAsync());
}
// GET: Users/Details/5
public async Task<IActionResult> Details(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
ApplicationUser applicationUser = await _context.ApplicationUser.SingleAsync(m => m.Id == id);
if (applicationUser == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
return View(applicationUser);
}
// GET: Users/Create
public IActionResult Create()
{
2026-06-04 12:13:37 +01:00
ViewBag.PostalAddressId = new SelectList(_context.Locations, "Id", "PostalAddress");
return View();
}
// POST: Users/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
_context.ApplicationUser.Add(applicationUser);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
2026-06-04 12:13:37 +01:00
ViewBag.PostalAddressId = new SelectList(_context.Locations, "Id", "PostalAddress", applicationUser.PostalAddressId);
return View(applicationUser);
}
// GET: Users/Edit/5
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
ApplicationUser applicationUser = await _context.ApplicationUser.SingleAsync(m => m.Id == id);
if (applicationUser == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
2026-06-04 12:13:37 +01:00
ViewBag.PostalAddressId = new SelectList(_context.Locations, "Id", "PostalAddress", applicationUser.PostalAddressId);
return View(applicationUser);
}
// POST: Users/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
_context.Update(applicationUser);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
2026-06-04 12:13:37 +01:00
ViewBag.PostalAddressId = new SelectList(_context.Locations, "Id", "PostalAddress", applicationUser.PostalAddressId);
return View(applicationUser);
}
// GET: Users/Delete/5
[ActionName("Delete")]
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
ApplicationUser applicationUser = await _context.ApplicationUser.SingleAsync(m => m.Id == id);
if (applicationUser == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
}
return View(applicationUser);
}
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
ApplicationUser applicationUser = await _context.ApplicationUser.SingleAsync(m => m.Id == id);
_context.ApplicationUser.Remove(applicationUser);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}