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

Brandon Henry - Added submissions for Frontend, Most of Backend, and Part 1 of Database #102

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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions challenges/backend/BackendChallenge/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net5.0/BackendChallenge.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},

{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net5.0/BackendChallenge.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
42 changes: 42 additions & 0 deletions challenges/backend/BackendChallenge/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/BackendChallenge.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/BackendChallenge.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/BackendChallenge.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
18 changes: 18 additions & 0 deletions challenges/backend/BackendChallenge/BackendChallenge.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using BackendChallenge.Services;
using BackendChallenge.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

/*
I've had some trouble with errors that I'm not quite able to identify; seems as though it's consistently had something to do with my
"user" not having proper permissions for my own local api, and I'm pretty stuck on getting that working.
However, I think the actual logic of my api endpoints is sound, though I haven't been able to properly test due to the strange runtime
errors; may have something to do with my Connection String in appsettings.json. Feel free to pick it apart nonetheless and I apologize
I wasn't able to deliver a fully functional api for the purposes of this challenge. Thanks for looking over my code regardless though!
*/

namespace BackendChallenge.Controllers
{
[Route("data/")]
[ApiController]
public class RandomNumbersController : ControllerBase
{
private readonly IBackendChallengeService _backendChallengeService;
private readonly BackendChallengeContext _backendChallengeContext;

public RandomNumbersController(IBackendChallengeService backendChallengeService, BackendChallengeContext backendChallengeContext)
{
_backendChallengeService = backendChallengeService;
_backendChallengeContext = backendChallengeContext;
}

[HttpGet]
public async Task<IActionResult> GetNumbers()
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
var response = _backendChallengeService.GetSortedNumbers();
return Ok(response);
}
catch (Exception ex)
{
var exception = ex.Message;
return Problem("There was a problem retrieving sorted numbers.", null, 500);
}
}

[HttpPost]
public async Task<IActionResult> CreateNumbers([FromBody] int[] randomNumbers)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
if (randomNumbers.Length == 5)
{
foreach (var value in randomNumbers) {
var type = value.GetType();
if (!type.Equals(typeof(int))) {
return Problem("Provided value is not a list of numbers.", null, 500);
}
}
// Clear the db context before adding new numbers. This is running with the assumption of only needing to
// store one list of numbers at a time. This would change if supporting multiples lists was the goal.
var set = _backendChallengeContext.Set<RandomNumber>();
if (set.Any()) {
_backendChallengeContext.RandomNumbers.RemoveRange(_backendChallengeContext.RandomNumbers);
};
foreach (var number in randomNumbers) {
var randomNum = new RandomNumber();
// Could be used in the future to help scale this to multiple sets of numbers.
// randomNum.group = key;
randomNum.number = number;
_backendChallengeContext.Add(randomNum);
}
await _backendChallengeContext.SaveChangesAsync();
return Ok(randomNumbers);
}
else {
return Problem("Provided list of numbers does not have length of 500.", null, 500);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return Problem("There was a problem creating the list.", null, 500);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using BackendChallenge.Models;

public class BackendChallengeContext : DbContext
{
public BackendChallengeContext (DbContextOptions<BackendChallengeContext> options)
: base(options)
{
}

public DbSet<RandomNumber> RandomNumbers { get; set; }

public Task<int> SaveChangesAsync()
{
return base.SaveChangesAsync();
}
}
12 changes: 12 additions & 0 deletions challenges/backend/BackendChallenge/Models/RandomNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BackendChallenge.Models
{
// Class used to represent the individual numbers added to the dbContext.
public class RandomNumber
{
public int id {get;set;}
public int number { get; set; }

// Could be used in the future to help scale this to multiple lists of numbers.
// public int group { get; set; }
}
}
20 changes: 20 additions & 0 deletions challenges/backend/BackendChallenge/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace BackendChallenge
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
31 changes: 31 additions & 0 deletions challenges/backend/BackendChallenge/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35050",
"sslPort": 44349
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"BackendChallenge": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "api/randomnumbers",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;

namespace BackendChallenge.Services
{
public class BackendChallengeService : IBackendChallengeService
{
private readonly IConfiguration _config;
private BackendChallengeContext _context;

public BackendChallengeService(IConfiguration config, BackendChallengeContext context)
{
_config = config;
_context = context;
}

public int[] GetSortedNumbers()
{
List<int> numList = new List<int>();

/* Could use the idea of grouping to scale this to multiple lists as mentioned in RandomNumbers.cs.
Would need to just check for group # prior to adding to array of nums to be sorted and returned.
Currently just runs through the first 500 nums in the database because there should only ever be
500 nums in the database at a time with the current design of the POST request. */
for (var i = 0;i < 500;i++) {
numList.Add(_context.RandomNumbers.Find(i).number);
}
// Sort the numbers pulled from the dbContext before returning.
numList.ToArray();
numList.Sort();
return numList.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BackendChallenge.Services
{
public interface IBackendChallengeService
{
int[] GetSortedNumbers();
}
}
Loading