tcc/ASP.NET/Controllers/SimulationController.cs

54 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using tcc_app.Models;
namespace TCC.Controllers
{
[ApiController]
[Route("simulation")]
public class SimulationController : ControllerBase
{
public SimulationController()
{
}
[HttpGet("harmonic-progression")]
public async Task<IActionResult> GetHarmonicProgression([FromQuery] int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += 1.0f / i;
}
return Ok(sum);
}
[HttpPost("json")]
public async Task<IActionResult> PostJson([FromBody] PersonJson person)
{
return Ok(person);
}
[HttpPost("protobuf")]
public async Task<IActionResult> PostProtobuf()
{
PersonProto person;
byte[] response;
using (var stream = Request.BodyReader.AsStream())
{
person = ProtoBuf.Serializer.Deserialize<PersonProto>(stream);
}
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(stream, person);
response = stream.ToArray();
}
await Response.Body.WriteAsync(response, 0, response.Length);
return new EmptyResult();
}
}
}