-
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 remote-tracking branch 'origin/0025211'
- Loading branch information
Showing
43 changed files
with
1,918 additions
and
261 deletions.
There are no files selected for viewing
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,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"dotnet-ef": { | ||
"version": "6.0.1", | ||
"commands": [ | ||
"dotnet-ef" | ||
] | ||
} | ||
} | ||
} |
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
197 changes: 197 additions & 0 deletions
197
Library.Encyclopedia.API/Controllers/CommentController.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,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; | ||
} | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.