Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e8280d2
added migrations
pientkrz Mar 27, 2022
b74999c
added new db se representing customers and added running migrations d…
pientkrz Mar 27, 2022
623d074
added customer types, mappings, serivces and data access
pientkrz Mar 27, 2022
9038613
added readme
pientkrz Mar 27, 2022
2f858be
added endpoint for returning customer types; added enum conversion, e…
pientkrz Mar 27, 2022
2824c04
type is now string for proper error handling
pientkrz Mar 27, 2022
08bf81b
remvoed obsolete method
pientkrz Mar 27, 2022
182fc95
added ef console logging for developement environment
pientkrz Mar 27, 2022
238200b
added task functional requirements
pientkrz Mar 27, 2022
42ffe8a
added initial customer handling
pientkrz Mar 27, 2022
5c17dd9
udpated @types/node
pientkrz Mar 27, 2022
0588156
handing of subscriptions
pientkrz Mar 27, 2022
a2648d5
added previous migrations after db wipe with additional nullable fore…
pientkrz Apr 1, 2022
8885362
including customer job query; added missing handling for enumerations…
pientkrz Apr 1, 2022
f08a0c0
updated todolist
pientkrz Apr 1, 2022
ab374bc
added subscription handling for customer component
pientkrz Apr 1, 2022
850aa81
added unknown customer handling; added unsubscribing on ngDestroy
pientkrz Apr 1, 2022
12a443e
made accessor private for subscription array
pientkrz Apr 1, 2022
fd8bc05
added minimum name length validation
pientkrz Apr 1, 2022
d3b93f9
udpdated todos
pientkrz Apr 1, 2022
123e35c
converted task to value task and swapped out arrays with ienumerables
pientkrz Apr 1, 2022
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
66 changes: 66 additions & 0 deletions DeveloperTest/Business/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Database;
using DeveloperTest.Database.Models;
using DeveloperTest.Mappers;
using DeveloperTest.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

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

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

public async ValueTask<CustomerModel> CreateCustomerAsync(BaseCustomerModel model)
{
if (!Enum.TryParse(value: model.Type, ignoreCase: true, out CustomerType result))
{
throw new ArgumentException($"{nameof(CustomerType)} must be valid.");
}

var customer = context.Customers.Add(new Customer
{
Name = model.Name,
Type = result
});

await context.SaveChangesAsync();

return customer
.Entity
.ToModel();
}

public async ValueTask<CustomerModel> GetCustomerAsync(int id)
{
var customer = await context.Customers.FindAsync(id);
return customer?.ToModel();
}

public async ValueTask<IEnumerable<CustomerModel>> GetCustomersAsync()
{
return await context.Customers
.Select(customer => new CustomerModel
{
CustomerId = customer.CustomerId,
Name = customer.Name,
Type = customer.Type.ToString()
})
.ToArrayAsync();
}

public IEnumerable<string> GetTypes()
{
return typeof(CustomerType).GetEnumNames();
}
}
}
14 changes: 14 additions & 0 deletions DeveloperTest/Business/Interfaces/ICustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using DeveloperTest.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DeveloperTest.Business.Interfaces
{
public interface ICustomerService
{
ValueTask<IEnumerable<CustomerModel>> GetCustomersAsync();
ValueTask<CustomerModel> GetCustomerAsync(int id);
ValueTask<CustomerModel> CreateCustomerAsync(BaseCustomerModel model);
IEnumerable<string> GetTypes();
}
}
42 changes: 28 additions & 14 deletions DeveloperTest/Business/JobService.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
using System.Linq;
using System;
using System.Linq;
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Database;
using DeveloperTest.Database.Models;
using DeveloperTest.Models;
using Microsoft.EntityFrameworkCore;

namespace DeveloperTest.Business
{
public class JobService : IJobService
{
private readonly ApplicationDbContext context;
private readonly ICustomerService customerService;

public JobService(ApplicationDbContext context)
public JobService(ApplicationDbContext context,
ICustomerService customerService)
{
this.context = context;
this.customerService = customerService;
}

public JobModel[] GetJobs()
{
return context.Jobs.Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
}).ToArray();
return context.Jobs
.Include(j => j.Customer)
.Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When,
CustomerName = x.Customer.Name
}).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(c => c.Customer)
.Where(x => x.JobId == jobId).Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
CustomerId = x.CustomerId.Value,
CustomerName = x.Customer.Name,
When = x.When
}).SingleOrDefault();
}

