5b7952877e
Major refactor of AdminController and related services to support full CRUD for candidate resume, projects, hobbies, skills, academics, experiences, certifications, and contact info, all using access token claims for candidate identity. Introduced AdminService and expanded IResumeRepository for granular entity management. Updated DTOs for upsert operations and date support. Improved API versioning (Asp.Versioning.Mvc), Swagger integration, and middleware setup. Added unit test project. Enhanced error handling, documentation, and mapping.
155 lines
5.0 KiB
C#
155 lines
5.0 KiB
C#
using Asp.Versioning;
|
|
using AutoMapper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PortBlog.API.Entities;
|
|
using PortBlog.API.Models;
|
|
using PortBlog.API.Repositories.Contracts;
|
|
|
|
|
|
namespace PortBlog.API.Controllers
|
|
{
|
|
[Route("blog/api/v{version:apiVersion}/posts")]
|
|
[ApiController]
|
|
[ApiVersion("1.0")]
|
|
public class BlogController : ControllerBase
|
|
{
|
|
private readonly ILogger<BlogController> _logger;
|
|
private readonly IBlogRepository _blogRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository, IMapper mapper)
|
|
{
|
|
_logger = logger;
|
|
_blogRepository = blogRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
[HttpGet("GetPostLikesAndViews")]
|
|
public async Task<ActionResult<PostMetricsDto>> GetPostLikesAndViews(string blogUrl, string postSlug)
|
|
{
|
|
var postMetrics = new PostMetricsDto();
|
|
|
|
if(!await _blogRepository.BlogExistsAsync(blogUrl))
|
|
{
|
|
_logger.LogInformation($"Blog with id {blogUrl} wasn't found when fetching post likes and views.");
|
|
return NotFound();
|
|
}
|
|
|
|
if (!await _blogRepository.PostExistsAsync(blogUrl, postSlug))
|
|
{
|
|
_logger.LogInformation($"Post with id {postSlug} wasn't found when fetching post likes and views.");
|
|
return Ok(postMetrics);
|
|
}
|
|
|
|
var post = await _blogRepository.GetPostAsync(blogUrl, postSlug);
|
|
|
|
post.Views++;
|
|
|
|
_blogRepository.UpdatePost(post);
|
|
|
|
await _blogRepository.SaveChangesAsync();
|
|
|
|
postMetrics = _mapper.Map<PostMetricsDto>(post);
|
|
|
|
postMetrics.PostExists = true;
|
|
|
|
return Ok(postMetrics);
|
|
}
|
|
|
|
[HttpPost("CreatePost")]
|
|
public async Task<ActionResult<PostMetricsDto>> CreatePost([FromBody] PostCreationDto post)
|
|
{
|
|
if (!await _blogRepository.BlogExistsAsync(post.BlogUrl))
|
|
{
|
|
_logger.LogInformation($"Blog with id {post.BlogUrl} wasn't found when fetching post likes and views.");
|
|
return NotFound();
|
|
}
|
|
|
|
var postEntityExists = await _blogRepository.GetPostAsync(post.BlogUrl, post.Slug);
|
|
|
|
if (postEntityExists == null)
|
|
{
|
|
postEntityExists = _mapper.Map<Post>(post);
|
|
|
|
postEntityExists.Views++;
|
|
|
|
_blogRepository.AddPost(postEntityExists);
|
|
}
|
|
else
|
|
{
|
|
postEntityExists.Title = post.Title;
|
|
postEntityExists.Description = post.Description;
|
|
postEntityExists.Categories = post.Categories;
|
|
_blogRepository.UpdatePost(postEntityExists);
|
|
}
|
|
|
|
await _blogRepository.SaveChangesAsync();
|
|
|
|
var postMetrics = _mapper.Map<PostMetricsDto>(postEntityExists);
|
|
postMetrics.PostExists = true;
|
|
return Ok(postMetrics);
|
|
}
|
|
|
|
[HttpPost("LikePost")]
|
|
public async Task<ActionResult<int>> LikePost(string blogUrl, string postSlug)
|
|
{
|
|
if (!await _blogRepository.PostExistsAsync(blogUrl, postSlug))
|
|
{
|
|
_logger.LogInformation($"Post with id {postSlug} wasn't found when fetching post likes and views.");
|
|
return NotFound();
|
|
}
|
|
|
|
var post = await _blogRepository.GetPostAsync(blogUrl, postSlug);
|
|
|
|
post.Likes++;
|
|
|
|
_blogRepository.UpdatePost(post);
|
|
|
|
await _blogRepository.SaveChangesAsync();
|
|
|
|
return Ok(post.Likes);
|
|
}
|
|
|
|
[HttpPost("DislikePost")]
|
|
public async Task<ActionResult<int>> DislikePost(string blogUrl, string postSlug)
|
|
{
|
|
if (!await _blogRepository.PostExistsAsync(blogUrl, postSlug))
|
|
{
|
|
_logger.LogInformation($"Post with id {postSlug} wasn't found when fetching post likes and views.");
|
|
return NotFound();
|
|
}
|
|
|
|
var post = await _blogRepository.GetPostAsync(blogUrl, postSlug);
|
|
|
|
post.Likes--;
|
|
|
|
_blogRepository.UpdatePost(post);
|
|
|
|
await _blogRepository.SaveChangesAsync();
|
|
|
|
return Ok(post.Likes);
|
|
}
|
|
|
|
[HttpPost("UpdatePostCommentsCount")]
|
|
public async Task<ActionResult> UpdatePostCommentsCount(string blogUrl, string postSlug, [FromBody] int commentsCount)
|
|
{
|
|
if (!await _blogRepository.PostExistsAsync(blogUrl, postSlug))
|
|
{
|
|
_logger.LogInformation($"Post with id {postSlug} wasn't found when fetching post likes and views.");
|
|
return NotFound();
|
|
}
|
|
|
|
var post = await _blogRepository.GetPostAsync(blogUrl, postSlug);
|
|
|
|
post.Comments = commentsCount;
|
|
|
|
_blogRepository.UpdatePost(post);
|
|
|
|
await _blogRepository.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
}
|
|
}
|