Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/0025211'
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Feb 4, 2022
2 parents 8f786ad + df635d7 commit afebf43
Show file tree
Hide file tree
Showing 43 changed files with 1,918 additions and 261 deletions.
12 changes: 12 additions & 0 deletions Library.Encyclopedia.API/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.1",
"commands": [
"dotnet-ef"
]
}
}
}
4 changes: 4 additions & 0 deletions Library.Encyclopedia.API/Attributes/ApiKeyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace Library.Encyclopedia.API.Attributes
{
/// <summary>
/// This is a custom attribute for API Kay validation
/// Extracts API key from the header and then checks it with the API KEY hardcoded in appsettings
/// </summary>
[AttributeUsage(validOn: AttributeTargets.Class | AttributeTargets.Method)]
public class ApiKeyAttribute : Attribute, IAsyncActionFilter
{
Expand Down
197 changes: 197 additions & 0 deletions Library.Encyclopedia.API/Controllers/CommentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using Library.Encyclopedia.DataAccess;
using Library.Encyclopedia.DataAccess.DataAccess;
using Library.Encyclopedia.Entity.Interfaces;
using Library.Encyclopedia.Entity.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Threading.Tasks;

namespace Library.Encyclopedia.API.Controllers
{
[ApiController]
[Route("[controller]")]
public class CommentController : ControllerBase
{
private readonly ILogger<CommentController> _logger;
private readonly ICommentsDataAccess dataAccess;
private readonly ILikesDataAccess likesDataAccess;

public CommentController(ILogger<CommentController> logger, IApplicationDbContext dbContext)
{
this._logger = logger;
dataAccess = new CommentsDataAccess(dbContext);
likesDataAccess = new LikesDataAccess(dbContext);
}

[HttpGet]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public async Task<IActionResult> Get(int offset = 0, int limit = 10, bool asc = false)
{
try
{
var result = await dataAccess.Get(offset, limit, asc);
var likes = await likesDataAccess.GetLikes(result.Result.Select(s => s.Id).ToList());

if (likes != null)
{
foreach (var item in result.Result)
item.Likes = likes.ContainsKey(item.Id) ? likes[item.Id] : 0;
}

if (result != null)
return Ok(result);
else
return StatusCode(500);
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpPost("Resolve/{id}")]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public async Task<IActionResult> ResolveComment(Guid id, bool flag = true)
{
try
{
await dataAccess.Resolve(id, flag);

return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
try
{
var result = await dataAccess.Get(id);
var likes = await likesDataAccess.GetLikes(result.Select(s => s.Id).ToList());

if (likes != null)
{
foreach (var item in result)
item.Likes = likes.ContainsKey(item.Id) ? likes[item.Id] : 0;
}

if (result != null)
return Ok(result);
else
return StatusCode(500);
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpPost]
[Authorize]
public async Task<IActionResult> Post([FromBody]Comments comments)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);
comments.UserName = usr.DisplayName;
comments.UserSid = usr.Sid.Value;

var result = await dataAccess.CreateComment(comments);

if (result != null)
return Ok(result);
else
return StatusCode(500);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpDelete("{id}")]
[Authorize]
public async Task<IActionResult> Delete(Guid id)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);

if (usr.GetAuthorizationGroups().Select(s => s.Name).Contains("EncyclopediaAdministrators"))
await dataAccess.DeleteComment(id, string.Empty, true);
else
await dataAccess.DeleteComment(id, usr.Sid.Value);

return Ok();
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpPut("{id}")]
[Authorize]
public async Task<IActionResult> Update(Guid id, string comment)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);

await dataAccess.UpdateComment(id, comment, usr.Sid.Value);

return Ok();
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

[HttpPost("Like/{id}")]
[Authorize]
public async Task<IActionResult> LikeComment(Guid id, bool flag = true)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);

await likesDataAccess.Like(id, usr.Sid.Value, flag);

return Ok();
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}
}
}
48 changes: 48 additions & 0 deletions Library.Encyclopedia.API/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Library.Encyclopedia.DataAccess.Email;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Threading.Tasks;

namespace Library.Encyclopedia.API.Controllers
{
[ApiController]
[Route("[controller]")]
public class EmailController : ControllerBase
{
public readonly EmailAdapter adapter;
private readonly ILogger<EmailController> logger;

public EmailController(ILogger<EmailController> logger, IConfiguration configuration)
{
adapter = new EmailAdapter(configuration);
this.logger = logger;
}

[HttpPost]
[Authorize]
public async Task<IActionResult> SendEmail([FromBody]string content)
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);
await adapter.SendEmail(usr.DisplayName, usr.EmailAddress, content);

return Ok();
}
}
catch (Exception ex)
{
logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}
}
}
Loading

0 comments on commit afebf43

Please sign in to comment.