108 lines
3.2 KiB
C#

namespace ActiveAllocator.API.Controllers;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ActiveAllocator.API.Models.Accounts;
using ActiveAllocator.API.Services;
using ActiveAllocator.API.Config;
using System.Collections.Generic;
using ActiveAllocator.API.Entities;
using Microsoft.AspNetCore.Authorization;
using ActiveAllocator.API.Models.Integrations;
[Authorize]
[ApiController]
[Route("[controller]")]
public class IntegrationController : ControllerBase
{
private IIntegrationService _integrationService;
private IMapper _mapper;
private readonly AppSettings _appSettings;
public IntegrationController(
IIntegrationService integrationService,
IMapper mapper,
IOptions<AppSettings> appSettings)
{
_integrationService = integrationService;
_mapper = mapper;
_appSettings = appSettings.Value;
}
[HttpGet]
public IActionResult GetAll()
{
List<IntegrationDTO> DTOs = new();
foreach (var item in _integrationService.GetAll())
DTOs.Add(_mapper.Map<Integration, IntegrationDTO>(item));
return Ok(DTOs);
}
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
return Ok(_mapper.Map<Integration, IntegrationDTO>(_integrationService.GetById(id)));
}
[HttpPost]
public IActionResult Create([FromBody]IntegrationCreateRequest model)
{
_integrationService.Create(model);
return Ok(new { message = "integration created" });
}
[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody]IntegrationUpdateRequest model)
{
_integrationService.Update(id, model);
return Ok(new { message = "integration updated" });
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
_integrationService.Delete(id);
return Ok(new { message = "integration deleted" });
}
[HttpGet("Accounts")]
public IActionResult GetAllAccounts(int? integration_id = null)
{
List<IntegrationAccountDTO> DTOs = new();
foreach (var item in _integrationService.GetAllAccounts(integration_id))
DTOs.Add(_mapper.Map<IntegrationAccount, IntegrationAccountDTO>(item));
return Ok(DTOs);
}
[HttpGet("Accounts/{account_id}")]
public IActionResult GetAccountById(int account_id)
{
return Ok(_mapper.Map<IntegrationAccount, IntegrationAccountDTO>(_integrationService.GetAccountById(account_id)));
}
[HttpPost("Accounts")]
public IActionResult CreateAccount([FromBody]IntegrationAccountCreateRequest model)
{
_integrationService.CreateAccount(model);
return Ok(new { message = "integration account created" });
}
[HttpPut("Accounts/{id}")]
public IActionResult UpdateAccount(int id, [FromBody]IntegrationAccountUpdateRequest model)
{
_integrationService.UpdateAccount(id, model);
return Ok(new { message = "integration account updated" });
}
[HttpDelete("Accounts/{id}")]
public IActionResult DeleteAccount(int id)
{
_integrationService.DeleteAccount(id);
return Ok(new { message = "integration account deleted" });
}
}