File Controllers/VehiclesController.cs changed (mode: 100644) (index 660989a..f874130) |
... |
... |
using AutoMapper; |
4 |
4 |
using Microsoft.AspNetCore.Mvc; |
using Microsoft.AspNetCore.Mvc; |
5 |
5 |
using Microsoft.EntityFrameworkCore; |
using Microsoft.EntityFrameworkCore; |
6 |
6 |
using vega.Controllers.Resources; |
using vega.Controllers.Resources; |
|
7 |
|
using vega.Core.Repositories; |
7 |
8 |
using vega.Models; |
using vega.Models; |
8 |
9 |
using vega.Persistence; |
using vega.Persistence; |
9 |
10 |
|
|
|
... |
... |
namespace vega.Controllers |
14 |
15 |
{ |
{ |
15 |
16 |
private readonly IMapper mapper; |
private readonly IMapper mapper; |
16 |
17 |
private readonly VegaDbContext context; |
private readonly VegaDbContext context; |
|
18 |
|
private readonly IVehicleRepository repository; |
17 |
19 |
|
|
18 |
20 |
public VehiclesController( |
public VehiclesController( |
19 |
21 |
IMapper mapper, |
IMapper mapper, |
20 |
|
VegaDbContext context) |
|
|
22 |
|
VegaDbContext context, |
|
23 |
|
IVehicleRepository repository) |
21 |
24 |
{ |
{ |
22 |
25 |
this.mapper = mapper; |
this.mapper = mapper; |
23 |
26 |
this.context = context; |
this.context = context; |
|
27 |
|
this.repository = repository; |
24 |
28 |
} |
} |
25 |
29 |
|
|
26 |
30 |
[HttpPost] |
[HttpPost] |
|
... |
... |
namespace vega.Controllers |
42 |
46 |
context.Vehicles.Add(vehicle); |
context.Vehicles.Add(vehicle); |
43 |
47 |
await context.SaveChangesAsync(); |
await context.SaveChangesAsync(); |
44 |
48 |
|
|
45 |
|
vehicle = await context.Vehicles |
|
46 |
|
.Include(v => v.Features) |
|
47 |
|
.ThenInclude(vf => vf.Feature) |
|
48 |
|
.Include(v => v.Model) |
|
49 |
|
.ThenInclude(m => m.Make) |
|
50 |
|
.SingleOrDefaultAsync(v => v.Id == vehicle.Id); |
|
|
49 |
|
vehicle = await repository.GetVehicle(vehicle.Id); |
51 |
50 |
|
|
52 |
51 |
var result = mapper.Map<Vehicle, VehicleResource>(vehicle); |
var result = mapper.Map<Vehicle, VehicleResource>(vehicle); |
53 |
52 |
return Ok(result); |
return Ok(result); |
|
... |
... |
namespace vega.Controllers |
59 |
58 |
if (!ModelState.IsValid) |
if (!ModelState.IsValid) |
60 |
59 |
return BadRequest(ModelState); |
return BadRequest(ModelState); |
61 |
60 |
|
|
62 |
|
var vehicle = await context.Vehicles |
|
63 |
|
.Include(v => v.Features) |
|
64 |
|
.ThenInclude(vf => vf.Feature) |
|
65 |
|
.Include(v => v.Model) |
|
66 |
|
.ThenInclude(m => m.Make) |
|
67 |
|
.SingleOrDefaultAsync(v => v.Id == id); |
|
|
61 |
|
var vehicle =await repository.GetVehicle(id); |
68 |
62 |
|
|
69 |
63 |
if (vehicle == null) |
if (vehicle == null) |
70 |
64 |
return NotFound(); |
return NotFound(); |
|
... |
... |
namespace vega.Controllers |
95 |
89 |
[HttpGet("{id}")] |
[HttpGet("{id}")] |
96 |
90 |
public async Task<IActionResult> GetVehicle(int id) |
public async Task<IActionResult> GetVehicle(int id) |
97 |
91 |
{ |
{ |
98 |
|
var vehicle = await context.Vehicles |
|
99 |
|
.Include(v => v.Features) |
|
100 |
|
.ThenInclude(vf => vf.Feature) |
|
101 |
|
.Include(v => v.Model) |
|
102 |
|
.ThenInclude(m => m.Make) |
|
103 |
|
.SingleOrDefaultAsync(v => v.Id == id); |
|
|
92 |
|
var vehicle = await repository.GetVehicle(id); |
104 |
93 |
|
|
105 |
94 |
if (vehicle == null) |
if (vehicle == null) |
106 |
95 |
return NotFound(); |
return NotFound(); |
File Persistence/Repositories/VehicleRepository.cs added (mode: 100644) (index 0000000..77173b0) |
|
1 |
|
using System.Threading.Tasks; |
|
2 |
|
using Microsoft.EntityFrameworkCore; |
|
3 |
|
using vega.Core.Repositories; |
|
4 |
|
using vega.Models; |
|
5 |
|
|
|
6 |
|
namespace vega.Persistence.Repositories |
|
7 |
|
{ |
|
8 |
|
public class VehicleRepository : IVehicleRepository |
|
9 |
|
{ |
|
10 |
|
private readonly VegaDbContext context; |
|
11 |
|
public VehicleRepository(VegaDbContext context) |
|
12 |
|
{ |
|
13 |
|
this.context = context; |
|
14 |
|
} |
|
15 |
|
public async Task<Vehicle> GetVehicle(int id) |
|
16 |
|
{ |
|
17 |
|
return await context.Vehicles |
|
18 |
|
.Include(v => v.Features) |
|
19 |
|
.ThenInclude(vf => vf.Feature) |
|
20 |
|
.Include(v => v.Model) |
|
21 |
|
.ThenInclude(m => m.Make) |
|
22 |
|
.SingleOrDefaultAsync(v => v.Id == id); |
|
23 |
|
} |
|
24 |
|
} |
|
25 |
|
} |
File Startup.cs changed (mode: 100644) (index 9433e33..175d242) |
... |
... |
using Microsoft.Extensions.DependencyInjection; |
8 |
8 |
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
9 |
9 |
using vega.Persistence;
|
using vega.Persistence;
|
10 |
10 |
using AutoMapper;
|
using AutoMapper;
|
|
11 |
|
using vega.Persistence.Repositories;
|
|
12 |
|
using vega.Core.Repositories;
|
11 |
13 |
|
|
12 |
14 |
namespace vega
|
namespace vega
|
13 |
15 |
{
|
{
|
|
... |
... |
namespace vega |
23 |
25 |
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
24 |
26 |
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
25 |
27 |
{
|
{
|
|
28 |
|
services.AddScoped<IVehicleRepository, VehicleRepository>();
|
|
29 |
|
|
26 |
30 |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
27 |
31 |
|
|
28 |
32 |
services.AddDbContext<VegaDbContext>(options =>
|
services.AddDbContext<VegaDbContext>(options =>
|