Skip to content

Commit

Permalink
Merge pull request #1 from PFW-Helmke-Library/0025211
Browse files Browse the repository at this point in the history
Working APIs for file upload and download
  • Loading branch information
mazumdes authored Jan 21, 2022
2 parents 437db1e + aad7d09 commit 8f786ad
Show file tree
Hide file tree
Showing 81 changed files with 1,210 additions and 15,513 deletions.
37 changes: 0 additions & 37 deletions Library.Encyclopedia.API.sln

This file was deleted.

43 changes: 43 additions & 0 deletions Library.Encyclopedia.API/Attributes/ApiKeyAttribute.cs
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 Library.Encyclopedia.API/Controllers/EncylopediaController.cs
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;
}
}
}
}
Loading

0 comments on commit 8f786ad

Please sign in to comment.