generated from egmont11/ASP.Net-Core-MVC-Template
Přemístění + dodělání funkčnosti webu.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using StuzkovakWeb.Data;
|
||||
using StuzkovakWeb.Entities;
|
||||
using StuzkovakWeb.ViewModels;
|
||||
|
||||
namespace StuzkovakWeb.Areas.Admin.Controllers;
|
||||
|
||||
public class InvitesController : AdminBaseController
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public InvitesController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var invites = await _context.Invites.ToListAsync();
|
||||
return View(invites);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(Guid id)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite == null) return NotFound();
|
||||
return View(new AdminInviteDetailViewModel
|
||||
{
|
||||
Id = invite.Id,
|
||||
TeacherName = invite.TeacherName,
|
||||
IsAccepted = invite.IsAccepted
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(string teacherName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(teacherName)) return BadRequest();
|
||||
var invite = new InviteEntity
|
||||
{
|
||||
TeacherName = teacherName,
|
||||
IsAccepted = null
|
||||
};
|
||||
await _context.Invites.AddAsync(invite);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
TempData["SuccessMessage"] = "Pozvánka byla vytvořena.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite == null) return NotFound();
|
||||
_context.Invites.Remove(invite);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(Guid id, string name)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite is null) return NotFound();
|
||||
|
||||
invite.TeacherName = name;
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
TempData["SuccessMessage"] = "Pozvánka byla upravena.";
|
||||
return RedirectToAction("Details", new { id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user