/
EventApiController.cs
147 lines (134 loc) · 4.96 KB
/
EventApiController.cs
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
138
139
140
141
142
143
144
145
146
147
// Author: Ryan Cobb (@cobbr_io)
// Project: Covenant (https://github.com/cobbr/Covenant)
// License: GNU GPLv3
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Covenant.Core;
using Covenant.Models;
using Covenant.Models.Covenant;
namespace Covenant.Controllers
{
[Authorize(Policy = "RequireJwtBearer")]
[ApiController]
[Route("api/events")]
public class EventApiController : Controller
{
private readonly CovenantContext _context;
public EventApiController(CovenantContext context)
{
_context = context;
}
// GET: api/events
// <summary>
// Get a list of Events
// </summary>
[HttpGet(Name = "GetEvents")]
public ActionResult<IEnumerable<Event>> GetEvents()
{
return _context.Events.ToList();
}
// GET api/events/{id}
// <summary>
// Get an Event by id
// </summary>
[HttpGet("{id}", Name = "GetEvent")]
public ActionResult<Event> GetEvent(int id)
{
var anEvent = _context.Events.FirstOrDefault(E => E.Id == id);
if (anEvent == null)
{
return NotFound($"NotFound - Event with id: {id}");
}
return Ok(anEvent);
}
// GET: api/events/time
// <summary>
// Get Covenant's current DateTime
// </summary>
[HttpGet("time", Name = "GetTime")]
public ActionResult<long> GetTime()
{
return Ok(DateTime.UtcNow.ToBinary());
}
// GET: api/events/range/{fromdate}
// <summary>
// Get a list of Events that occurred after the specified DateTime
// </summary>
[HttpGet("range/{fromdate}", Name = "GetEventsAfter")]
public ActionResult<IEnumerable<Event>> GetEventsAfter(long fromdate)
{
DateTime start = DateTime.FromBinary(fromdate);
return _context.Events.Where(E => E.Time.CompareTo(start) >= 0).ToList();
}
// GET: api/events/range/{fromdate}/{todate}
// <summary>
// Get a list of Events that occurred between the range of specified DateTimes
// </summary>
[HttpGet("range/{fromdate}/{todate}", Name = "GetEventsRange")]
public ActionResult<IEnumerable<Event>> GetEventsRange(long fromdate, long todate)
{
DateTime start = DateTime.FromBinary(fromdate);
DateTime end = DateTime.FromBinary(todate);
return _context.Events.Where(E => E.Time.CompareTo(start) >= 0 && E.Time.CompareTo(end) <= 0).ToList();
}
// POST api/events
// <summary>
// Create an Event
// </summary>
[HttpPost(Name = "CreateEvent")]
[ProducesResponseType(typeof(Event), 201)]
public ActionResult<Event> CreateEvent([FromBody]Event anEvent)
{
anEvent.Time = DateTime.UtcNow;
_context.Events.Add(anEvent);
_context.SaveChanges();
return CreatedAtRoute(nameof(GetEvent), new { id = anEvent.Id }, anEvent);
}
// GET: api/events/download/{id}
// <summary>
// Get a DownloadEvent
// </summary>
[HttpGet("download/{id}", Name = "GetDownloadEvent")]
public ActionResult<DownloadEvent> GetDownloadEvent(int id)
{
return ((DownloadEvent)_context.Events.FirstOrDefault(E => E.Id == id && E.Type == Event.EventType.Download));
}
// GET: api/events/download/{id}/content
// <summary>
// Get a downloaded file
// </summary>
[HttpGet("download/{id}/content", Name = "GetDownloadContent")]
public ActionResult<string> GetDownloadContent(int id)
{
DownloadEvent theEvent = ((DownloadEvent)_context.Events.FirstOrDefault(E => E.Id == id));
if (theEvent == null)
{
return NotFound($"NotFound - DownloadEvent with id: {id}"); ;
}
string filename = Path.Combine(Common.CovenantDownloadDirectory, theEvent.FileName);
if (!System.IO.File.Exists(filename))
{
return BadRequest($"BadRequest - Path does not exist on disk: {filename}");
}
return Convert.ToBase64String(System.IO.File.ReadAllBytes(filename));
}
// POST api/events/download
// <summary>
// Post a downloaded file or portion of a downloaded file
// </summary>
[HttpPost("download", Name = "CreateDownloadEvent")]
[ProducesResponseType(typeof(Event), 201)]
public ActionResult CreateDownloadEvent([FromBody]DownloadEvent downloadEvent)
{
downloadEvent.Time = DateTime.UtcNow;
downloadEvent.WriteToDisk();
_context.Events.Add(downloadEvent);
_context.SaveChanges();
return CreatedAtRoute(nameof(GetEvent), new { id = downloadEvent.Id }, downloadEvent);
}
}
}