MC #1 Model (Device, ScreenConfiguration, UserInfo, Widget, WidgetAgenda, WidgetHourAndDate, WidgetMessage, WidgetNews, WidgetRadio, WidgetTraffic, WidgetWeather), #1 Controllers (Device) & #2 Service (Device)

This commit is contained in:
ThomasFransolet 2019-08-04 23:26:50 +02:00
parent 0b08ffcf51
commit 36a745d219
17 changed files with 442 additions and 2 deletions

View File

@ -0,0 +1,96 @@
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/device")]
[ApiController]
public class DeviceController : ControllerBase
{
private readonly DeviceService _DeviceService;
public DeviceController(DeviceService DeviceService)
{
_DeviceService = DeviceService;
}
// GET: Devices
/// <summary>
///
/// </summary>
/// <param name="id">Id of the device you want to get informatiun</param>
[HttpGet]
public ActionResult<List<Device>> GetAllDevices()
{
return _DeviceService.GetAll();
}
// GET: Device
/// <summary>
///
/// </summary>
/// <param name="idDevice">Id of the device you want to get information</param>
[HttpGet("{idDevice}")]
public ActionResult<Device> GetDeviceInfo(string idDevice)
{
return _DeviceService.GetDeviceInfo(idDevice);
}
// POST: Device/Create
/// <summary>
///
/// </summary>
[HttpPost("{idDevice}")]
public IActionResult CreateDevice(int idDevice, [FromBody] Device device)
{
if (idDevice == 0)
{
_DeviceService.CreateDevice(device);
return StatusCode(201);
}
return StatusCode(500);
}
// PUT: Device/Update
/// <summary>
///
/// </summary>
[HttpPut("{idDevice}")]
public IActionResult UpdateDevice(int idDevice, [FromBody] Device device)
{
if (idDevice == 0)
{
_DeviceService.Update(device.Id, device);
return StatusCode(201);
}
return StatusCode(500);
}
// Delete: Device/Delete
/// <summary>
///
/// </summary>
[HttpDelete("{idDevice}")]
public IActionResult DeleteDevice(int idDevice, [FromBody] string deviceId)
{
if (idDevice == 0)
{
_DeviceService.Remove(deviceId);
return StatusCode(201);
}
return StatusCode(500);
}
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
using MyCore.Models;
namespace MyCore.Controllers
{
@ -24,6 +25,8 @@ namespace MyCore.Controllers
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
WidgetWeather widgetWeather = new WidgetWeather();
return new string[] { "value1", "value2" };
}

36
MyCore/Models/Device.cs Normal file
View File

@ -0,0 +1,36 @@
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 Device
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("Type")]
public string Type { get; set; }
[BsonElement("Location")]
public string Location { get; set; }
[BsonElement("LocationExplanation")]
public string LocationExplanation { get; set; }
[BsonElement("Height")]
public int Height { get; set; }
[BsonElement("Width")]
public int Width { get; set; }
}
}

View File

@ -0,0 +1,31 @@
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 ScreenConfiguration
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("Type")]
public string Type { get; set; }
[BsonElement("Widgets")]
public Widget[] Widgets { get; set; }
[BsonElement("Height")]
public int Height { get; set; }
[BsonElement("Width")]
public int Width { get; set; }
}
}

View File

@ -17,8 +17,8 @@ namespace MyCore.Models
[BsonElement("Role")]
public string Role { get; set; }
[BsonElement("Username")]
public string Username { get; set; }
[BsonElement("Email")]
public string Email { get; set; } // UNIQUE !..
[BsonElement("Password")]
public string Password { get; set; }
@ -34,6 +34,31 @@ namespace MyCore.Models
[BsonElement("Birthday")]
public string Birthday { get; set; }
[BsonElement("Address")]
public string Address { get; set; }
[BsonElement("City")]
public string City { get; set; }
[BsonElement("State")]
public string State { get; set; }
[BsonElement("Language")]
public string Language { get; set; }
[BsonElement("TimeZone")]
public string TimeZone { get; set; }
[BsonElement("PostalCode")]
public int PostalCode { get; set; }
[BsonElement("ScreenConfigurationIds")]
public int[] ScreenConfigurationIds { get; set; }
[BsonElement("DeviceIds")]
public int[] DeviceIds { get; set; }
}
}

