using AutoMapper;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.DataContract;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.Controllers
{
public class JumpController : Base
{
#region Public Constructors
public JumpController(IJumpService jumpService,
IMapper mapper)
{
_jumpService = jumpService;
_mapper = mapper;
}
#endregion Public Constructors
#region Public Methods
///
/// Deletes a jump by its ID.
///
/// The jump ID to delete.
[HttpDelete("{id}")]
[EnableCors]
public void Delete(int id)
{
_jumpService.DeleteJumpById(id);
}
///
/// Retrieves a list of all jumps.
///
/// A JumpListResp containing all jumps and their count.
[HttpGet]
[EnableCors]
public JumpListResp Get()
{
var tmp = _jumpService.GetAllJumps();
var result = new JumpListResp
{
Rows = _mapper.Map>(tmp),
Count = tmp.Count()
};
return result;
}
///
/// Retrieves a page of jumps with pagination.
///
/// The starting index for pagination.
/// The ending index for pagination.
/// A JumpListResp containing the requested page and total count.
[HttpGet("{beginJumpIndex}/{endJumpIndex}")]
[EnableCors]
public JumpListResp Get(int beginJumpIndex, int endJumpIndex)
{
var totalJumps = _jumpService.GetJumpCount();
var tmp = _jumpService.GetJumpsByIndexes(beginJumpIndex, endJumpIndex);
var result = new JumpListResp
{
Rows = _mapper.Map>(tmp),
Count = totalJumps
};
return result;
}
///
/// Retrieves a jump by its ID.
///
/// The jump ID to retrieve.
/// A JumpResp containing the jump details.
[HttpGet("{id}")]
[EnableCors]
public JumpResp Get(int id)
{
var result = _jumpService.GetJumpById(id);
return _mapper.Map(result);
}
///
/// Adds a new jump to the system.
///
/// JumpReq object containing the new jump data.
[HttpPost]
[EnableCors]
public void Post([FromBody] JumpReq value)
{
_jumpService.AddNewJump(value.AircraftId,
value.DropZoneId,
value.JumpTypeId,
value.GearId,
_mapper.Map(value));
}
///
/// Updates an existing jump.
///
/// The jump ID to update.
/// JumpReq object containing the new jump data.
[HttpPut("{id}")]
[EnableCors]
public void Put(int id, [FromBody] JumpReq value)
{
_jumpService.UpdateJump(id, _mapper.Map(value));
}
#endregion Public Methods
#region Private Fields
private readonly IJumpService _jumpService;
private readonly IMapper _mapper;
#endregion Private Fields
}
}