key4.Comments 2.0.0

key4.Comments

NuGet that contains the Domain, Persistence, Commands and Queries to get, add, update, delete comments on any entity.

Initialization

StartUp

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddK4Comments(Configuration);
    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.EnsureDatabasesMigrated(env);
    ...
    app.UseK4Comments();
    ...
}

AutoMapperConfiguration

public static MapperConfiguration Configure()
    => new(cfg =>
    {
        cfg.AddMaps(...);

        cfg.AddK4Comments();
    });

Usage

GET / POST

Add a partial controller with GET / POST methods for an entity controller.

Example : MyEntityController.Comments.cs

using Host.Dtos.Common.Responses;
using key4.Comments.Application.Comments.Commands;
using key4.Comments.Application.Comments.Queries;
using key4.Comments.Host.Dtos.Comments.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Host.Controllers;

public partial class MyEntityController
{
    #region Public Methods

    [HttpGet("{id}/comments")]
    [ProducesResponseType(typeof(IEnumerable<CommentGetByEntity.CommentDto>), StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> GetCommentsAsync([FromRoute] Guid id)
        => Ok(await _mediator.Send(new CommentGetByEntity.Query { EntityId = id }));

    [Authorize]
    [HttpPost("{id}/comments")]
    [ProducesResponseType(typeof(CreatedDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> CreateCommentAsync(
        [FromRoute] Guid id,
        [FromBody] SyncCommentDto request)
    {
        var mappedRequest = _mapper.Map<CommentCreate.Command>(request);
        _mapper.Map(HttpContext.User, mappedRequest);
        mappedRequest.EntityId = id;
        return Ok(new CreatedDto(await _mediator.Send(mappedRequest)));
    }
    
    #endregion Public Methods
}

PUT / DELETE

Add a CommentController for all the PUTs / DELETEs for all the entities of the microservice.

using AutoMapper;
using key4.Comments.Application.Comments.Commands;
using key4.Comments.Host.Dtos.Comments.Requests;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Host.Controllers;

[Authorize]
[ApiController]
[ApiVersion("1.0")]
[Produces("application/json")]
[Route("operations/{operationId}/registration/comments")]
public class CommentController : ControllerBase
{
    #region Private Fields

    private readonly IMediator _mediator;
    private readonly IMapper _mapper;

    #endregion Private Fields

    #region Public Constructors

    public CommentController(IMediator mediator, IMapper mapper)
    {
        _mediator = mediator;
        _mapper = mapper;
    }

    #endregion Public Constructors

    #region Public Methods

    [HttpPut("{id}")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> UpdateCommentAsync(
        [FromRoute] Guid id,
        [FromBody] SyncCommentDto request)
    {
        var mappedRequest = _mapper.Map<CommentUpdate.Command>(request);
        _mapper.Map(HttpContext.User, mappedRequest);
        mappedRequest.Id = id;
        await _mediator.Send(mappedRequest);
        return NoContent();
    }

    [HttpDelete("{id}")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> DeleteCommentAsync([FromRoute] Guid id)
    {
        var request = new CommentDelete.Command { Id = id };
        _mapper.Map(HttpContext.User, request);
        await _mediator.Send(request);
        return NoContent();
    }

    #endregion Public Methods
}

No packages depend on key4.Comments.

.NET 8.0

Version Downloads Last updated
2.0.0 273 11/15/2023
2.0.0-a.93 10 11/15/2023
1.1.10 947 06/26/2023
1.1.9 71 06/08/2023
1.1.8 23 06/07/2023
1.1.8-a.86 10 06/07/2023
1.1.7 62 05/02/2023
1.1.7-a.83 9 05/02/2023
1.1.6 10 05/02/2023
1.1.6-a.80 10 05/02/2023
1.1.6-a.79 10 05/02/2023
1.1.6-a.72 10 05/02/2023
1.1.6-a.71 10 05/01/2023
1.1.6-a.70 10 05/01/2023
1.1.6-a.69 10 05/01/2023
1.1.6-a.64 10 05/01/2023
1.1.6-a.63 10 05/01/2023
1.1.6-a.62 10 05/01/2023
1.1.6-a.61 12 05/01/2023
1.1.6-a.52 10 05/01/2023
1.1.5 104 03/01/2023
1.1.4 68 02/21/2023
1.1.3 12 02/20/2023
1.1.2 11 02/20/2023
1.1.1 12 02/20/2023
1.1.0-a.42 68 12/12/2022
1.1.0-a.41 10 12/12/2022
1.1.0-a.38 81 12/08/2022
1.0.0-a.35 10 12/08/2022
1.0.0-a.33 10 12/08/2022
1.0.0-a.30 10 12/08/2022
1.0.0-a.26 10 12/08/2022
1.0.0-a.25 10 12/08/2022
1.0.0-a.20 11 12/08/2022
1.0.0-a.8 11 12/08/2022
1.0.0-a.7 12 12/08/2022
1.0.0-a.6 13 12/08/2022
1.0.0-a.5 11 12/08/2022
1.0.0-a.4 12 12/08/2022