WebService Serveur (API) - Json FromBody handle

This commit is contained in:
ThomasFransolet 2019-03-27 23:23:47 +01:00
parent d010167dff
commit 6370257154
9 changed files with 216 additions and 1 deletions

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MyCore.Models;
using MyCore.Services;
namespace MyCore.Controllers
{
[Authorize(Roles = "Admin")]
[Route("api/iot")]
[ApiController]
public class IOTController : Controller
{
private readonly IoTDeviceService _ioTDeviceService;
public IOTController(IoTDeviceService ioTDeviceService)
{
_ioTDeviceService = ioTDeviceService;
}
// GET: IOT
/// <summary>
/// Retrieve all SmartPrinterMessage
/// </summary>
[HttpGet]
public ActionResult<List<SmartPrinterMessage>> Get(int id)
{
return _ioTDeviceService.Get();
}
// POST: IOT/Create
/// <summary>
/// It's the method to post data from mqtt broker to Database (Thanks Rpi!)
/// </summary>
[HttpPost("{idDevice}")]
public IActionResult PostToDB(int idDevice, [FromBody] SmartPrinterMessage[] content)
{
if(idDevice == 0) {
foreach (SmartPrinterMessage message in content) {
_ioTDeviceService.Create(message);
}
return StatusCode(201);
}
return StatusCode(500);
}
/*// GET: IOT/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: IOT/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
// GET: IOT/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: IOT/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}*/
}
}

View File

@ -0,0 +1,28 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class SmartPrinterMessage
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Time")]
public string Time { get; set; }
[BsonElement("Temperature")]
public double Temperature { get; set; }
[BsonElement("Pressure")]
public double Pressure { get; set; }
[BsonElement("Smoke")]
public int Smoke { get; set; }
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using MyCore.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
namespace MyCore.Services
{
public class IoTDeviceService
{
private readonly IMongoCollection<SmartPrinterMessage> _SmartPrinterMessages;
public IoTDeviceService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_SmartPrinterMessages = database.GetCollection<SmartPrinterMessage> ("IoTDevices");
}
public List<SmartPrinterMessage> Get()
{
return _SmartPrinterMessages.Find(m => true).ToList();
}
public SmartPrinterMessage Get(string id)
{
return _SmartPrinterMessages.Find<SmartPrinterMessage>(m => m.Id == id).FirstOrDefault();
}
public SmartPrinterMessage Create(SmartPrinterMessage message)
{
_SmartPrinterMessages.InsertOne(message);
return message;
}
/*public void Update(string id, Book bookIn)
{
_books.ReplaceOne(book => book.Id == id, bookIn);
}
public void Remove(Book bookIn)
{
_books.DeleteOne(book => book.Id == bookIn.Id);
}
public void Remove(string id)
{
_books.DeleteOne(book => book.Id == id);
}*/
}
}

View File

@ -34,6 +34,7 @@ namespace MyCore
{
// Add the service (test purpose)
services.AddScoped<BookService>();
services.AddScoped<IoTDeviceService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

View File

@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"BookstoreDb": "mongodb://localhost:27017"
"BookstoreDb": "mongodb://localhost:27017",
"MyCoreDb": "mongodb://localhost:27017"
},
"Logging": {
"LogLevel": {

View File

@ -4,6 +4,21 @@
<name>MyCore</name>
</assembly>
<members>
<member name="M:MyCore.Controllers.IOTController.Get(System.Int32)">
<summary>
Retrieve all SmartPrinterMessage
</summary>
</member>
<member name="M:MyCore.Controllers.IOTController.PostToDB(System.Int32,MyCore.Models.SmartPrinterMessage[])">
<summary>
It's the method to post data from mqtt broker to Database (Thanks Rpi!)
</summary>
</member>
<member name="M:MyCore.Controllers.MQTTController.GetToPublishMqtt">
<summary>
It's a mqtt publish test ! :)
</summary>
</member>
<member name="M:MyCore.Controllers.ValuesController.Get">
<summary>
It's a test ! :)

View File

@ -4,6 +4,21 @@
<name>MyCore</name>
</assembly>
<members>
<member name="M:MyCore.Controllers.IOTController.Get(System.Int32)">
<summary>
Retrieve all SmartPrinterMessage
</summary>
</member>
<member name="M:MyCore.Controllers.IOTController.PostToDB(System.Int32,MyCore.Models.SmartPrinterMessage[])">
<summary>
It's the method to post data from mqtt broker to Database (Thanks Rpi!)
</summary>
</member>
<member name="M:MyCore.Controllers.MQTTController.GetToPublishMqtt">
<summary>
It's a mqtt publish test ! :)
</summary>
</member>
<member name="M:MyCore.Controllers.ValuesController.Get">
<summary>
It's a test ! :)