Skip to content

Instantly share code, notes, and snippets.

View phillippelevidad's full-sized avatar

Phillippe Santana phillippelevidad

View GitHub Profile
@phillippelevidad
phillippelevidad / AwsCostExplorerHelper.cs
Created February 13, 2020 19:11
A quick-start class to pull sumarized costs from your AWS account.
namespace NameIt
{
public static class AwsCostExplorer
{
private const string metricType = "BlendedCost";
private static readonly RegionEndpoint regionEndpoint = RegionEndpoint.USEast1;
public static AwsCostStructure GetCostForAccount(string accessKey, string secretKey, Period period)
{
var client = new AmazonCostExplorerClient(
@phillippelevidad
phillippelevidad / async-form.js
Created July 2, 2019 14:13
Post HTML forms assynchronously by adding a simple data-async="true" attribute.
$('form[data-async=true]').on('submit', function (event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function (data, status) {
@phillippelevidad
phillippelevidad / roaddd-dtosandviewmodels-introcode.cs
Created June 6, 2019 20:23
ViewModels and DTOs: intro code.
// Repository
public class CustomerRepository : ICustomerRepository
{
public Customer Get(Guid id) { ... }
public List<Customer> GetAll() { ... }
public Add(Customer customer) { ... }
public Update(Customer customer) { ... }
public Delete(Customer customer) { ... }
}
public class Customer : Entity
{
public Guid Id { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public CustomerLevel Level { get; set; }
}
public class CustomerViewModel
{
[HttpPut("customers")]
public JsonResult UpdateCustomer(Customer customer)
{
if (ModelState.IsValid)
{
// Consumers are able to set Customer.Level as they can do with every other property added now and in the future.
customerRepository.Update(customer);
return Ok();
}
@phillippelevidad
phillippelevidad / roaddd-dtosandviewmodels-getcustomer-json-after.json
Created June 6, 2019 18:40
Customer JSON result, after new requirements.
{
"id": "d3edcff2-072c-41eb-b83e-6a0f795fa83a",
"name": "John Doe",
"birthDate": "1980-05-03",
"level": "Basic"
}
@phillippelevidad
phillippelevidad / roaddd-dtosandviewmodels-customerentity-after.cs
Last active June 6, 2019 19:54
Customer Entity, after new requirements.
public class Customer : Entity
{
[Id]
public Guid Id { get; set; }
[Required]
[MaxLength(50)
public string Name { get; set; }
public DateTime BirthDate { get; set; }
{
"id": "d3edcff2-072c-41eb-b83e-6a0f795fa83a",
"name": "John Doe",
"isAdvanced": false
}
[HttpGet("customers/{id}")]
public IActionResult GetCustomer(Guid id)
{
var model = customerRepository.Get(id);
return Ok(model);
}
public class Customer : Entity
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool IsAdvanced { get; set; }
}