-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from PFW-Helmke-Library/0025211
Working APIs for file upload and download
- Loading branch information
Showing
81 changed files
with
1,210 additions
and
15,513 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Library.Encyclopedia.API.Attributes | ||
{ | ||
[AttributeUsage(validOn: AttributeTargets.Class | AttributeTargets.Method)] | ||
public class ApiKeyAttribute : Attribute, IAsyncActionFilter | ||
{ | ||
private const string APIKEYNAME = "ApiKey"; | ||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
if (!context.HttpContext.Request.Headers.TryGetValue(APIKEYNAME, out var extractedApiKey)) | ||
{ | ||
context.Result = new ContentResult() | ||
{ | ||
StatusCode = 401, | ||
Content = "Api Key was not provided" | ||
}; | ||
return; | ||
} | ||
|
||
var appSettings = context.HttpContext.RequestServices.GetRequiredService<IConfiguration>(); | ||
|
||
var apiKey = appSettings.GetValue<string>(APIKEYNAME); | ||
|
||
if (!apiKey.Equals(extractedApiKey)) | ||
{ | ||
context.Result = new ContentResult() | ||
{ | ||
StatusCode = 401, | ||
Content = "Api Key is not valid" | ||
}; | ||
return; | ||
} | ||
|
||
await next(); | ||
} | ||
} | ||
} |
265 changes: 265 additions & 0 deletions
265
Library.Encyclopedia.API/Controllers/EncylopediaController.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
using Library.Encyclopedia.API.Attributes; | ||
using Library.Encyclopedia.DataAccess; | ||
using Library.Encyclopedia.DataAccess.DataAccess; | ||
using Library.Encyclopedia.Entity.Interfaces; | ||
using Library.Encyclopedia.Entity.Models; | ||
using Library.Encyclopedia.Entity.Models.External; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Library.Encyclopedia.Controllers | ||
{ | ||
/// <summary> | ||
/// CRUD operations on Main Table | ||
/// </summary> | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class EncylopediaController : ControllerBase | ||
{ | ||
private readonly ILogger<EncylopediaController> _logger; | ||
private readonly IMainDataAccess mainDataAccess; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="logger"></param> | ||
/// <param name="dbContext"></param> | ||
public EncylopediaController(ILogger<EncylopediaController> logger, IApplicationDbContext dbContext) | ||
{ | ||
_logger = logger; | ||
this.mainDataAccess = new MainDataAccess(dbContext); | ||
} | ||
|
||
/// <summary> | ||
/// Get all items based on search query | ||
/// </summary> | ||
/// <param name="query"></param> | ||
/// <param name="offset"></param> | ||
/// <param name="size"></param> | ||
/// <param name="asc"></param> | ||
/// <returns></returns> | ||
[HttpGet] | ||
public async Task<IActionResult> Get(string query, | ||
int offset = 0, | ||
int size = 10, | ||
bool asc = true) | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.GetAsync(query, offset, size, asc); | ||
if (response == null) | ||
{ | ||
return StatusCode(204); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get all items based on category | ||
/// </summary> | ||
/// <param name="query"></param> | ||
/// <param name="offset"></param> | ||
/// <param name="size"></param> | ||
/// <param name="asc"></param> | ||
/// <returns></returns> | ||
[HttpGet("category")] | ||
public async Task<IActionResult> GetByCategory(string category, | ||
int offset = 0, | ||
int size = 10, | ||
bool asc = true) | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.GetByCategoryAsync(category, offset, size, asc); | ||
|
||
if (response == null) | ||
{ | ||
return StatusCode(204); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get all items based on starting alphabet | ||
/// </summary> | ||
/// <param name="query"></param> | ||
/// <param name="offset"></param> | ||
/// <param name="size"></param> | ||
/// <param name="asc"></param> | ||
/// <returns></returns> | ||
[HttpGet("alphabet")] | ||
public async Task<IActionResult> GetByStartingAlphabet(char alphabet, | ||
int offset = 0, | ||
int size = 10, | ||
bool asc = true) | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, size, asc); | ||
|
||
if (response == null) | ||
{ | ||
return StatusCode(204); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get an entry by id | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet("{id}")] | ||
public async Task<IActionResult> Get(Guid id) | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.GetAsync(id); | ||
|
||
if (response == null) | ||
{ | ||
return StatusCode(204); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create new entry | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpPost] | ||
[ApiKey] | ||
//[Authorize] | ||
public async Task<IActionResult> Create([FromBody]Main model) | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.CreateAsync(model); | ||
|
||
if (response == null) | ||
{ | ||
return StatusCode(500); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create new entry | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpPut("{id}")] | ||
[ApiKey] | ||
//[Authorize] | ||
public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model) | ||
{ | ||
try | ||
{ | ||
await mainDataAccess.UpdateAsync(id, model); | ||
|
||
return Ok(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create new entry | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpDelete("{id}")] | ||
[ApiKey] | ||
//[Authorize] | ||
public async Task<IActionResult> Delete(Guid id) | ||
{ | ||
try | ||
{ | ||
await mainDataAccess.DeleteAsync(id); | ||
|
||
return Ok(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get all categories | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet("fetchallcategories")] | ||
public async Task<IActionResult> GetAllCategories() | ||
{ | ||
try | ||
{ | ||
var response = await mainDataAccess.GetAllCategoriesAsync(); | ||
|
||
if (response == null) | ||
{ | ||
return StatusCode(204); | ||
} | ||
else | ||
{ | ||
return Ok(response); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.