Skip to content

Commit

Permalink
⛏ Implement Customer Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
furkandeveloper committed Aug 5, 2021
1 parent 666f8da commit 3df2041
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions samples/IronHook.Web/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using IronHook.Core.Abstractions;
using IronHook.Core.Primitives;
using IronHook.PostgreSql.Context;
using IronHook.Web.Context;
using IronHook.Web.Dtos;
using IronHook.Web.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace IronHook.Web.Controllers
{
/// <summary>
/// Customers Endpoint
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class CustomersController : ControllerBase
{
private readonly SampleDbContext sampleDbContext;
private readonly IHookService<IronHookPostgreSqlDbContext> hookService;

/// <summary>
/// Ctor
/// </summary>
/// <param name="sampleDbContext">
/// Sample Database Context
/// </param>
/// <param name="hookService">
/// Hook Service
/// </param>
public CustomersController(SampleDbContext sampleDbContext, IHookService<IronHookPostgreSqlDbContext> hookService)
{
this.sampleDbContext = sampleDbContext;
this.hookService = hookService;
}
/// <summary>
/// Customer Create
/// </summary>
/// <returns></returns>
[HttpPost(Name = "AddCustomer")]
[ProducesResponseType(typeof(HookResponse[]),200)]
public async Task<IActionResult> AddCustomerAsync([FromBody] CustomerRequestDto model)
{
var entity = await sampleDbContext.Customers.AddAsync(new Customer
{
Name = model.Name,
Surname = model.Surname,
Phone = model.Phone
});
await sampleDbContext.SaveChangesAsync();
var response = await hookService.RaiseHookAsync(EventNames.CUSTOMER_CREATED, "1", entity.Entity);
return Ok(response);
}

/// <summary>
/// Get All Customers
/// </summary>
/// <returns>
/// Array of Customers
/// </returns>
[HttpGet(Name = "GetAllCustomers")]
[ProducesResponseType(typeof(Customer[]), 200)]
public async Task<IActionResult> GetAllAsync()
=> Ok(await sampleDbContext.Customers.Skip(0).Take(10).ToListAsync());
}
}

0 comments on commit 3df2041

Please sign in to comment.