53
MyCore/Models/Widget.cs Normal file
View File

@ -0,0 +1,53 @@
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 Widget
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("DisplayName")]
public string DisplayName { get; set; }
[BsonElement("Type")]
public string Type { get; set; }
[BsonElement("Activated")]
public bool Activated { get; set; }
[BsonElement("Form")]
public string Form { get; set; }
[BsonElement("Font")]
public string Font { get; set; }
[BsonElement("Color")]
public string Color { get; set; }
[BsonElement("Size")]
public string Size { get; set; }
[BsonElement("Width")]
public int Width { get; set; }
[BsonElement("Height")]
public int Height { get; set; }
[BsonElement("PositionX")]
public int PositionX { get; set; }
[BsonElement("PositionY")]
public int PositionY { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetAgenda : Widget
{
[BsonElement("Provider")]
public string Provider { get; set; }
[BsonElement("Link")]
public string Link { get; set; } // ?
[BsonElement("ApiKey")]
public string ApiKey { get; set; } // ?
}
}

View File

@ -0,0 +1,14 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetHourAndDate : Widget
{
[BsonElement("Timezone")]
public string Timezone { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetMessage : Widget
{
[BsonElement("Message")]
public string Message { get; set; }
[BsonElement("Permanent")]
public bool Permanent { get; set; }
[BsonElement("VisibleDuration")]
public int VisibleDuration { get; set; }
[BsonElement("Link")] // LINK RSS FEED Motivational message .. ? / Citations etc
public string Link { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetNews : Widget
{
[BsonElement("Country")]
public string Country { get; set; }
[BsonElement("Provider")]
public string Provider { get; set; }
[BsonElement("Speed")]
public string Speed { get; set; } // Or int ?
}
}

View File

@ -0,0 +1,17 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetRadio : Widget
{
[BsonElement("Provider")]
public string Provider { get; set; }
[BsonElement("Link")]
public string Link { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Models
{
public class WidgetTraffic : Widget
{
[BsonElement("WorkAddress")]
public string WorkAddress { get; set; }
[BsonElement("WorkCity")]
public string WorkCity { get; set; }
[BsonElement("WorkPostCode")]
public string WorkPostCode { get; set; }
[BsonElement("WorkDays")]
public bool[] WorkDays { get; set; } // [1,1,1,1,1,0,0]
}
}

View File

@ -0,0 +1,24 @@
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 WidgetWeather : Widget
{
[BsonElement("City")]
public string City { get; set; }
[BsonElement("Wind")]
public string Wind { get; set; }
[BsonElement("Rain")]
public int Rain { get; set; }
[BsonElement("Nextdays")]
public int Nextdays { 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 DeviceService
{
private readonly IMongoCollection<Device> _devices;
public DeviceService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_devices = database.GetCollection<Device>("Devices");
}
public List<Device> GetAll()
{
return _devices.Find(m => true).ToList();
}
public Device GetDeviceInfo(string id)
{
return _devices.Find<Device>(m => m.Id == id).FirstOrDefault();
}
public Device CreateDevice(Device device)
{
_devices.InsertOne(device);
return device;
}
public void Update(string id, Device deviceIn)
{
_devices.ReplaceOne(device => device.Id == id, deviceIn);
}
public void Remove(Device deviceIn)
{
_devices.DeleteOne(device => device.Id == deviceIn.Id);
}
public void Remove(string id)
{
_devices.DeleteOne(device => device.Id == id);
}
}
}