Add AutoMapper and update the interface of business services

This commit is contained in:
Sébastien André
2019-09-25 19:38:52 +02:00
parent cb105c13af
commit a2718e4ba7
17 changed files with 102 additions and 20 deletions

View File

@@ -1,10 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface namespace skydiveLogs_api.Business.Interface
{ {
public interface IAircraftService public interface IAircraftService
{ {
IEnumerable<skydiveLogs_api.DataContract.AircraftResp> GetAllAircrafts();
skydiveLogs_api.DataContract.AircraftResp GetAircraftById(int id);
void AddNewAircraft(Aircraft aircraft);
void UpdateAircraft(int id, Aircraft aircraft);
void DeleteAircraftById(int id);
} }
} }

View File

@@ -1,10 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface namespace skydiveLogs_api.Business.Interface
{ {
public interface IDropZoneService public interface IDropZoneService
{ {
IEnumerable<skydiveLogs_api.DataContract.DropZoneResp> GetAllDzs();
skydiveLogs_api.DataContract.DropZoneResp GetDzById(int id);
void DeleteDzById(int id);
void UpdateDz(int id, DropZone dropZone);
void AddNewDz(DropZone dropZone);
} }
} }

View File

@@ -1,10 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface namespace skydiveLogs_api.Business.Interface
{ {
public interface IJumpService public interface IJumpService
{ {
IEnumerable<skydiveLogs_api.DataContract.JumpResp> GetAllJumps();
skydiveLogs_api.DataContract.JumpResp GetJumpById(int id);
void AddNewJump(Jump jump);
void UpdateJump(int id, Jump jump);
void DeleteJumpById(int id);
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using skydiveLogs_api.Model;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -6,5 +7,14 @@ namespace skydiveLogs_api.Business.Interface
{ {
public interface IJumpTypeService public interface IJumpTypeService
{ {
IEnumerable<JumpType> GetAllJumpTypes();
JumpType GetJumpTypeById(int id);
void AddNewJumpType(JumpType value);
void UpdateJumpType(int id, JumpType value);
void DeleteJumpTypeById(int id);
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace skydiveLogs_api.Model namespace skydiveLogs_api.Model
{ {
class Aircraft public class Aircraft
{ {
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace skydiveLogs_api.Model namespace skydiveLogs_api.Model
{ {
class DropZone public class DropZone
{ {
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace skydiveLogs_api.Model namespace skydiveLogs_api.Model
{ {
class Gear public class Gear
{ {
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace skydiveLogs_api.Model namespace skydiveLogs_api.Model
{ {
class Jump public class Jump
{ {
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Text;
namespace skydiveLogs_api.Model namespace skydiveLogs_api.Model
{ {
class JumpType public class JumpType
{ {
} }
} }

View File

@@ -2,10 +2,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.Business.Interface; using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
@@ -13,43 +15,49 @@ namespace skydiveLogs_api.Controllers
[ApiController] [ApiController]
public class AircraftController : ControllerBase public class AircraftController : ControllerBase
{ {
public AircraftController(IAircraftService aircraftService) public AircraftController(IAircraftService aircraftService,
IMapper mapper)
{ {
_aircraftService = aircraftService; _aircraftService = aircraftService;
_mapper = mapper;
} }
// GET: api/Aircraft // GET: api/Aircraft
[HttpGet] [HttpGet]
public IEnumerable<AircraftResp> Get() public IEnumerable<AircraftResp> Get()
{ {
return new AircraftResp[] { "value1", "value2" }; return _aircraftService.GetAllAircrafts();
} }
// GET: api/Aircraft/5 // GET: api/Aircraft/5
[HttpGet("{id}", Name = "Get")] [HttpGet("{id}", Name = "Get")]
public AircraftResp Get(int id) public AircraftResp Get(int id)
{ {
return "value"; return _aircraftService.GetAircraftById(id);
} }
// POST: api/Aircraft // POST: api/Aircraft
[HttpPost] [HttpPost]
public void Post([FromBody] AircraftReq value) public void Post([FromBody] AircraftReq value)
{ {
_aircraftService.AddNewAircraft(_mapper.Map<Aircraft>(value));
} }
// PUT: api/Aircraft/5 // PUT: api/Aircraft/5
[HttpPut("{id}")] [HttpPut("{id}")]
public void Put(int id, [FromBody] AircraftReq value) public void Put(int id, [FromBody] AircraftReq value)
{ {
_aircraftService.UpdateAircraft(id, _mapper.Map<Aircraft>(value));
} }
// DELETE: api/ApiWithActions/5 // DELETE: api/ApiWithActions/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
_aircraftService.DeleteAircraftById(id);
} }
private readonly IAircraftService _aircraftService; private readonly IAircraftService _aircraftService;
private readonly IMapper _mapper;
} }
} }

View File

@@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.Business.Interface; using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
using AutoMapper;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
@@ -13,43 +15,49 @@ namespace skydiveLogs_api.Controllers
[ApiController] [ApiController]
public class DropZoneController : ControllerBase public class DropZoneController : ControllerBase
{ {
public DropZoneController(IDropZoneService dropZoneService) public DropZoneController(IDropZoneService dropZoneService,
IMapper mapper)
{ {
_dropZoneService = dropZoneService; _dropZoneService = dropZoneService;
_mapper = mapper;
} }
// GET: api/DropZone // GET: api/DropZone
[HttpGet] [HttpGet]
public IEnumerable<DropZoneResp> Get() public IEnumerable<DropZoneResp> Get()
{ {
return new DropZoneResp[] { "value1", "value2" }; return _dropZoneService.GetAllDzs();
} }
// GET: api/DropZone/5 // GET: api/DropZone/5
[HttpGet("{id}", Name = "Get")] [HttpGet("{id}", Name = "Get")]
public DropZoneResp Get(int id) public DropZoneResp Get(int id)
{ {
return "value"; return _dropZoneService.GetDzById(id);
} }
// POST: api/DropZone // POST: api/DropZone
[HttpPost] [HttpPost]
public void Post([FromBody] DropZoneReq value) public void Post([FromBody] DropZoneReq value)
{ {
_dropZoneService.AddNewDz(_mapper.Map<DropZone>(value));
} }
// PUT: api/DropZone/5 // PUT: api/DropZone/5
[HttpPut("{id}")] [HttpPut("{id}")]
public void Put(int id, [FromBody] DropZoneReq value) public void Put(int id, [FromBody] DropZoneReq value)
{ {
_dropZoneService.UpdateDz(id, _mapper.Map<DropZone>(value));
} }
// DELETE: api/ApiWithActions/5 // DELETE: api/ApiWithActions/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
_dropZoneService.DeleteDzById(id);
} }
private readonly IDropZoneService _dropZoneService; private readonly IDropZoneService _dropZoneService;
private readonly IMapper _mapper;
} }
} }

View File

@@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.Business.Interface; using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
using AutoMapper;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
@@ -13,43 +15,49 @@ namespace skydiveLogs_api.Controllers
[ApiController] [ApiController]
public class JumpController : ControllerBase public class JumpController : ControllerBase
{ {
public JumpController(IJumpService jumpService) public JumpController(IJumpService jumpService,
IMapper mapper)
{ {
_jumpService = jumpService; _jumpService = jumpService;
_mapper = mapper;
} }
// GET: api/Jump // GET: api/Jump
[HttpGet] [HttpGet]
public IEnumerable<JumpResp> Get() public IEnumerable<JumpResp> Get()
{ {
return new JumpResp[] { "value1", "value2" }; return _jumpService.GetAllJumps();
} }
// GET: api/Jump/5 // GET: api/Jump/5
[HttpGet("{id}", Name = "Get")] [HttpGet("{id}", Name = "Get")]
public JumpResp Get(int id) public JumpResp Get(int id)
{ {
return "value"; return _jumpService.GetJumpById(id);
} }
// POST: api/Jump // POST: api/Jump
[HttpPost] [HttpPost]
public void Post([FromBody] JumpReq value) public void Post([FromBody] JumpReq value)
{ {
_jumpService.AddNewJump(_mapper.Map<Jump>(value));
} }
// PUT: api/Jump/5 // PUT: api/Jump/5
[HttpPut("{id}")] [HttpPut("{id}")]
public void Put(int id, [FromBody] JumpReq value) public void Put(int id, [FromBody] JumpReq value)
{ {
_jumpService.UpdateJump(id, _mapper.Map<Jump>(value));
} }
// DELETE: api/ApiWithActions/5 // DELETE: api/ApiWithActions/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
_jumpService.DeleteJumpById(id);
} }
private readonly IJumpService _jumpService; private readonly IJumpService _jumpService;
private readonly IMapper _mapper;
} }
} }

View File

@@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.Business.Interface; using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
using AutoMapper;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
@@ -13,43 +15,49 @@ namespace skydiveLogs_api.Controllers
[ApiController] [ApiController]
public class JumpTypeController : ControllerBase public class JumpTypeController : ControllerBase
{ {
public JumpTypeController(IJumpTypeService jumpTypeService) public JumpTypeController(IJumpTypeService jumpTypeService,
IMapper mapper)
{ {
_jumpTypeService = jumpTypeService; _jumpTypeService = jumpTypeService;
_mapper = mapper;
} }
// GET: api/JumpType // GET: api/JumpType
[HttpGet] [HttpGet]
public IEnumerable<JumpTypeResp> Get() public IEnumerable<JumpTypeResp> Get()
{ {
return new JumpTypeResp[] { "value1", "value2" }; return _jumpTypeService.GetAllJumpTypes();
} }
// GET: api/JumpType/5 // GET: api/JumpType/5
[HttpGet("{id}", Name = "Get")] [HttpGet("{id}", Name = "Get")]
public JumpTypeResp Get(int id) public JumpTypeResp Get(int id)
{ {
return "value"; return _jumpTypeService.GetJumpTypeById(id);
} }
// POST: api/JumpType // POST: api/JumpType
[HttpPost] [HttpPost]
public void Post([FromBody] JumpTypeReq value) public void Post([FromBody] JumpTypeReq value)
{ {
_jumpTypeService.AddNewJumpType(_mapper.Map<JumpType>(value));
} }
// PUT: api/JumpType/5 // PUT: api/JumpType/5
[HttpPut("{id}")] [HttpPut("{id}")]
public void Put(int id, [FromBody] JumpTypeReq value) public void Put(int id, [FromBody] JumpTypeReq value)
{ {
_jumpTypeService.UpdateJumpType(id, _mapper.Map<JumpType>(value));
} }
// DELETE: api/ApiWithActions/5 // DELETE: api/ApiWithActions/5
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
_jumpTypeService.DeleteJumpTypeById(id);
} }
private readonly IJumpTypeService _jumpTypeService; private readonly IJumpTypeService _jumpTypeService;
private readonly IMapper _mapper;
} }
} }

View File

@@ -0,0 +1,18 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
namespace skydiveLogs_api.Mapper
{
public class ModelProfile : Profile
{
public ModelProfile()
{
CreateMap<DataContract.JumpReq, Model.Jump>();
CreateMap<DataContract.JumpTypeReq, Model.JumpType>();
CreateMap<DataContract.AircraftReq, Model.Aircraft>();
CreateMap<DataContract.DropZoneReq, Model.DropZone>();
}
}
}

View File

@@ -10,7 +10,6 @@ namespace skydiveLogs_api
CreateWebHostBuilder(args).Build().Run(); CreateWebHostBuilder(args).Build().Run();
} }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
} }
} }

View File

@@ -3,8 +3,10 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using skydiveLogs_api.Ioc; using skydiveLogs_api.Ioc;
namespace skydiveLogs_api namespace skydiveLogs_api
{ {
public class Startup public class Startup
@@ -24,6 +26,8 @@ namespace skydiveLogs_api
// IoC // IoC
var iocService = new IocService(services); var iocService = new IocService(services);
iocService.Configure(); iocService.Configure();
services.AddAutoMapper(typeof(Mapper.ModelProfile));
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />