Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using SportBarFormula.Core.Services.Contracts;
- using SportBarFormula.Core.ViewModels.Reservation;
- using System.Security.Claims;
- using System.Threading.Tasks;
- namespace SportBarFormula.Controllers
- {
- [Authorize]
- public class ReservationController : Controller
- {
- private readonly IReservationService _service;
- public ReservationController(IReservationService service)
- {
- _service = service;
- }
- /// <summary>
- /// Списък с всички резервации за администраторите.
- /// </summary>
- [Authorize(Roles = "Admin")]
- public async Task<IActionResult> Index()
- {
- var reservations = await _service.GetAllReservationsAsync();
- return View(reservations);
- }
- /// <summary>
- /// Показва на клиентите техните текущи и минали резервации.
- /// </summary>
- public async Task<IActionResult> MyReservations()
- {
- var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Assuming a method to get the logged-in user's ID
- var reservations = await _service.GetReservationsByUserIdAsync(userId);
- return View(reservations);
- }
- /// <summary>
- /// Форма за създаване на нова резервация.
- /// </summary>
- [HttpGet]
- public IActionResult Create()
- {
- return View();
- }
- /// <summary>
- /// Записва новата резервация.
- /// </summary>
- [HttpPost]
- public async Task<IActionResult> Create(ReservationViewModel model)
- {
- if (ModelState.IsValid)
- {
- var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Assuming a method to get the logged-in user's ID
- model.UserId = userId;
- await _service.AddReservationAsync(model);
- return RedirectToAction(nameof(MyReservations));
- }
- return View(model);
- }
- /// <summary>
- /// Форма за редактиране на съществуваща резервация.
- /// </summary>
- [HttpGet]
- public async Task<IActionResult> Edit(int id)
- {
- var reservation = await _service.GetReservationByIdAsync(id);
- if (reservation == null)
- {
- return NotFound();
- }
- return View(reservation);
- }
- /// <summary>
- /// Записва промените по резервацията.
- /// </summary>
- [HttpPost]
- public async Task<IActionResult> Edit(ReservationViewModel model)
- {
- if (ModelState.IsValid)
- {
- await _service.UpdateReservationAsync(model);
- return RedirectToAction(nameof(MyReservations));
- }
- return View(model);
- }
- /// <summary>
- /// Анулира съществуваща резервация.
- /// </summary>
- [HttpPost]
- public async Task<IActionResult> Cancel(int id)
- {
- await _service.CancelReservationAsync(id);
- return RedirectToAction(nameof(MyReservations));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement