Skip to content

Commit

Permalink
Flesh out controllers and models for web api
Browse files Browse the repository at this point in the history
  • Loading branch information
xxbiohazrdxx committed Dec 7, 2021
1 parent 41d2b22 commit b9697ea
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 263 deletions.
12 changes: 6 additions & 6 deletions AmpAPI/AmpAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MPRSGxZ\MPRSGxZ.csproj" />
<ProjectReference Include="..\MPRSGxZ\MPRSGxZ.csproj" />
</ItemGroup>

</Project>
82 changes: 47 additions & 35 deletions AmpAPI/Controllers/AmplifierController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AmpAPI.Services;
using AmpAPI.Models;
using AmpAPI.Services;
using AmpAPI.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
Expand All @@ -8,9 +9,9 @@
namespace AmpAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AmplifierController : ControllerBase
{
[ApiController]
public class AmplifierController : ControllerBase
{
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

Expand All @@ -20,36 +21,47 @@ public AmplifierController(IOptions<AmplifierStackSettings> Settings, IAmplifier
this.AmplifierService = AmplifierService;
}

// GET: api/Amplifier
[HttpGet]
public IEnumerable<Amplifier> Get()
{
// GET: api/Amplifier
[HttpGet]
public IEnumerable<Amplifier> Get()
{
return AmplifierService.Amplifiers;
}

// GET: api/Amplifier/5
[HttpGet("{id:int:range(1,3)}")]
public Amplifier Get(int id)
{
return AmplifierService.Amplifiers[id - 1];
}

// POST: api/Amplifier
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT: api/Amplifier/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

// GET: api/Amplifier/5
[HttpGet("{id:int:range(1,3)}")]
public IActionResult Get(int id)
{
if (id > AmplifierService.Amplifiers.Length)
{
return NotFound();
}

return Ok(AmplifierService.Amplifiers[id - 1]);
}

// POST: api/Amplifier
[NonAction]
[HttpPost]
public void Post([FromBody] AmpModel value)
{
}

// PUT: api/Amplifier/5
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] AmpModel value)
{
return Conflict($"The object contains modifications to properties that are read only.");
AmplifierService.Amplifiers[id - 1].Enabled = value.Enabled;
AmplifierService.Amplifiers[id - 1].Name = value.Name;
return Ok();
}

// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public void Delete(int id)
{
AmplifierService.Amplifiers[id - 1].Enabled = false;
}
}
}
67 changes: 39 additions & 28 deletions AmpAPI/Controllers/SourceController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AmpAPI.Services;
using AmpAPI.Models;
using AmpAPI.Services;
using AmpAPI.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
Expand All @@ -7,10 +8,10 @@

namespace AmpAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SourceController : ControllerBase
{
[Route("api/[controller]")]
[ApiController]
public class SourceController : ControllerBase
{
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

Expand All @@ -22,34 +23,44 @@ public SourceController(IOptions<AmplifierStackSettings> Settings, IAmplifierSer

// GET: api/Source
[HttpGet]
public IEnumerable<Source> Get()
{
public IEnumerable<Source> Get()
{
return AmplifierService.Sources;
}
}

// GET: api/Source/5
[HttpGet("{id:int:range(1,6)}")]
public Source Get(int id)
{
// GET: api/Source/5
[HttpGet("{id:int:range(1,6)}")]
public Source Get(int id)
{
return AmplifierService.Sources[id - 1];
}

// POST: api/Source
[HttpPost]
public void Post([FromBody] string value)
{
}
// POST: api/Source
[NonAction]
[HttpPost]
public void Post([FromBody] SourceModel value)
{
}

// PUT: api/Source/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// PUT: api/Source/5
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] SourceModel value)
{
if (id != value.ID)
{
return Conflict("Supplied id does not match");
}

AmplifierService.Sources[id - 1].Name = value.Name;
AmplifierService.Sources[id - 1].Enabled = value.Enabled;
return Ok();
}

// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public void Delete(int id)
{
AmplifierService.Sources[id - 1].Enabled = false;
}
}
}
78 changes: 41 additions & 37 deletions AmpAPI/Controllers/ZoneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using MPRSGxZ.Hardware;
using System.Collections.Generic;

