blob: 912f591a31f66059d9f02199487cecddbebf6b70 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
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<List<Reservation>> GetReservationsForUser()
{
return _context.Reservations.Where(r => r.UserId == LoggedInUser.Id).ToList();
}
[HttpGet("all")]
[Authorize("Administrator")]
public ActionResult<List<ReservationDto>> 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<List<DateTime>> 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<DateTime>();
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();
}
}
}
|