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,
Customer = x.CustomerName,
Type = x.Type
}).ToArray();
}

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

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

context.SaveChanges();

return new CustomerModel
{
CustomerId = addedCustomer.Entity.CustomerId,
Customer = addedCustomer.Entity.CustomerName,
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 jobId);

CustomerModel CreateCustomer(BaseCustomerModel model);
}
}
43 changes: 30 additions & 13 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,46 @@ public JobService(ApplicationDbContext context)

public JobModel[] GetJobs()
{
return context.Jobs.Select(x => new JobModel
{
JobId = x.JobId,
Engineer = x.Engineer,
When = x.When
}).ToArray();
var jbs = (from j in context.Jobs
from c in context.Customers
.Where(c => c.CustomerId == j.CustomerId)
.DefaultIfEmpty()
select new JobModel
{
JobId = j.JobId,
Engineer = j.Engineer,
When = j.When,
CustName = c.CustomerName
}).ToArray();

return jbs;
}

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();
var jb = (from j in context.Jobs.Where(x => x.JobId == jobId)
from c in context.Customers
.Where(c => c.CustomerId == j.CustomerId)
.DefaultIfEmpty()
select new JobModel
{
JobId = j.JobId,
Engineer = j.Engineer,
When = j.When,
CustName = c.CustomerName,
CustType = c.Type
}).SingleOrDefault();

return jb;
}

public JobModel CreateJob(BaseJobModel model)
{
var addedJob = context.Jobs.Add(new Job
{
Engineer = model.Engineer,
When = model.When
When = model.When,
CustomerId = model.CustID
});

context.SaveChanges();
Expand Down
46 changes: 46 additions & 0 deletions DeveloperTest/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using DeveloperTest.Business.Interfaces;
using DeveloperTest.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DeveloperTest.Controllers
{
[Route("[controller]")]
[ApiController]
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 customer = customerService.CreateCustomer(model);

return Created($"customer/{customer.CustomerId}", customer);
}
}
}
8 changes: 8 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 @@ -24,6 +25,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.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
13 changes: 13 additions & 0 deletions DeveloperTest/Database/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace DeveloperTest.Database.Models
{
public class Customer
{
public int CustomerId { get; set; }

public string CustomerName { get; set; }

public string Type { get; set; }
}
}
2 changes: 2 additions & 0 deletions DeveloperTest/Database/Models/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class Job
public string Engineer { get; set; }

public DateTime When { get; set; }

public int CustomerId { get; set; }
}
}
79 changes: 79 additions & 0 deletions DeveloperTest/Migrations/20220212174933_iiiitester.Designer.cs

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

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

#nullable disable

namespace DeveloperTest.Migrations
{
public partial class iiiitester : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "CustomerId",
table: "Jobs",
type: "int",
nullable: false,
defaultValue: 0);

migrationBuilder.CreateTable(
name: "Customers",
columns: table => new
{
CustomerId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CustomerName = table.Column<string>(type: "nvarchar(max)", nullable: true),
Type = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Customers", x => x.CustomerId);
});
}

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

migrationBuilder.DropColumn(
name: "CustomerId",
table: "Jobs");
}
}
}
23 changes: 23 additions & 0 deletions DeveloperTest/Migrations/ApplicationDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ protected override void BuildModel(ModelBuilder modelBuilder)

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("DeveloperTest.Database.Models.Customer", b =>
{
b.Property<int>("CustomerId")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("CustomerId"), 1L, 1);

b.Property<string>("CustomerName")
.HasColumnType("nvarchar(max)");

b.Property<string>("Type")
.HasColumnType("nvarchar(max)");

b.HasKey("CustomerId");

b.ToTable("Customers");
});

modelBuilder.Entity("DeveloperTest.Database.Models.Job", b =>
{
b.Property<int>("JobId")
Expand All @@ -30,6 +49,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("JobId"), 1L, 1);

b.Property<int>("CustomerId")
.HasColumnType("int");

b.Property<string>("Engineer")
.HasColumnType("nvarchar(max)");

Expand All @@ -44,6 +66,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
new
{
JobId = 1,
CustomerId = 0,
Engineer = "Test",
When = new DateTime(2022, 2, 1, 12, 0, 0, 0, DateTimeKind.Unspecified)
});
Expand Down
Loading