Skip to content

Instantly share code, notes, and snippets.

View pedrovasconcellos's full-sized avatar

Pedro Vasconcellos pedrovasconcellos

View GitHub Profile
@pedrovasconcellos
pedrovasconcellos / Pagination.cs
Last active April 28, 2023 20:01
Pagination
public async Task<IActionResult> Example(int? page, int? rows)
{
IList<object> entities = await _service.Get();
if (page != null && page > 0 && rows != null && rows > 0)
{
entities = query.Skip((int)(rows * (page - 1))).Take((int)rows).ToList();
}
else
entities = query.ToList();
@pedrovasconcellos
pedrovasconcellos / DateVascAndMask.php
Created January 23, 2019 16:25
DateVasc and Mask
<?php
function DateVasc($data){
if(count(explode("/",$data)) > 1){
return str_replace('/','-',implode("-",array_reverse(explode("/",$data))));
}elseif(count(explode("-",$data)) > 1){
return str_replace('-','/',(implode("/",array_reverse(explode("-",$data)))));
}
}
echo '';
@pedrovasconcellos
pedrovasconcellos / LoggingMiddleware.cs
Created January 30, 2019 19:28
Logging Middleware ASP.NET Core
//Use => [ app.UseRequestResponseLogging() ] inside [Class: Startup; Method: Configure;].
using Microsoft.AspNetCore.Builder;
namespace Vasconcellos.WebAPI.Extensions
{
/// <summary>
/// RequestResponseLoggingMiddlewareExtensions
/// </summary>
/// <remarks>
@pedrovasconcellos
pedrovasconcellos / PreventingRedirectionAttacksForOtherDomains.cs
Last active January 31, 2019 20:11
Preventing redirection attacks for other domains (C #)
//Note: Before using these two functions read this
//https://docs.microsoft.com/pt-br/aspnet/mvc/overview/security/preventing-open-redirection-attacks
public static class PreventingRedirectionAttacksForOtherDomains
{
public static bool CheckURLValid(string url)
{
return Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp);
}
@pedrovasconcellos
pedrovasconcellos / CarWithPaginationController.cs
Last active March 27, 2019 12:21
Example: Car Controller with pagination in C# ASP.NET Core
using AutoMapper;
using Vasconcellos.WebAPI.Entity;
using Vasconcellos.WebAPI.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
@pedrovasconcellos
pedrovasconcellos / DropDatabaseWithCloseAllConnections.sql
Created September 4, 2019 04:15
SQL to close all connections and drop database
--SQL to close all connections and drop database
USE [master];
DECLARE @DatabaseName VARCHAR(MAX) = 'MyDatabaseName' --to change
DECLARE @kill VARCHAR(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(VARCHAR(5), session_id) + ';'
FROM sys.dm_exec_sessions
WHERE database_id = db_id(@DatabaseName)
EXEC(@kill);
@pedrovasconcellos
pedrovasconcellos / httpsredirect.php
Created September 25, 2019 03:49
HTTPS Redirect
<?php
if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off"){
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}
?>
@pedrovasconcellos
pedrovasconcellos / http-get.js
Last active November 1, 2019 18:02
Http GET in Ajax using pure Javascript
var schoolID = 77;
var classId = 3;
var url = 'https://udemy.com.br/Cursos?schoolID='+schoolID+'&classId='+classId;
var callBackParameter = {};
var callBackFunction = function(callBackParameter, ajax){
let objResponse = JSON.parse(ajax.responseText);
console.log('Response Object', objResponse);
};
@pedrovasconcellos
pedrovasconcellos / http-post.js
Last active December 26, 2020 01:32
Http POST in Ajax using pure Javascript
var url = 'https://udemy.com.br/OpenClass';
var obj = { schoolID: 77 , classId: 3};
var callBackParameter = {};
var callBackFunction = function(ajax, callBackParameter) {
let objResponse = JSON.parse(ajax.responseText);
console.log('Response Object', objResponse);
};
var callBackErrorParamater = {};
@pedrovasconcellos
pedrovasconcellos / RecursionExamples.cs
Last active February 21, 2020 03:41
Recursion Examples
void Main()
{
var x = 19;
var y = 7;
Console.WriteLine($"Factorial of x={x}, result={Factorial(x)}.\n");
Console.WriteLine($"Multiplication of x*y={x}*{y}={Multiplication(x, y)}.\n");
Console.WriteLine($"Binary of x={x}, result={DecimalToBinary(x)}.\n");
}
public long Factorial(long x)