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
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,
Name = x.Name,
Type = x.Type
}).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
{
Name = model.Name,
Type = model.Type
});

context.SaveChanges();

return new CustomerModel
{
CustomerId = addedCustomer.Entity.CustomerId,
Name = addedCustomer.Entity.Name,
Type = addedCustomer.Entity.Type
};
}
}
}
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);
}
}
30 changes: 25 additions & 5 deletions DeveloperTest/Business/JobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DeveloperTest.Database;
using DeveloperTest.Database.Models;
using DeveloperTest.Models;
using Microsoft.EntityFrameworkCore;

namespace DeveloperTest.Business
{
Expand All @@ -21,17 +22,29 @@ public JobModel[] GetJobs()
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
When = x.When,
Customer = x.Customer != null ? new CustomerModel
{
CustomerId = x.Customer.CustomerId,
Name = x.Customer.Name,
Type = x.Customer.Type
} : null
}).ToArray();
}

public JobModel GetJob(int jobId)
{
return context.Jobs.Where(x => x.JobId == jobId).Select(x => new JobModel
return context.Jobs.Include(x => x.Customer).Where(x => x.JobId == jobId).Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
When = x.When,
Customer = x.Customer != null ? new CustomerModel
{
CustomerId = x.Customer.CustomerId,
Name = x.Customer.Name,
Type = x.Customer.Type
} : null
}).SingleOrDefault();
}

Expand All @@ -40,7 +53,8 @@ public JobModel CreateJob(BaseJobModel model)
var addedJob = context.Jobs.Add(new Job
{
Engineer = model.Engineer,
When = model.When
When = model.When,
CustomerId = model.CustomerId
});

context.SaveChanges();
Expand All @@ -49,7 +63,13 @@ public JobModel CreateJob(BaseJobModel model)
{
JobId = addedJob.Entity.JobId,
Engineer = addedJob.Entity.Engineer,
When = addedJob.Entity.When
When = addedJob.Entity.When,
Customer = addedJob.Entity.Customer != null ? new CustomerModel
{
CustomerId = addedJob.Entity.Customer.CustomerId,
Name = addedJob.Entity.Customer.Name,
Type = addedJob.Entity.Customer.Type
} : null
};
}
}
Expand Down
56 changes: 56 additions & 0 deletions DeveloperTest/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Microsoft.AspNetCore.Mvc;
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;
using System.Linq;

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)
{
var allowedTypes = new string[] { "Large", "Small" };
if (!ModelState.IsValid)
{
return BadRequest("Name and Type fields are required and name must be minimum 5 characters");
}
if (!allowedTypes.Any(t => t != model.Name))
{
return BadRequest("Customer type should be either Large or Small");
}

var customer = customerService.CreateCustomer(model);

return Created($"customer/{customer.CustomerId}", customer);
}
}
}
9 changes: 9 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,10 +21,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Job>()
.HasKey(x => x.JobId);


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

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

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

modelBuilder.Entity<Job>()
.HasData(new Job
{
Expand Down
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 Name { get; set; }

public string Type { get; set; }
}
}
4 changes: 4 additions & 0 deletions DeveloperTest/Database/Models/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class Job

public string Engineer { get; set; }

public int? CustomerId { get; set; }

public Customer Customer { get; set; }

public DateTime When { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading