Permalink
Browse files

Merge pull request #7 from danbopes/master

Support sub fields
  • Loading branch information...
ArcticEcho committed Nov 3, 2018
2 parents 827db1e + 25028f7 commit 717b65a254a749f8efe3b633a7778d51ca28ce98
@@ -1,80 +1,108 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Reports.Config;
using Reports.Models;
using Reports.Services.LocalData.Reports;
using Reports.Services.Reports;

namespace Reports.Controllers.API.V2
{
[Route("api/v2/[controller]/[action]")]
public class ReportController : Controller
{
private readonly IReportStore reportStore;
private readonly IIdGenerator idGenerator;
private readonly IOptionsSnapshot<HostingOptions> configAccessor;

public ReportController(IOptionsSnapshot<HostingOptions> ca, IReportStore rs, IIdGenerator idGen)
{
configAccessor = ca;
reportStore = rs;
idGenerator = idGen;
}

[HttpPost]
public IActionResult Create([FromBody]Report report)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

if (report.ExpiresAt == default(DateTime))
{
report.ExpiresAt = DateTime.UtcNow.AddDays(30);
}

report.ID = idGenerator.GetNewId();
report.AppName = report.AppName.Trim();
report.AppURL = report.AppURL?.Trim();
report.CreatedAt = DateTime.UtcNow;

for (var i = 0; i < report.Fields.Length; i++)
for (var j = 0; j < report.Fields[i].Length; j++)
{
var f = report.Fields[i][j];

report.Fields[i][j].ID = "FID" + f.ID;
report.Fields[i][j].Name = f.Name.Trim();
report.Fields[i][j].Value = f.Value.Trim();

if (f.Type == Models.Type.Date)
{
var dt = DateTime.Parse(f.Value);
var epoch = dt.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

report.Fields[i][j].Value = epoch.ToString();
}
}

reportStore.Save(report);

var protocol = "http";

if (configAccessor.Value.TlsSupported)
{
protocol += "s";
}

return Json(new
{
#if RELEASE
ReportURL = $"{protocol}://{configAccessor.Value.FQD}/r/{report.ID}"
#else
ReportURL = $"{protocol}://{Request.Host}/r/{report.ID}"
#endif
});
}
}
}
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Reports.Config;
using Reports.Models;
using Reports.Services.LocalData.Reports;
using Reports.Services.Reports;

namespace Reports.Controllers.API.V2
{
[Route("api/v2/[controller]/[action]")]
public class ReportController : Controller
{
private readonly IReportStore reportStore;
private readonly IIdGenerator idGenerator;
private readonly IOptionsSnapshot<HostingOptions> configAccessor;

public ReportController(IOptionsSnapshot<HostingOptions> ca, IReportStore rs, IIdGenerator idGen)
{
configAccessor = ca;
reportStore = rs;
idGenerator = idGen;
}

private void FormatField(Field f)
{
f.ID = "FID" + f.ID;
f.Name = f.Name.Trim();
f.Value = f.Value?.Trim();
f.Fields = f.Fields ?? new SubField[0];

switch (f.Type)
{
case Models.Type.Date:
{
var dt = DateTime.Parse(f.Value);
var epoch = dt.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

f.Value = epoch.ToString();
}
break;
case Models.Type.Fields:
foreach (var sf in f.Fields)
{
sf.ID = "FID" + sf.ID;
sf.Name = sf.Name.Trim();
sf.Value = sf.Value.Trim();

if (sf.Type == SubType.Date)
{
var dt = DateTime.Parse(sf.Value);
var epoch = dt.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

sf.Value = epoch.ToString();
}
}
break;
}


}

[HttpPost]
public IActionResult Create([FromBody]Report report)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

if (report.ExpiresAt == default(DateTime))
{
report.ExpiresAt = DateTime.UtcNow.AddDays(30);
}

report.ID = idGenerator.GetNewId();
report.AppName = report.AppName.Trim();
report.AppURL = report.AppURL?.Trim();
report.CreatedAt = DateTime.UtcNow;

foreach (var fields in report.Fields)
{
foreach (var field in fields)
{
FormatField(field);
}
}

reportStore.Save(report);

var protocol = "http";

if (configAccessor.Value.TlsSupported)
{
protocol += "s";
}

return Json(new
{
#if RELEASE
ReportURL = $"{protocol}://{configAccessor.Value.FQD}/r/{report.ID}"
#else
ReportURL = $"{protocol}://{Request.Host}/r/{report.ID}"
#endif
});
}
}
}
@@ -22,8 +22,6 @@ public HomeController(IReportStore rs, IMetaStatStore mss)
metaStatStore = mss;
}



[Route("/")]
[Route("/home")]
public IActionResult Index()
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Reports.Models.Validators.Report;
using ZeroFormatter;
@@ -10,9 +12,38 @@ public enum Type
Default,
Link,
Date,
Answers
Answers,
Fields
}

public enum SubType
{
Default,
Link,
Date,
Answers
}

[ZeroFormattable]
public class SubField
{
[Index(0)]
[Required(AllowEmptyStrings = false)]
[RegularExpression("^[a-zA-Z0-9_\\- ]{1,25}")]
public virtual string ID { get; set; }

[Index(1)]
[Required(AllowEmptyStrings = false)]
public virtual string Name { get; set; }

[Index(2)]
[Required(AllowEmptyStrings = false)]
public virtual string Value { get; set; }

[Index(3)]
public virtual SubType Type { get; set; }
}

[ZeroFormattable]
public class Field
{
@@ -26,11 +57,13 @@ public class Field
public virtual string Name { get; set; }

[Index(2)]
[Required(AllowEmptyStrings = false)]
public virtual string Value { get; set; }

[Index(3)]
public virtual Type Type { get; set; }

[Index(4)]
public virtual SubField[] Fields { get; set; }
}

[ZeroFormattable]
@@ -75,11 +75,77 @@
<span class="fieldData">@f.Value</span>
</span>
</div>
}
break;
}
default:
{
}
break;
}
case Reports.Models.Type.Fields:
{
<div class="@f.ID">
<span>
<span class="fieldName">@f.Name</span>:
@foreach (var sf in f.Fields)
{
switch (sf.Type)
{
case Reports.Models.SubType.Link:
{
<div class="@sf.ID reportLink">
<a title="@sf.Name" target="_blank" href="@sf.Value">
@sf.Name
</a>
</div>
break;
}
case Reports.Models.SubType.Date:
{
<div class="@sf.ID">
<span>
<span class="fieldName">@sf.Name</span>:
<span class="timestamp fieldData" data-unixtime="@sf.Value">@sf.Value</span>
</span>
</div>
break;
}
case Reports.Models.SubType.Answers:
{
if (f.Value.ToUpperInvariant().StartsWith("A"))
{
<div class="@sf.ID" style="color: #00AB0B" title="This question has an accepted answer">
<span>
<span class="fieldName">@sf.Name</span>:
<span class="fieldData">@sf.Value.Remove(0, 1)</span>
</span>
</div>
}
else
{
<div class="@sf.ID" title="This question does not have an accepted answer">
<span>
<span class="fieldName">@sf.Name</span>:
<span class="fieldData">@sf.Value</span>
</span>
</div>
}
break;
}
default:
{
<div class="@sf.ID">
<span>
<span class="fieldName">@sf.Name</span>:
<span class="fieldData">@sf.Value</span>
</span>
</div>
break;
}
}
}
</span>
</div>
break;
}
default:
{
<div class="@f.ID">
<span>
<span class="fieldName">@f.Name</span>:

0 comments on commit 717b65a

Please sign in to comment.