using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Runtime.InteropServices.ComTypes; using Flurl.Util; using IOL.Fagprove.Data; using IOL.Fagprove.Data.DTOs; using IOL.Fagprove.Data.Enums; using IOL.Fagprove.Data.Models; using IOL.Fagprove.Services.Interfaces; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Graph; using IOL.Fagprove.Services; namespace IOL.Fagprove.Controllers { public class ReservationsController : BaseController { private readonly AppDbContext _context; private readonly IAppReservationService _reservationService; public ReservationsController(AppDbContext context, IAppReservationService reservationService) { _context = context; _reservationService = reservationService; } // /api/reservations [HttpGet] public ActionResult> GetReservationsForUser() { return _context.Reservations.Where(r => r.UserId == LoggedInUser.Id).ToList(); } [HttpGet("all")] [Authorize("Administrator")] public ActionResult> GetAllReservations() { return _context.Reservations.Select(r => new ReservationDto { Id = r.Id, Cabin = _context.Cabins.SingleOrDefault(c => c.Id == r.ReservationObjectId).Name ?? "", From = r.From.ToString("dd-MM-yyyy"), To = r.To.ToString("dd-MM-yyyy"), Status = r.Status, Name = _context.Users.SingleOrDefault(u => u.Id == r.UserId).Name ?? "Ukjent bruker" }).ToList(); } [HttpGet("occupancy")] public ActionResult> GetOccupany(Guid cabinId) { var today = DateTime.Today; var reservationsForThisCabin = _context.Reservations.Where(r => r.ReservationObjectId == cabinId && r.From > today && r.Status == ReservationStatus.Granted).ToList(); if (reservationsForThisCabin.Count == 0) return default; var occupiedDates = new List(); foreach (var reservation in reservationsForThisCabin) { for (var dt = reservation.From; dt <= reservation.To; dt = dt.AddDays(1)) { occupiedDates.Add(dt.Date); } } return occupiedDates; } [HttpPost("create")] public ActionResult CreateReservation(Reservation data) { var reservationIsPossible = _reservationService.ReservationIsPossible(data); if (!reservationIsPossible) return BadRequest(new {error = "Reservasjonen er ikke mulig lenger."}); data.UserId = LoggedInUser.Id; data.CreatedBy = LoggedInUser.Id; data.CreatedUtc = DateTime.UtcNow; data.Status = ReservationStatus.Pending; _context.Reservations.Add(data); _context.SaveChanges(); return Ok(); } [HttpGet("grant")] [Authorize("Administrator")] public ActionResult GrantReservation(Guid id) { if (id == Guid.Empty) return BadRequest(); var reservation = _context.Reservations.SingleOrDefault(r => r.Id == id); if (reservation == default) return BadRequest(new {error = "Fant ikke reservasjonen"}); if (reservation.Status == ReservationStatus.Granted || reservation.Status == ReservationStatus.Expired) return BadRequest(new {error = "Denne reservasjonen er allerede godkjent"}); reservation.Status = ReservationStatus.Granted; reservation.ModifiedBy = LoggedInUser.Id; reservation.ModifiedUtc = DateTime.UtcNow; _context.Reservations.Update(reservation); _context.Entry(reservation).Property(x => x.CreatedBy).IsModified = false; _context.Entry(reservation).Property(x => x.CreatedUtc).IsModified = false; _context.Entry(reservation).Property(x => x.Id).IsModified = false; _context.SaveChanges(); // TODO: Inform user if mail fails var emailSent = _reservationService.SendReservationStatusMail(reservation); return Ok(); } [HttpGet("deny")] [Authorize("Administrator")] public ActionResult DenyReservation(Guid id) { if (id == Guid.Empty) return BadRequest(); var reservation = _context.Reservations.SingleOrDefault(r => r.Id == id); if (reservation == default) return BadRequest(new {error = "Fant ikke reservasjonen"}); if (reservation.Status == ReservationStatus.Rejected || reservation.Status == ReservationStatus.Expired) return BadRequest(new {error = "Denne reservasjonen er allerede avvist"}); reservation.Status = ReservationStatus.Rejected; reservation.ModifiedBy = LoggedInUser.Id; reservation.ModifiedUtc = DateTime.UtcNow; _context.Reservations.Update(reservation); _context.Entry(reservation).Property(x => x.CreatedBy).IsModified = false; _context.Entry(reservation).Property(x => x.CreatedUtc).IsModified = false; _context.Entry(reservation).Property(x => x.Id).IsModified = false; _context.SaveChanges(); // TODO: Inform user if mail fails var emailSent = _reservationService.SendReservationStatusMail(reservation); return Ok(); } [HttpDelete("delete")] public ActionResult DeleteReservation(Guid reservationId) { var reservation = _context.Reservations.SingleOrDefault(r => r.Id == reservationId); if (reservation == default) return BadRequest(new {error = "Fant ikke reservasjonen"}); if (reservation.UserId != LoggedInUser.Id) return Unauthorized(new {error = "Du har ikke lov til å slette denne reservasjonen"}); _context.Reservations.Remove(reservation); _context.SaveChanges(); return Ok(); } } }