Skip to content

Instantly share code, notes, and snippets.

View Kashkovsky's full-sized avatar
😎

Denys Kashkovskyi Kashkovsky

😎
  • Grammarly, Inc.
  • Kyiv, Ukraine
View GitHub Profile
@Kashkovsky
Kashkovsky / NinjectControllerFactory.cs
Created June 8, 2016 13:13
Ninject Controller Factory
using Ninject;
using System;
using System.Web.Mvc;
namespace Factories
{
public class NinjectControllerFactory : DefaultControllerFactory
{
public IKernel Kernel { get; private set; }
@Kashkovsky
Kashkovsky / ApplicationDynamicDiscovery.cs
Last active June 8, 2016 15:20
Dynamic discovery of mvc application assemblies
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Compilation;
//Add to AssemblyInfo [assembly: PreApplicationStartMethod(typeof(ApplicationDynamicDiscovery), "Discover")]
namespace Web
{
public static class ApplicationDynamicDiscovery
@Kashkovsky
Kashkovsky / NamespaceConstraint.cs
Created June 8, 2016 12:38
Namespace constraint attribute
public class NamespaceConstraint : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var dataTokenNamespace = (string)controllerContext.RouteData.DataTokens.FirstOrDefault(dt => dt.Key == "Namespace").Value;
var actionNamespace = methodInfo?.DeclaringType?.FullName;
return dataTokenNamespace == actionNamespace;
}
}
@Kashkovsky
Kashkovsky / MockBaseRepository.cs
Created June 8, 2016 10:10
Mock for abstract base repository
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Moq;
namespace Test.Common.Mock.Repository
{
public abstract class MockBaseRepository<T, TEntity> : Mock<T>
where TEntity : class, IEntity
where T : BaseRepository<TEntity>
@Kashkovsky
Kashkovsky / PasswordGenerator.cs
Last active June 3, 2016 16:12
Simple password generator
using System;
using System.Text;
namespace Common.Utility
{
public class PasswordGenerator
{
public static string Create(int length = 8)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
@Kashkovsky
Kashkovsky / ImageController.cs
Created June 3, 2016 14:22
Image upload: Asp.Net
public class ImageController : Controller
{
private readonly IImageService _imageService;
public ImageController(IImageService imageService)
{
_imageService = imageService;
}
[HttpPost]
@Kashkovsky
Kashkovsky / upload-photo.cshtml
Last active June 3, 2016 14:25
Image Upload: AngularJs
<!-- ASP.NET part: "https://gist.github.com/Mr-Zoidberg/d9be453eb7d8cc7fc787f01dd381db17" -->
<!-- angular module and service are common so not included -->
<style>
.cropArea {
width: 200px;
height: 200px;
margin: 0 auto;
background-color: black;
}
.horizontal-center {
@Kashkovsky
Kashkovsky / dataToBlob.js
Created June 3, 2016 14:02
Convert data Uri to blob
function dataUrItoBlob(dataUri) {
var binary = atob(dataUri.split(',')[1]);
var mimeString = dataUri.split(',')[0].split(':')[1].split(';')[0];
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], { type: mimeString });
};
@Kashkovsky
Kashkovsky / validation.js
Created June 3, 2016 13:57
AngularJS phone number validation directive
(function () {
'use strict';
var phonePattern = /^\+{0,1}[\d ()-]+$/;
app.directive('phoneNumber', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$validators.phoneNumber = function (modelValue, viewValue) {
@Kashkovsky
Kashkovsky / authInterceptorService.js
Created June 3, 2016 13:55
AngularJS common interceptor
(function () {
'use strict';
app.factory('authInterceptorService', authInterceptorService);
authInterceptorService.$inject = ['$q', 'localStorageService', 'constantsService'];
function authInterceptorService ($q, localStorageService, constantsService) {
var authInterceptorServiceFactory = {};
function request (config) {