Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
111 changes: 111 additions & 0 deletions Back End Challenge/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Http;
using Microsoft.AspNetCore.JsonPatch;
using Newtonsoft.Json;

namespace Headstorm_Front_End_Challenge
{
public class HomeController : ApiController
{
//Used double since it didn't specify that numbers would be integers.
List<double> StoredList = new List<double>();
//Didn't want to deal with permissions so just stored it on a spared drive I have.
string fileName = "E:\\test.txt";


[HttpPost]
public bool GetNumberList([FromBody] List<string> list)
{
//false is error.
if (list.Count != 500)
{
return false;
}

foreach (string item in list)
{
double temp;
if (Double.TryParse(item, out temp))
{
StoredList.Add(temp);
}
else
{
//false is error.
return false;
}
}

StoredList.Sort(); //From the wording it implies the list should be sorted at this point.

//In the real world I'd put the file IO in separate functions, but didn't want to add a bunch of files to this project.
File.Delete(fileName);
TextWriter tw = new StreamWriter(@fileName, true);

tw.WriteLine(JsonConvert.SerializeObject(StoredList));

tw.Flush();
tw.Close();

return true;
}

[HttpGet]
public string SendNumberList()
{
if (File.Exists(fileName))
{
String JSONtxt = File.ReadAllText(fileName);
StoredList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<double>>(JSONtxt);
}

return JsonConvert.SerializeObject(StoredList);
}

[HttpPatch]
public bool ModifyNumberList([FromBody] JsonPatchDocument patchDoc)
{
if (File.Exists(fileName))
{
String JSONtxt = File.ReadAllText(fileName);
StoredList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<double>>(JSONtxt);
}
else
{
return false;
}

try
{
if (patchDoc != null)
{
patchDoc.ApplyTo(StoredList);
}

//The instructions said to put the value in the appropriate location in the list, so I figure sorting it will get everything in order again if they place it in the wrong spot.
StoredList.Sort();
//Instructions unclear if the list should grow or remain at 500, but if it should remain at 500 then remove the last element from the list here.

File.Delete(fileName);
TextWriter tw = new StreamWriter(@fileName, true);

tw.WriteLine(JsonConvert.SerializeObject(StoredList));

tw.Flush();
tw.Close();

return true;
}
catch (Exception ex)
{
return false;
}

}


}
}
Binary file not shown.
78 changes: 78 additions & 0 deletions Database Challenge/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Headstorm_Database_Challenge
{

public class Record
{
public int RecordID { get; set; }
public string Name { get; set; }
public string CellPhone { get; set; }
public string WorkPhone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public int? BasicWidgetOrder { get; set; }
public int? AdvancedWidgetOrder { get; set; }
public bool ProtectionPlan { get; set; }
}


class Program
{
public static string SQLQueryMaker()
{
StringBuilder result = new StringBuilder();
List<Record> records = new List<Record>();
string fileName = System.Environment.CurrentDirectory + "\\DatabaseRecords.json";

if (File.Exists(fileName))
{
String JSONtxt = File.ReadAllText(fileName);
records = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(JSONtxt);
}

result.AppendLine("DECLARE @CustomerID AS INT");
result.AppendLine("");

foreach (Record record in records)
{
//Insert the customer into the Customers table if it's a new customer, otherwise get the existing customer ID to link the order.
//Usually would use parameters instead of adding strings, but that doesn't work when printing a query to a file.
//I haven't tested the SQL statements themselves, but they should either work or be close to working.
result.AppendLine("IF " + record.Email + " IN (SELECT Email FROM Customers)");
result.AppendLine("BEGIN");
result.AppendLine("SELECT @CustomerID = CustomerID FROM Customers WHERE Email = " + record.Email);
result.AppendLine("END");
result.AppendLine("ELSE");
result.AppendLine("BEGIN");
result.AppendLine("INSERT INTO Orders (Name, CellPhone, WorkPhone, Email, Address)");
result.AppendLine("OUTPUT Inserted.ID INTO @CustomerID");
result.AppendLine("VALUES( " + record.Name + ", " + record.CellPhone + ", " + record.WorkPhone + ", " + record.Email + ", " + record.Address + " )");
result.AppendLine("END");
result.AppendLine("");
result.AppendLine("INSERT INTO Orders (CustomerID, RecordID, WidgetOrder, Advanced, ProtectionPlan) VALUES (@CustomerID, " + record.RecordID + ", " + (record.BasicWidgetOrder == null ? record.AdvancedWidgetOrder : record.BasicWidgetOrder) + ", " + (record.BasicWidgetOrder == null ? 1 : 0) + ", " + (record.ProtectionPlan ? 1 : 0 ) + " )");
result.AppendLine("");
}

return result.ToString();
}

static void Main(string[] args)
{
string fileName = System.Environment.CurrentDirectory + "\\test.txt";
File.Delete(fileName);
TextWriter tw = new StreamWriter(@fileName, true);

tw.Write(SQLQueryMaker());

tw.Flush();
tw.Close();
}
}
}
41 changes: 41 additions & 0 deletions Front End Challenge/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@page
@model IndexModel
@{
ViewData["Title"] = "AG Inc Contact Page";
}

<script>
function Submit() {
document.ContactForm.submit();
console.log("Name:" + document.getElementById("NameText").value);
console.log("Number:" + document.getElementById("NumberText").value);
console.log("Comment:" + document.getElementById("CommentText").value);
}
</script>
<!--I didn't do anything to make it look good, just put the form together as instructed.-->
<div class="text-center">
<h1 class="display-4">AG Inc Contact Form</h1>
<form id="ContactForm" name="ContactForm">
<label>Name</label>
<br />
<input id="NameText" type="text" />
<br />
<label>Number</label>
<br />
<!--Limit to 12 for phone numbers, 10 digits for the numbers and 2 for dashes if the user inputs them.
I was going to auto-map the format but it turned out to be too complicated for such a small project.-->
<input id="NumberText" type="tel" maxlength="12" />
<br />
<label>Comment</label>
<br />
<input id="CommentText" type="text" />
<br />
</form>
<br />
<!-- reCAPTCHA doesn't work since I don't have a site to register a key to, but the documentation said this is the way to do it.-->
<button id="SubmitButton" onclick="Submit()" form="ContactForm" type="submit" value="Submit" class="g-recaptcha" data-sitekey="reCAPTCHA_site_key"
data-action='submit'>
Submit
</button>
<br />
</div>
54 changes: 54 additions & 0 deletions Front End Challenge/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<script src="https://www.google.com/recaptcha/api.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="favicon.ico" type="image/ico" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">AG Inc</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2021 - Headstorm_Front_End_Challenge - <a asp-area="" asp-page="/Privacy">Privacy</a>
</div>
</footer>

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
Binary file added Front End Challenge/favicon.ico
Binary file not shown.
Loading