Skip to content

Working APIs for file upload and download #1

Merged
merged 6 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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