Skip to content

Commit

Permalink
Create AlertsController and DataService methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Mar 24, 2020
1 parent bbfceee commit 68a0cc0
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 103 deletions.
100 changes: 91 additions & 9 deletions Server/API/AlertsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Remotely.Server.Auth;
using Remotely.Server.Services;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Remotely.Server.API
Expand All @@ -13,27 +19,103 @@ namespace Remotely.Server.API
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public class AlertsController : ControllerBase
{
public AlertsController(DataService dataService, IEmailSenderEx emailSender)
{
DataService = dataService;
EmailSender = emailSender;
}

private DataService DataService { get; }
private IEmailSenderEx EmailSender { get; }

[HttpPost("Create")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public async Task<IActionResult> Create(AlertOptions alertOptions)
{
Request.Headers.TryGetValue("OrganizationID", out var orgID);

DataService.WriteEvent("Alert created. Alert Options: " + JsonSerializer.Serialize(alertOptions), orgID);

if (alertOptions.ShouldAlert)
{
var alert = new Alert()
try
{
var alert = new Alert()
{
CreatedOn = DateTimeOffset.Now,
DeviceID = alertOptions.AlertDeviceID,
Message = alertOptions.AlertMessage,
OrganizationID = orgID
};
await DataService.AddAlert(alert);
}
catch (Exception ex)
{
CreatedOn = DateTimeOffset.Now,
DeviceID = alertOptions.AlertDeviceID,
Message = alertOptions.AlertMessage
};
// TODO: Add alert.
DataService.WriteEvent(ex, orgID);
}
}

// TODO: Email.
if (alertOptions.ShouldEmail)
{
try
{
await EmailSender.SendEmailAsync(alertOptions.EmailTo,
alertOptions.EmailSubject,
alertOptions.EmailBody,
orgID);
}
catch (Exception ex)
{
DataService.WriteEvent(ex, orgID);
}

}

// TODO: API request.
if (alertOptions.ShouldSendApiRequest)
{
try
{
var httpRequest = WebRequest.CreateHttp(alertOptions.ApiRequestUrl);
httpRequest.Method = alertOptions.ApiRequestMethod;
httpRequest.ContentType = "application/json";
foreach (var header in alertOptions.ApiRequestHeaders)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
using (var rs = httpRequest.GetRequestStream())
using (var sw = new StreamWriter(rs))
{
sw.Write(alertOptions.ApiRequestBody);
}
var response = (HttpWebResponse)httpRequest.GetResponse();
DataService.WriteEvent($"Alert API Response Status: {response.StatusCode}.", orgID);
}
catch (Exception ex)
{
DataService.WriteEvent(ex, orgID);
}

}

return Ok();
}

[HttpPost("Delete/{alertID}")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public async Task<IActionResult> Delete(string alertID)
{
Request.Headers.TryGetValue("OrganizationID", out var orgID);

var alert = await DataService.GetAlert(alertID);

if (alert?.OrganizationID == orgID)
{
await DataService.DeleteAlert(alert);

return Ok();
}

return Unauthorized();
}
}
}
8 changes: 8 additions & 0 deletions Server/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> context)
{
}

public DbSet<Alert> Alerts { get; set; }

public DbSet<ApiToken> ApiTokens { get; set; }

public DbSet<CommandResult> CommandResults { get; set; }
Expand Down Expand Up @@ -74,6 +76,9 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<Organization>()
.HasMany(x => x.ApiTokens)
.WithOne(x => x.Organization);
builder.Entity<Organization>()
.HasMany(x => x.Alerts)
.WithOne(x => x.Organization);


builder.Entity<CommandResult>()
Expand Down Expand Up @@ -122,6 +127,9 @@ protected override void OnModelCreating(ModelBuilder builder)

builder.Entity<Device>()
.HasIndex(x => x.DeviceName);
builder.Entity<Device>()
.HasMany(x => x.Alerts)
.WithOne(x => x.Device);

builder.Entity<ApiToken>()
.HasIndex(x => x.Token);
Expand Down
Loading

0 comments on commit 68a0cc0

Please sign in to comment.