From 74b4fde36db798108a433cee16249bec5e1f728d Mon Sep 17 00:00:00 2001 From: toxicbakery Date: Thu, 22 Aug 2019 14:58:15 -0500 Subject: [PATCH] Making a clean PR of the code --- DocumentApi.sln | 25 +++++++++++++ DocumentApi/DocumentApi.csproj | 14 ++++++++ DocumentApi/DocumentDto.cs | 11 ++++++ DocumentApi/DocumentRepository.cs | 58 ++++++++++++++++++++++++++++++ DocumentApi/DocumentTests.cs | 20 +++++++++++ DocumentApi/DocumentsController.cs | 40 +++++++++++++++++++++ DocumentApi/Program.cs | 9 +++++ README.md | 1 + 8 files changed, 178 insertions(+) create mode 100644 DocumentApi.sln create mode 100644 DocumentApi/DocumentApi.csproj create mode 100644 DocumentApi/DocumentDto.cs create mode 100644 DocumentApi/DocumentRepository.cs create mode 100644 DocumentApi/DocumentTests.cs create mode 100644 DocumentApi/DocumentsController.cs create mode 100644 DocumentApi/Program.cs create mode 100644 README.md diff --git a/DocumentApi.sln b/DocumentApi.sln new file mode 100644 index 0000000..242acdc --- /dev/null +++ b/DocumentApi.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.168 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentApi", "DocumentApi\DocumentApi.csproj", "{3672E5D4-A225-4D5B-AE2B-C94B75AEB2E4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3672E5D4-A225-4D5B-AE2B-C94B75AEB2E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3672E5D4-A225-4D5B-AE2B-C94B75AEB2E4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3672E5D4-A225-4D5B-AE2B-C94B75AEB2E4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3672E5D4-A225-4D5B-AE2B-C94B75AEB2E4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DD3D7BD6-94D8-42DC-834A-5756A7AAEA16} + EndGlobalSection +EndGlobal diff --git a/DocumentApi/DocumentApi.csproj b/DocumentApi/DocumentApi.csproj new file mode 100644 index 0000000..2689357 --- /dev/null +++ b/DocumentApi/DocumentApi.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.1 + + + + + + + + + diff --git a/DocumentApi/DocumentDto.cs b/DocumentApi/DocumentDto.cs new file mode 100644 index 0000000..5e60852 --- /dev/null +++ b/DocumentApi/DocumentDto.cs @@ -0,0 +1,11 @@ +namespace DocumentApi +{ + public class DocumentDto + { + public string Id { get; set; } + + public string Name { get; set; } + + public string Content { get; set; } + } +} \ No newline at end of file diff --git a/DocumentApi/DocumentRepository.cs b/DocumentApi/DocumentRepository.cs new file mode 100644 index 0000000..0df2a26 --- /dev/null +++ b/DocumentApi/DocumentRepository.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Threading.Tasks; + +namespace DocumentApi +{ + public class DocumentRepository + { + public Task> GetAllDocuments() + { + var connection = new SqlConnection("fake-connection-string"); + var command = connection.CreateCommand(); + + var commandText = @" + SELECT * + FROM Documents + "; + + command.CommandText = commandText; + + var documents = new List(); + + var reader = command.ExecuteReaderAsync().Result; + while (reader.ReadAsync().Result) + { + var document = new DocumentDto(); + document.Id = (string)reader[0]; + document.Name = (string)reader[1]; + document.Content = (string)reader[2]; + documents.Add(document); + } + + return Task.FromResult(documents.AsEnumerable()); + } + + public Task UploadDocument(DocumentDto document) + { + var connection = new SqlConnection("fake-connection-string"); + var command = connection.CreateCommand(); + + var commandText = @" + INSERT INTO Documents (Name, Content) + VALUES (" + document.Name + "," + document.Content + @") + "; + + command.CommandText = commandText; + + var reader = command.ExecuteReaderAsync().Result; + while (reader.ReadAsync().Result) + { + document.Id = (string)reader[0]; + } + + return Task.FromResult(document); + } + } +} \ No newline at end of file diff --git a/DocumentApi/DocumentTests.cs b/DocumentApi/DocumentTests.cs new file mode 100644 index 0000000..905ee8b --- /dev/null +++ b/DocumentApi/DocumentTests.cs @@ -0,0 +1,20 @@ +using NUnit.Framework; + +namespace DocumentApi +{ + [TestFixture] + public class DocumentTests + { + [Test] + public void ShouldNotUploadDocumentsThatAlreadyExist() + { + Assert.Fail(); + } + + [Test] + public void ShouldUploadDocumentsThatDoNotAlreadyExist() + { + Assert.Fail(); + } + } +} \ No newline at end of file diff --git a/DocumentApi/DocumentsController.cs b/DocumentApi/DocumentsController.cs new file mode 100644 index 0000000..b06069d --- /dev/null +++ b/DocumentApi/DocumentsController.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace DocumentApi +{ + public class DocumentsController : ControllerBase + { + [HttpGet] + [Route("uploadDocuments")] + public Task>> UploadDocuments(IEnumerable documents) + { + var documentRepository = new DocumentRepository(); + + var currentDocuments = documentRepository.GetAllDocuments().Result; + + foreach (var document in documents) + { + var documentExists = false; + + foreach (var currentDocument in currentDocuments) + { + if (currentDocument.Name == document.Name) + { + documentExists = true; + } + } + + if (!documentExists) + { + var createdDocument = documentRepository.UploadDocument(document).Result; + document.Id = createdDocument.Id; + } + } + + HttpContext.Response.StatusCode = 200; + return Task.FromResult(new ActionResult>(documents)); + } + } +} \ No newline at end of file diff --git a/DocumentApi/Program.cs b/DocumentApi/Program.cs new file mode 100644 index 0000000..9c17bb4 --- /dev/null +++ b/DocumentApi/Program.cs @@ -0,0 +1,9 @@ +namespace DocumentApi +{ + public class Program + { + public static void Main() + { + } + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..067f69c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +This is an interview programming challenge. It is written poorly on purpose. Please don't judge me.