namespace AmpAPI.Controllers
{
[Route("api/amplifier/{amplifierID:int:range(1,3)}/[controller]")]
[ApiController]
public class ZoneController : ControllerBase
{
[Route("api/amplifier/{amplifierID:int:range(1,3)}/[controller]")]
[ApiController]
public class ZoneController : ControllerBase
{
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

Expand All @@ -23,22 +22,37 @@ public ZoneController(IOptions<AmplifierStackSettings> Settings, IAmplifierServi

// GET: api/Zone
[HttpGet]
public IEnumerable<Zone> Get([FromRoute]int AmplifierID)
{
return AmplifierService.Amplifiers[AmplifierID - 1].Zones;
}
public IActionResult Get([FromRoute]int AmplifierID)
{
if (AmplifierID > AmplifierService.Amplifiers.Length)
{
return NotFound();
}

return Ok(AmplifierService.Amplifiers[AmplifierID - 1].Zones);
}

// GET: api/Zone/5
[HttpGet("{id:int:range(1,6)}")]
public Zone Get([FromRoute]int AmplifierID, int id)
{
return AmplifierService.Amplifiers[AmplifierID - 1].Zones[id - 1];
// GET: api/Zone/5
[HttpGet("{id:int:range(1,6)}")]
public IActionResult Get([FromRoute]int AmplifierID, int id)
{
if (AmplifierID > AmplifierService.Amplifiers.Length)
{
return NotFound();
}

return Ok(AmplifierService.Amplifiers[AmplifierID - 1].Zones[id - 1]);
}

// PUT: api/Zone/5
[HttpPut("{ZoneID:int:range(1,6)}")]
public void Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone)
public IActionResult Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone)
{
if (AmplifierID > AmplifierService.Amplifiers.Length)
{
return NotFound();
}

var Zone = AmplifierService.Amplifiers[AmplifierID - 1].Zones[ZoneID - 1];

Zone.Power = PutZone.Power;
Expand All @@ -53,30 +67,20 @@ public void Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone)
Zone.Name = PutZone.Name;
Zone.Enabled = PutZone.Enabled;
Zone.VolumeFactor = PutZone.VolumeFactor;
}

// PUT: api/Zone/5
//[HttpPut("{ZoneID:int:range(1,6)}/volume/{value:int:range(0,38)}")]
//public void Put([FromRoute]int AmplifierID, int ZoneID, int value)
//{
//}
return Ok();
}

// PUT: api/Zone/5
//[HttpPut("{ZoneID:int:range(1,6)}/volume")]
//public void Put([FromRoute]int AmplifierID, int ZoneID, [FromBody]int Volume)
//{
//}
[HttpDelete("{ZoneID:int:range(1,6)}")]
public IActionResult Delete([FromRoute] int AmplifierID, int ZoneID)
{
if (AmplifierID > AmplifierService.Amplifiers.Length)
{
return NotFound();
}

// No POST/DELETE as zones are never created/deleted
// POST: api/Zone
//[HttpPost]
//public void Post([FromBody] string value)
//{
//}
// DELETE: api/ApiWithActions/5
//[HttpDelete("{id}")]
// public void Delete(int id)
// {
// }
AmplifierService.Amplifiers[AmplifierID - 1].Zones[ZoneID - 1].Enabled = false;
return Ok();
}
}
}
16 changes: 8 additions & 8 deletions AmpAPI/Hubs/AmpHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.Threading.Tasks;
using AmpAPI.Models;

namespace AmpApi.Hubs
namespace AmpAPI.Hubs
{
public class AmpHub : Hub
{
public async Task SendZoneUpdate(ZoneModel Zone)
{
await Clients.All.SendAsync("ReceiveZoneUpdate", Zone);
}
}
public class AmpHub : Hub
{
public async Task SendZoneUpdate(ZoneModel Zone)
{
await Clients.All.SendAsync("ReceiveZoneUpdate", Zone);
}
}
}
13 changes: 13 additions & 0 deletions AmpAPI/Models/AmpModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace AmpAPI.Models
{
public class AmpModel
{
public int ID { get; set; }
public string Name { get; set; }
public bool Enabled { get; set; }

public ZoneModel[] Zones { get; set; }

public AmpModel() { }
}
}
11 changes: 11 additions & 0 deletions AmpAPI/Models/SourceModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AmpAPI.Models
{
public class SourceModel
{
public int ID { get; set; }
public string Name { get; set; }
public bool Enabled { get; set; }

public SourceModel() { }
}
}
2 changes: 2 additions & 0 deletions AmpAPI/Models/ZoneModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ZoneModel
public decimal VolumeFactor { get; private set; }
public int AdjustedVolume { get; private set; }

internal ZoneModel() { }

public ZoneModel(int AmpID, int ZoneID, bool Power, bool Mute, bool PublicAddress, bool DoNotDisturb, int Volume,
int Treble, int Bass, int Balance, int Source, string Name, bool Enabled, decimal VolumeFactor, int AdjustedVolume)
{
Expand Down
Loading

0 comments on commit b9697ea

Please sign in to comment.