Skip to content

Controllers

Orbis Alonzo Gutierrez edited this page Sep 21, 2023 · 4 revisions

Controllers

This Controllers are override from Controller class, this controllers contains the basic methods of crud operations.

ApiCoreController

This controller is used with BaseRepository class please see the definition in: FoundationKit\Core\Controllers\ApiCoreController.cs

methods

complete this copilot template with the following methods:

  • GetByIdAsync : Get by id
  • AddAsync: Add new entity
  • DeleteAsync: Apply Soft remove to entity
  • GetAllPaginatedAsync: Get list paginated
  • UpdateAsync: Update entity

Example:

public class PeopleController : ApiCoreController<Person,IPersonService>
{
    public PeopleController(IPersonService service) : base(service)
    {
    }
}

ApiMapController

This controller is used with MapRepository class please see the definition in: FoundationKit\Core\Controllers\ApiMapController.cs

  • GetById : Get by id
  • Create: Add new entity
  • Delete: Apply Soft remove to entity
  • GetAll: Get list paginated
  • Update: Update entity

Example:

public class PeopleController : ApiMapController<IPersonMapService, PersonDto, PersonInput, PersonEdit>
{
    public PeopleController(IPersonMapService service) : base(service)
    {
    }
}

MvcCoreController

This controller is used with BaseRepository class please see the definition in: FoundationKit\Core\Controllers\ApiMapController.cs , is used for only for mvc projects.

Note: For endpoints work you need create the pages with same name that this methods

  • Update : Get entity by id [get]
  • Update: Update entity [post]
  • Create: Add new entity [get]
  • Create: Add new entity [post]
  • Delete: Apply Soft remove to entity
  • Index: Get list paginated
  • GetUserId: Get user id from claim by default usage the NameIdentifier and you can specified you claimType.
  • ShowAlert: Show Sweetalert2 alerts please see the example:FoundationKit.Web.Example\Controllers\HomeController.cs.
    • Note: You need add SweetAlert2 to project and usage the @Html.RenderAlerts(TempData); where you usage this. example: FoundationKit.Web.Example\Views\Home\Index.cshtml

this controller permit override the next variables for change messages defined:

  • CreateSuccess
  • CreateError
  • UpdateSuccess
  • UpdateError

Example:

public class PersonController : MvcCoreController<Person, IPersonService>
{
    public PersonController(IPersonService service) : base(service)
    {
    }
}

MvcCoreController with SweetAlert2 example

Example:

public class HomeController : MvcCoreController<Person, IPersonService>
{
    public HomeController(IPersonService service) : base(service)
    {
    }

    /// <summary>
    /// Test notifications
    /// </summary>
    /// <returns></returns>
    public override Task<IActionResult> Index([FromQuery] Paginate paginate, CancellationToken cancellationToken = default)
    {
        string config = $"{{" +
            $"html:'Hello from config'," +
            $"showCancelButton:true" +
            $"}}";

        //with config
        ShowAlert("Hello", FoundationKit.Domain.Enums.MvcCoreNotification.Error, "",config);
        //without config
        ShowAlert("Hello", FoundationKit.Domain.Enums.MvcCoreNotification.Success);
        return base.Index(paginate, cancellationToken);
    }
}
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>


@Html.RenderAlerts(TempData);

Clone this wiki locally