Advertisement
marto9119

Controler

Nov 18th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | Source Code | 0 0
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using SportBarFormula.Core.Services.Contracts;
  4. using SportBarFormula.Core.ViewModels.Reservation;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SportBarFormula.Controllers
  9. {
  10.     [Authorize]
  11.     public class ReservationController : Controller
  12.     {
  13.         private readonly IReservationService _service;
  14.  
  15.         public ReservationController(IReservationService service)
  16.         {
  17.             _service = service;
  18.         }
  19.  
  20.         /// <summary>
  21.         /// Списък с всички резервации за администраторите.
  22.         /// </summary>
  23.         [Authorize(Roles = "Admin")]
  24.         public async Task<IActionResult> Index()
  25.         {
  26.             var reservations = await _service.GetAllReservationsAsync();
  27.             return View(reservations);
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Показва на клиентите техните текущи и минали резервации.
  32.         /// </summary>
  33.         public async Task<IActionResult> MyReservations()
  34.         {
  35.             var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Assuming a method to get the logged-in user's ID
  36.             var reservations = await _service.GetReservationsByUserIdAsync(userId);
  37.             return View(reservations);
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Форма за създаване на нова резервация.
  42.         /// </summary>
  43.         [HttpGet]
  44.         public IActionResult Create()
  45.         {
  46.             return View();
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Записва новата резервация.
  51.         /// </summary>
  52.         [HttpPost]
  53.         public async Task<IActionResult> Create(ReservationViewModel model)
  54.         {
  55.             if (ModelState.IsValid)
  56.             {
  57.                 var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Assuming a method to get the logged-in user's ID
  58.                 model.UserId = userId;
  59.                 await _service.AddReservationAsync(model);
  60.                 return RedirectToAction(nameof(MyReservations));
  61.             }
  62.             return View(model);
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Форма за редактиране на съществуваща резервация.
  67.         /// </summary>
  68.         [HttpGet]
  69.         public async Task<IActionResult> Edit(int id)
  70.         {
  71.             var reservation = await _service.GetReservationByIdAsync(id);
  72.             if (reservation == null)
  73.             {
  74.                 return NotFound();
  75.             }
  76.             return View(reservation);
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Записва промените по резервацията.
  81.         /// </summary>
  82.         [HttpPost]
  83.         public async Task<IActionResult> Edit(ReservationViewModel model)
  84.         {
  85.             if (ModelState.IsValid)
  86.             {
  87.                 await _service.UpdateReservationAsync(model);
  88.                 return RedirectToAction(nameof(MyReservations));
  89.             }
  90.             return View(model);
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Анулира съществуваща резервация.
  95.         /// </summary>
  96.         [HttpPost]
  97.         public async Task<IActionResult> Cancel(int id)
  98.         {
  99.             await _service.CancelReservationAsync(id);
  100.             return RedirectToAction(nameof(MyReservations));
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement