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

Mw changes #7

Open
wants to merge 8 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
31 changes: 28 additions & 3 deletions DeveloperTest.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
# Visual Studio Version 17
VisualStudioVersion = 17.1.32407.343
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeveloperTest", "DeveloperTest\DeveloperTest.csproj", "{1D4CF36B-FE89-404A-867A-4986AB960BD1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeveloperTest", "DeveloperTest\DeveloperTest.csproj", "{1D4CF36B-FE89-404A-867A-4986AB960BD1}"
EndProject
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "ui", "ui\", "{4029FB3E-93A6-4943-8D0D-8141DE73AD6B}"
ProjectSection(WebsiteProperties) = preProject
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.0"
Debug.AspNetCompiler.VirtualPath = "/localhost_54959"
Debug.AspNetCompiler.PhysicalPath = "ui\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_54959\"
Debug.AspNetCompiler.Updateable = "true"
Debug.AspNetCompiler.ForceOverwrite = "true"
Debug.AspNetCompiler.FixedNames = "false"
Debug.AspNetCompiler.Debug = "True"
Release.AspNetCompiler.VirtualPath = "/localhost_54959"
Release.AspNetCompiler.PhysicalPath = "ui\"
Release.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_54959\"
Release.AspNetCompiler.Updateable = "true"
Release.AspNetCompiler.ForceOverwrite = "true"
Release.AspNetCompiler.FixedNames = "false"
Release.AspNetCompiler.Debug = "False"
VWDPort = "54959"
SlnRelativePath = "ui\"
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +36,10 @@ Global
{1D4CF36B-FE89-404A-867A-4986AB960BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D4CF36B-FE89-404A-867A-4986AB960BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D4CF36B-FE89-404A-867A-4986AB960BD1}.Release|Any CPU.Build.0 = Release|Any CPU
{4029FB3E-93A6-4943-8D0D-8141DE73AD6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4029FB3E-93A6-4943-8D0D-8141DE73AD6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4029FB3E-93A6-4943-8D0D-8141DE73AD6B}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{4029FB3E-93A6-4943-8D0D-8141DE73AD6B}.Release|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
56 changes: 56 additions & 0 deletions DeveloperTest/Business/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Linq;
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Database;
using DeveloperTest.Database.Models;
using DeveloperTest.Models;

namespace DeveloperTest.Business
{
public class CustomerService : ICustomerService
{
private readonly ApplicationDbContext context;

public CustomerService(ApplicationDbContext context)
{
this.context = context;
}

public CustomerModel[] GetCustomers()
{
return context.Customers.Select(x => new CustomerModel
{
CustomerId = x.CustomerId,
Type = x.Type,
Name = x.Name
}).ToArray();
}

public CustomerModel GetCustomer(int customerId)
{
return context.Customers.Where(x => x.CustomerId == customerId).Select(x => new CustomerModel
{
CustomerId = x.CustomerId,
Type = x.Type,
Name = x.Name
}).SingleOrDefault();
}

public CustomerModel CreateCustomer(BaseCustomerModel model)
{
var addedCustomer = context.Customers.Add(new Customer
{
Type = model.Type,
Name = model.Name
});

context.SaveChanges();

return new CustomerModel
{
CustomerId = addedCustomer.Entity.CustomerId,
Type = addedCustomer.Entity.Type,
Name = addedCustomer.Entity.Name
};
}
}
}
13 changes: 13 additions & 0 deletions DeveloperTest/Business/Interfaces/ICustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DeveloperTest.Models;

namespace DeveloperTest.Business.Interfaces
{
public interface ICustomerService
{
CustomerModel[] GetCustomers();

CustomerModel GetCustomer(int customerId);

CustomerModel CreateCustomer(BaseCustomerModel model);
}
}
50 changes: 50 additions & 0 deletions DeveloperTest/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Microsoft.AspNetCore.Mvc;
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;

namespace DeveloperTest.Controllers
{
[ApiController, Route("[controller]")]
public class CustomerController : ControllerBase
{
private readonly ICustomerService customerService;

public CustomerController(ICustomerService customerService)
{
this.customerService = customerService;
}

[HttpGet]
public IActionResult Get()
{
return Ok(customerService.GetCustomers());
}

[HttpGet("{id}")]
public IActionResult Get(int id)
{
var customer = customerService.GetCustomer(id);

if (customer == null)
{
return NotFound();
}

return Ok(customer);
}

[HttpPost]
public IActionResult Create(BaseCustomerModel model)
{
if (model.Name.Length < 5)
{
return BadRequest("Name must be at least 5.");
}

var customer = customerService.CreateCustomer(model);

return Created($"customer/{customer.CustomerId}", customer);
}
}
}
25 changes: 25 additions & 0 deletions DeveloperTest/Controllers/TypeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace DeveloperTest.Controllers
{
[ApiController, Route("[controller]")]
public class TypeController : ControllerBase
{
private static readonly string[] Types = { "Large", "Small"};

private readonly ILogger<TypeController> _logger;

public TypeController(ILogger<TypeController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<string> Get()
{
return Types;
}
}
}
16 changes: 16 additions & 0 deletions DeveloperTest/Database/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace DeveloperTest.Database
public class ApplicationDbContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
public DbSet<Customer> Customers { get; set; }

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
Expand All @@ -20,17 +21,32 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Job>()
.HasKey(x => x.JobId);

modelBuilder.Entity<Customer>()
.HasKey(x => x.CustomerId);

modelBuilder.Entity<Job>()
.Property(x => x.JobId)
.ValueGeneratedOnAdd();

modelBuilder.Entity<Customer>()
.Property(x => x.CustomerId)
.ValueGeneratedOnAdd();

modelBuilder.Entity<Job>()
.HasData(new Job
{
JobId = 1,
Engineer = "Test",
When = new DateTime(2022, 2, 1, 12, 0, 0)
});

modelBuilder.Entity<Customer>()
.HasData(new Customer
{
CustomerId = 1,
Type = "Large",
Name = "Test Customer"
});
}
}
}
11 changes: 11 additions & 0 deletions DeveloperTest/Database/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DeveloperTest.Database.Models
{
public class Customer
{
public int CustomerId { get; set; }

public string Type { get; set; }

public string Name { get; set; }
}
}
8 changes: 4 additions & 4 deletions DeveloperTest/DeveloperTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.3" />
</ItemGroup>


Expand Down
52 changes: 0 additions & 52 deletions DeveloperTest/Migrations/20200219141416_init.Designer.cs

This file was deleted.

Loading