public JobModel CreateJob(BaseJobModel model)
{
var addedJob = context.Jobs.Add(new Job
{
Engineer = model.Engineer,
CustomerId = model.CustomerId.HasValue
? model.CustomerId.Value : throw new ArgumentException($"{nameof(model.CustomerId)} cannot be null"),
When = model.When
});

Expand Down
64 changes: 64 additions & 0 deletions DeveloperTest/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Microsoft.AspNetCore.Mvc;
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;
using System.Threading.Tasks;
using DeveloperTest.Database.Models;

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

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

[HttpGet]
public async ValueTask<IActionResult> Get()
{
return Ok(await customerService.GetCustomersAsync());
}

[HttpGet("{id}")]
public async ValueTask<IActionResult> Get(int id)
{
var customer = await customerService.GetCustomerAsync(id);

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

return Ok(customer);
}

[HttpPost]
public async ValueTask<IActionResult> Create(BaseCustomerModel model)
{
const int minCustomerNameLength = 5;
if (model.Name.Length < 5)
{
return BadRequest($"Customer name cannot have less than {minCustomerNameLength} characters");
}

if (!Enum.TryParse(value: model.Type, ignoreCase: true, out CustomerType result))
{
return BadRequest($"Customer type must be chosen.");
}

var customer = await customerService.CreateCustomerAsync(model);

return Created($"customer/{customer.CustomerId}", customer);
}

[HttpGet("types")]
public IActionResult GetTypes()
{
return Ok(customerService.GetTypes());
}
}
}
7 changes: 6 additions & 1 deletion DeveloperTest/Controllers/JobController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public IActionResult Create(BaseJobModel model)
{
if (model.When.Date < DateTime.Now.Date)
{
return BadRequest("Date cannot be in the past");
return BadRequest("Date cannot be in the past.");
}

if (model.CustomerId is null)
{
return BadRequest("Job must have a customer.");
}

var job = jobService.CreateJob(model);
Expand Down
23 changes: 22 additions & 1 deletion 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 @@ -15,7 +16,20 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ba

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>()
.HasKey(x => x.CustomerId);

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

modelBuilder.Entity<Customer>()
.HasData(new Customer
{
CustomerId = 1,
Name = "TestCustomer",
Type = CustomerType.Small
});

modelBuilder.Entity<Job>()
.HasKey(x => x.JobId);
Expand All @@ -24,13 +38,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.Property(x => x.JobId)
.ValueGeneratedOnAdd();

modelBuilder.Entity<Job>()
.HasOne(j => j.Customer)
.WithMany(c => c.Jobs);

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

base.OnModelCreating(modelBuilder);

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

namespace DeveloperTest.Database.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public CustomerType Type { get; set; }
public virtual ICollection<Job> Jobs { get; set; }
}

public enum CustomerType
{
Small,
Large
}
}
4 changes: 2 additions & 2 deletions DeveloperTest/Database/Models/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace DeveloperTest.Database.Models
public class Job
{
public int JobId { get; set; }

public string Engineer { get; set; }

public DateTime When { get; set; }
public int? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
}
31 changes: 31 additions & 0 deletions DeveloperTest/Mappers/CustomerMappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using DeveloperTest.Database.Models;
using DeveloperTest.Models;
using System;

namespace DeveloperTest.Mappers
{
public static class CustomerMappingExtensions
{
public static Customer ToCustomerDbEntity(this CustomerModel source)
{
return new Customer()
{
CustomerId = source.CustomerId,
Name = source.Name,
Type = Enum.TryParse(source.Type, out CustomerType type)
? type
: throw new ArgumentException("Invalid customer type. Cannot parse the customer type."),
};
}

public static CustomerModel ToModel(this Customer source)
{
return new CustomerModel()
{
CustomerId = source.CustomerId,
Name = source.Name,
Type = source.Type.ToString()
};
}
}
}
Loading