blob: 4fe87cca281e82e138f6a18ffe04e29e23ca326a (
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
|
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection.Metadata;
using IOL.Fagprove.Data;
using IOL.Fagprove.Data.DTOs;
using IOL.Fagprove.Data.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using IOL.Fagprove.Utilities;
namespace IOL.Fagprove.Controllers
{
[Authorize("Administrator")]
public class CabinsController : BaseController
{
private readonly AppDbContext _context;
public CabinsController(AppDbContext context)
{
_context = context;
}
[HttpGet]
public ActionResult<List<CabinDto>> GetCabins()
{
var cabins = _context.Cabins.ToList();
var res = new List<CabinDto>();
foreach (var cabin in cabins)
{
var fieldName = StaticData.CabinFields.FirstOrDefault(s => s.Id == cabin.CategoryId)?.Name ?? "Ukjent";
res.Add(new CabinDto
{
Capacity = cabin.Capacity,
Description = cabin.Description,
Id = cabin.Id,
Name = cabin.Name,
Price = cabin.Price,
CategoryId = cabin.CategoryId,
CategoryName = fieldName
});
}
return res;
}
[HttpPost("create")]
public ActionResult CreateCabin(ReservationObject data)
{
if (data.Name.IsMissing()) return BadRequest(new {error = "Navn er påkrevd"});
data.CreatedBy = LoggedInUser.Id;
data.CreatedUtc = DateTime.UtcNow;
_context.Cabins.Add(data);
_context.SaveChanges();
return Ok(data.Name);
}
[HttpPut("update")]
public ActionResult UpdateCabin(ReservationObject data)
{
if (data.Name.IsMissing()) return BadRequest("Navn er påkrevd");
if (data.Id == Guid.Empty) return BadRequest();
var cabinExists = _context.Cabins.Any(c => c.Id == data.Id);
if (!cabinExists) return BadRequest(new {error = "Fant ikke hytten"});
data.ModifiedBy = LoggedInUser.Id;
data.ModifiedUtc = DateTime.UtcNow;
_context.Cabins.Update(data);
_context.Entry(data).Property(x => x.CreatedBy).IsModified = false;
_context.Entry(data).Property(x => x.CreatedUtc).IsModified = false;
_context.Entry(data).Property(x => x.Id).IsModified = false;
_context.SaveChanges();
return Ok();
}
[HttpDelete("delete")]
public ActionResult DeleteCabin(CabinDto data)
{
var cabinToRemove = _context.Cabins.SingleOrDefault(c => c.Id == data.Id);
if (cabinToRemove == default) return Ok();
_context.Cabins.Remove(cabinToRemove);
var existingFutureReservationsForThisCabin = _context.Reservations.Where(r => r.ReservationObjectId == cabinToRemove.Id
&& DateTime.Compare(r.From, DateTime.Today) >= 0).ToList();
if (existingFutureReservationsForThisCabin.Count != 0)
{
_context.Reservations.RemoveRange(existingFutureReservationsForThisCabin);
}
_context.SaveChanges();
return Ok();
}
}
}
|