forked from DataStax-Academy/cassandra-workshop-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstrumentsController.cs
220 lines (209 loc) · 10.5 KB
/
InstrumentsController.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.Linq;
using getting_started_with_astra_csharp.Interfaces;
using Microsoft.AspNetCore.Mvc;
using getting_started_with_astra_csharp.Models;
using Cassandra.Data.Linq;
using Cassandra.Mapping;
using System.Threading.Tasks;
namespace getting_started_with_astra_csharp.Controllers
{
/// <summary>
/// Handles operations around the instrument readings of a spacecraft journey
/// </summary>
[Route("api/spacecraft/{spaceCraftName}/{journeyId}/instruments")]
[ApiController]
public class InstrumentsController : ControllerBase
{
private IDataStaxService Service { get; set; }
public InstrumentsController(IDataStaxService service)
{
Service = service;
}
// GET api/spacecraft/{spaceCraftName}/{journeyId}/instruments/temperature
/// <summary>
/// This returns the temperature readings for specified page and page size for the spacecraft and journey.
/// </summary>
/// <param name="spaceCraftName">The name of the spacecraft</param>
/// <param name="journeyId">The id of the journey</param>
/// <param name="pageState">The pagestate of a previous request to continue, or null if this is a new request</param>
/// <param name="pageSize">The page size to return</param>
/// <returns>A PageResultWrapper containing the results</returns>
[HttpGet("temperature")]
public async Task<PagedResultWrapper<ICollection<spacecraft_temperature_over_time>>> GetTemperatureReading(string spaceCraftName, Guid journeyId,
[FromQuery]string pageState, [FromQuery]int? pageSize)
{
var spaceCraft = new Table<spacecraft_temperature_over_time>(Service.Session);
var query = spaceCraft.
Where(s => s.Spacecraft_Name == spaceCraftName && s.Journey_Id == journeyId);
if (pageSize.HasValue)
{
query.SetPageSize(pageSize.Value);
}
if (pageState != null && pageState.Length > 0)
{
query.SetPagingState(Convert.FromBase64String(pageState));
}
var temperature = await query.ExecutePagedAsync();
return new PagedResultWrapper<ICollection<spacecraft_temperature_over_time>>(pageSize.HasValue ? pageSize.Value : 0,
temperature.PagingState, temperature.OrderBy(s => s.Reading_Time).ToList()
);
}
// GET api/spacecraft/{spaceCraftName}/{journeyId}/instruments/pressure
/// <summary>
/// This returns the pressure readings for specified page and page size for the spacecraft and journey.
/// </summary>
/// <param name="spaceCraftName">The name of the spacecraft</param>
/// <param name="journeyId">The id of the journey</param>
/// <param name="pageState">The pagestate of a previous request to continue, or null if this is a new request</param>
/// <param name="pageSize">The page size to return</param>
/// <returns>A PageResultWrapper containing the results</returns>
[HttpGet("pressure")]
public async Task<ActionResult<PagedResultWrapper<ICollection<spacecraft_pressure_over_time>>>> GetPressureReading(string spaceCraftName, Guid journeyId,
[FromQuery]string pageState, [FromQuery]int? pageSize)
{
var spaceCraft = new Table<spacecraft_pressure_over_time>(Service.Session);
var query = spaceCraft.
Where(s => s.Spacecraft_Name == spaceCraftName && s.Journey_Id == journeyId);
if (pageSize.HasValue)
{
query.SetPageSize(pageSize.Value);
}
if (pageState != null && pageState.Length > 0)
{
query.SetPagingState(Convert.FromBase64String(pageState));
}
var pressure = await query.ExecutePagedAsync();
return Ok(new PagedResultWrapper<ICollection<spacecraft_pressure_over_time>>(pageSize.HasValue ? pageSize.Value : 0,
pressure.PagingState, pressure.OrderBy(s => s.Reading_Time).ToList())
);
}
// GET api/spacecraft/{spaceCraftName}/{journeyId}/instruments/location
/// <summary>
/// This returns the location readings for specified page and page size for the spacecraft and journey.
/// </summary>
/// <param name="spaceCraftName">The name of the spacecraft</param>
/// <param name="journeyId">The id of the journey</param>
/// <param name="pageState">The pagestate of a previous request to continue, or null if this is a new request</param>
/// <param name="pageSize">The page size to return</param>
/// <returns>A PageResultWrapper containing the results</returns>
[HttpGet("location")]
public async Task<ActionResult<PagedResultWrapper<ICollection<spacecraft_location_over_time>>>> GetLocationReading(string spaceCraftName, Guid journeyId,
[FromQuery]string pageState, [FromQuery]int? pageSize)
{
var spaceCraft = new Table<spacecraft_location_over_time>(Service.Session);
var query = spaceCraft.
Where(s => s.Spacecraft_Name == spaceCraftName && s.Journey_Id == journeyId);
if (pageSize.HasValue)
{
query.SetPageSize(pageSize.Value);
}
if (pageState != null && pageState.Length > 0)
{
query.SetPagingState(Convert.FromBase64String(pageState));
}
var location = await query.ExecutePagedAsync();
return new PagedResultWrapper<ICollection<spacecraft_location_over_time>>(pageSize.HasValue ? pageSize.Value : 0,
location.PagingState, location.OrderBy(s => s.Reading_Time).ToList()
);
}
// GET api/spacecraft/{spaceCraftName}/{journeyId}/instruments/speed
/// <summary>
/// This returns the speed readings for specified page and page size for the spacecraft and journey.
/// </summary>
/// <param name="spaceCraftName">The name of the spacecraft</param>
/// <param name="journeyId">The id of the journey</param>
/// <param name="pageState">The pagestate of a previous request to continue, or null if this is a new request</param>
/// <param name="pageSize">The page size to return</param>
/// <returns>A PageResultWrapper containing the results</returns>
[HttpGet("speed")]
public async Task<ActionResult<PagedResultWrapper<ICollection<spacecraft_speed_over_time>>>> GetSpeedReading(string spaceCraftName, Guid journeyId,
[FromQuery]string pageState, [FromQuery]int? pageSize)
{
var spaceCraft = new Table<spacecraft_speed_over_time>(Service.Session);
var query = spaceCraft.
Where(s => s.Spacecraft_Name == spaceCraftName && s.Journey_Id == journeyId);
if (pageSize.HasValue)
{
query.SetPageSize(pageSize.Value);
}
if (pageState != null && pageState.Length > 0)
{
query.SetPagingState(Convert.FromBase64String(pageState));
}
var speed = await query.ExecutePagedAsync();
return new PagedResultWrapper<ICollection<spacecraft_speed_over_time>>(pageSize.HasValue ? pageSize.Value : 0,
speed.PagingState, speed.OrderBy(s => s.Reading_Time).ToList()
);
}
/// <summary>
/// This accepts an array of spacecraft_temperature_over_time objects and saves them to the database
/// </summary>
/// <param name="temperatures">The array of spacecraft_temperature_over_time</param>
/// <returns>A 200 if successful, other wise an error</returns>
[HttpPost("temperature")]
public async Task<IActionResult> SaveTemperatures([FromBody]spacecraft_temperature_over_time[] temperatures)
{
IMapper mapper = new Mapper(Service.Session);
var batch = mapper.CreateBatch();
for (int i = 0; i < temperatures.Count(); i++)
{
batch.Insert(temperatures[i]);
}
await mapper.ExecuteAsync(batch);
return Ok();
}
/// <summary>
/// This accepts an array of spacecraft_pressure_over_time objects and saves them to the database
/// </summary>
/// <param name="pressures">The array of spacecraft_pressure_over_time</param>
/// <returns>A 200 if successful, other wise an error</returns>
[HttpPost("pressure")]
public async Task<IActionResult> SavePressures([FromBody]spacecraft_pressure_over_time[] pressures)
{
IMapper mapper = new Mapper(Service.Session);
var batch = mapper.CreateBatch();
for (int i = 0; i < pressures.Count(); i++)
{
batch.Insert(pressures[i]);
}
await mapper.ExecuteAsync(batch);
return Ok();
}
/// <summary>
/// This accepts an array of spacecraft_speed_over_time objects and saves them to the database
/// </summary>
/// <param name="speed">The array of spacecraft_speed_over_time</param>
/// <returns>A 200 if successful, other wise an error</returns>
[HttpPost("speed")]
public async Task<IActionResult> SaveSpeed([FromBody]spacecraft_speed_over_time[] speed)
{
IMapper mapper = new Mapper(Service.Session);
var batch = mapper.CreateBatch();
for (int i = 0; i < speed.Count(); i++)
{
batch.Insert(speed[i]);
}
await mapper.ExecuteAsync(batch);
return Ok();
}
/// <summary>
/// This accepts an array of spacecraft_location_over_time objects and saves them to the database
/// </summary>
/// <param name="locations">The array of spacecraft_location_over_time</param>
/// <returns>A 200 if successful, other wise an error</returns>
[HttpPost("location")]
public async Task<ActionResult> SaveLocations([FromBody]spacecraft_location_over_time[] locations)
{
IMapper mapper = new Mapper(Service.Session);
var batch = mapper.CreateBatch();
for (int i = 0; i < locations.Count(); i++)
{
batch.Insert(locations[i]);
}
await mapper.ExecuteAsync(batch);
return Ok();
}
}
}