Created
October 14, 2021 01:49
-
-
Save salasrob/4007718c6c7930ef972f2c7f6a94e024 to your computer and use it in GitHub Desktop.
.NET Core - Provider details RESTful API controller - Welrus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
using Sabio.Models; | |
using Sabio.Models.Domain.Provider; | |
using Sabio.Models.Requests.ProviderDetails; | |
using Sabio.Services; | |
using Sabio.Services.Interfaces; | |
using Sabio.Services.ProviderDetailsService; | |
using Sabio.Web.Controllers; | |
using Sabio.Web.Models.Responses; | |
using System; | |
using System.Collections.Generic; | |
using System.Data.SqlClient; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Sabio.Web.Api.Controllers.ProviderDetailsGetApiController | |
{ | |
[Route("api/providers")] | |
[ApiController] | |
public class ProviderDetailsApiController : BaseApiController | |
{ | |
private IProviderDetailsService _service = null; | |
private IAuthenticationService<int> _authService = null; | |
public ProviderDetailsApiController(IProviderDetailsService service | |
, ILogger<ProviderDetailsApiController> logger | |
, IAuthenticationService<int> authService) : base(logger) | |
{ | |
_service = service; | |
_authService = authService; | |
} | |
[HttpGet()] | |
public ActionResult<ItemResponse<Provider>>GetByUserId() | |
{ | |
int userId = 0; | |
int iCode = 200; | |
BaseResponse response = null; | |
try | |
{ | |
userId = _authService.GetCurrentUserId(); | |
Provider provider = _service.GetByUserId(userId); | |
if (provider == null) | |
{ | |
iCode = 400; | |
response = new ErrorResponse("Application Resource not found."); | |
} | |
else | |
{ | |
response = new ItemResponse<Provider> { Item = provider }; | |
} | |
} | |
catch (Exception ex) | |
{ | |
iCode = 500; | |
base.Logger.LogError(ex.ToString()); | |
response = new ErrorResponse($"Generic Error: {ex.Message}"); | |
} | |
return StatusCode(iCode, response); | |
} | |
[HttpGet("{id:int}/details")] | |
public ActionResult<ItemResponse<ProviderDetail>>Get(int id) | |
{ | |
int iCode = 200; | |
BaseResponse response = null; | |
try | |
{ | |
List<ProviderDetail> provDetails = _service.Get(id); | |
if (provDetails == null) | |
{ | |
iCode = 400; | |
response = new ErrorResponse("Application Resource not found."); | |
} | |
else | |
{ | |
response = new ItemResponse<List<ProviderDetail>> { Item = provDetails }; | |
} | |
} | |
catch (Exception ex) | |
{ | |
iCode = 500; | |
base.Logger.LogError(ex.ToString()); | |
response = new ErrorResponse($"Generic Error: {ex.Message}"); | |
} | |
return StatusCode(iCode, response); | |
} | |
[HttpGet("paginate")] | |
public ActionResult<ItemResponse<Paged<ProviderDetail>>> GetAll(int pageIndex, int pageSize) | |
{ | |
int iCode = 200; | |
BaseResponse response = null; | |
try | |
{ | |
Paged<ProviderDetail> provider = _service.GetAll(pageIndex, pageSize); | |
if (provider == null) | |
{ | |
iCode = 400; | |
response = new ErrorResponse("Application Resource not found."); | |
} | |
else | |
{ | |
response = new ItemResponse<Paged<ProviderDetail>> { Item = provider }; | |
} | |
} | |
catch (Exception ex) | |
{ | |
iCode = 500; | |
base.Logger.LogError(ex.ToString()); | |
response = new ErrorResponse($"Generic Error: {ex.Message}"); | |
} | |
return StatusCode(iCode, response); | |
} | |
[HttpGet("search")] | |
public ActionResult<ItemResponse<Paged<ProviderDetail>>> Search(int pageIndex, int pageSize, string Query) | |
{ | |
int iCode = 200; | |
BaseResponse response = null; | |
try | |
{ | |
Paged<ProviderDetail> provider = _service.Search(pageIndex, pageSize, Query); | |
if (provider == null) | |
{ | |
iCode = 400; | |
response = new ErrorResponse("Application Resource not found."); | |
} | |
else | |
{ | |
response = new ItemResponse<Paged<ProviderDetail>> { Item = provider }; | |
} | |
} | |
catch (Exception ex) | |
{ | |
iCode = 500; | |
base.Logger.LogError(ex.ToString()); | |
response = new ErrorResponse($"Generic Error: {ex.Message}"); | |
} | |
return StatusCode(iCode, response); | |
} | |
[HttpPost] | |
public ActionResult<ItemResponse<int>> Create(ProviderAddRequest model) | |
{ | |
int code = 201; | |
BaseResponse response = null; | |
try | |
{ | |
int userId = _authService.GetCurrentUserId(); | |
int id = _service.Add(model, userId); | |
response = new ItemResponse<int> { Item = id }; // Provider Id | |
} | |
catch (Exception ex) | |
{ | |
code = 500; | |
response = new ErrorResponse($"ArgumentException Error: {ex.ToString() }"); | |
base.Logger.LogError(ex.ToString()); | |
} | |
return StatusCode(code, response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment