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

Dev test #8

Open
wants to merge 4 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
73 changes: 73 additions & 0 deletions DeveloperTest/Business/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Database;
using DeveloperTest.Database.Models;
using DeveloperTest.Enums;
using DeveloperTest.Models;
using System.Linq;

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

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

/// <summary>
/// Create a new customer, and return with Id number.
/// </summary>
/// <param name="model">Customer to be created.</param>
/// <returns></returns>
public CustomerModel CreateCustomer(BaseCustomerModel model)
{
var addedCustomer = context.Customers.Add(new Customer
{
CustomerType = model.CustomerType,
Name = model.Name
});

context.SaveChanges();

return new CustomerModel
{
CustomerId = addedCustomer.Entity.CustomerId,
CustomerType = addedCustomer.Entity.CustomerType,
Name = addedCustomer.Entity.Name
};
}

/// <summary>
/// Get a SINGLE customer
/// </summary>
/// <param name="id">Customer Id</param>
/// <returns></returns>
public CustomerModel GetCustomer(int id)
{
//Will return null if no customer found for provided Id.
return context.Customers.Where(x => x.CustomerId == id).Select(x => new CustomerModel
{
CustomerId = x.CustomerId,
CustomerType = x.CustomerType,
Name = x.Name
}).SingleOrDefault();
}

/// <summary>
/// Get ALL customers
/// </summary>
/// <returns></returns>
public CustomerModel[] GetCustomers()
{
return context.Customers.Select(x => new CustomerModel
{
CustomerId = x.CustomerId,
CustomerType = x.CustomerType,
Name = x.Name
}).ToArray();
}
}
}
11 changes: 11 additions & 0 deletions DeveloperTest/Business/Interfaces/ICustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DeveloperTest.Models;

namespace DeveloperTest.Business.Interfaces
{
public interface ICustomerService
{
CustomerModel[] GetCustomers();
CustomerModel GetCustomer(int id);
CustomerModel CreateCustomer(BaseCustomerModel model);
}
}
40 changes: 26 additions & 14 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 @@ -17,30 +18,40 @@ public JobService(ApplicationDbContext context)

public JobModel[] GetJobs()
{
return context.Jobs.Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
}).ToArray();
return context.Jobs
.Include(x => x.Customer)
.Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When,
CustomerId = x.CustomerId,
Customer = x.Customer != null ? new CustomerModel() { CustomerId = x.Customer.CustomerId, CustomerType = x.Customer.CustomerType, Name = x.Customer.Name } : null

}).ToArray();
}

public JobModel GetJob(int jobId)
{
return context.Jobs.Where(x => x.JobId == jobId).Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
}).SingleOrDefault();
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,
CustomerId = x.CustomerId,
Customer = x.Customer != null ? new CustomerModel() { CustomerId = x.Customer.CustomerId, CustomerType = x.Customer.CustomerType, Name = x.Customer.Name } : null
}).SingleOrDefault();
}

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 +60,8 @@ public JobModel CreateJob(BaseJobModel model)
{
JobId = addedJob.Entity.JobId,
Engineer = addedJob.Entity.Engineer,
When = addedJob.Entity.When
When = addedJob.Entity.When,
CustomerId = addedJob.Entity.CustomerId
};
}
}
Expand Down
49 changes: 49 additions & 0 deletions DeveloperTest/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

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 (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var customer = customerService.CreateCustomer(model);
return Created($"customer/{customer.CustomerId}", customer);
}
}
}
5 changes: 5 additions & 0 deletions DeveloperTest/Controllers/JobController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public IActionResult Get(int id)
[HttpPost]
public IActionResult Create(BaseJobModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

if (model.When.Date < DateTime.Now.Date)
{
return BadRequest("Date cannot be in the past");
Expand Down
12 changes: 12 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 @@ -17,6 +18,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

//Job
modelBuilder.Entity<Job>()
.HasKey(x => x.JobId);

Expand All @@ -31,6 +33,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Engineer = "Test",
When = new DateTime(2022, 2, 1, 12, 0, 0)
});


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

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

}
}
}
12 changes: 12 additions & 0 deletions DeveloperTest/Database/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using DeveloperTest.Enums;

namespace DeveloperTest.Database.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public CustomerType CustomerType { 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 @@ -9,5 +9,9 @@ public class Job
public string Engineer { get; set; }

public DateTime When { get; set; }

//Nullable customer - jobs should be created with one, but existing jobs shouldn't crash out if they don't have one.
public int? CustomerId { get; set; }
public Customer Customer { get; set; }
}
}
8 changes: 8 additions & 0 deletions DeveloperTest/Enums/CustomerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DeveloperTest.Enums
{
public enum CustomerType
{
Large,
Small
}
}
75 changes: 75 additions & 0 deletions DeveloperTest/Migrations/20220518164245_CustomerTable.Designer.cs

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

32 changes: 32 additions & 0 deletions DeveloperTest/Migrations/20220518164245_CustomerTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DeveloperTest.Migrations
{
public partial class CustomerTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Customers",
columns: table => new
{
CustomerId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
CustomerType = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Customers", x => x.CustomerId);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Customers");
}
}
}
Loading