Skip to content
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
25 changes: 25 additions & 0 deletions DocumentApi.sln
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions DocumentApi/DocumentApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.9" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions DocumentApi/DocumentDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DocumentApi
{
public class DocumentDto
{
public string Id { get; set; }

public string Name { get; set; }

public string Content { get; set; }
}
}
58 changes: 58 additions & 0 deletions DocumentApi/DocumentRepository.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<DocumentDto>> GetAllDocuments()
{
var connection = new SqlConnection("fake-connection-string");
var command = connection.CreateCommand();

var commandText = @"
SELECT *
FROM Documents
";

command.CommandText = commandText;

var documents = new List<DocumentDto>();

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<DocumentDto> 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);
}
}
}
20 changes: 20 additions & 0 deletions DocumentApi/DocumentTests.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
40 changes: 40 additions & 0 deletions DocumentApi/DocumentsController.cs
Original file line number Diff line number Diff line change
@@ -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<ActionResult<IEnumerable<DocumentDto>>> UploadDocuments(IEnumerable<DocumentDto> 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<IEnumerable<DocumentDto>>(documents));
}
}
}
9 changes: 9 additions & 0 deletions DocumentApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DocumentApi
{
public class Program
{
public static void Main()
{
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is an interview programming challenge. It is written poorly on purpose. Please don't judge me.