Skip to content

Instantly share code, notes, and snippets.

View hudo's full-sized avatar

Hrvoje Hudo hudo

View GitHub Profile
@hudo
hudo / FlattenList.cs
Created April 29, 2019 11:02
FlattenList
class Program
{
static void Main(string[] args)
{
// sample list to test
var list = new ArrayList
{
new ArrayList {
new ArrayList
{
public class ValidateModelAttribute : ActionFilterAttribute
{
public override async Task OnActionExecutionAsync(ActionExecutingContext context, Action
{
var result = new ViewResult();
if (!context.ModelState.IsValid)
{
// Usually POST action looks like: IActionResult Save(MyMode model)
// if validation failed we need to forward that model back to view
// Model is in ActionArgument[0] in this case, but you may tweak
[HttpPost, ValidateAnyForgeryToken]
public async Task<IActionResult> Save(ProductModel model)
{
if(!ModelState.IsValid)
return View(model);
// invoke some business logic magic that makes money
try
{
await _dbContext.SaveChanges();
internal class LinkDecorator : IRenderable
{
private readonly IRenderable _inner;
private readonly string _url;
public LinkDecorator(IRenderable inner, string url)
{
_inner = inner;
_url = url;
}
internal interface IRenderable
{
string Render();
}
var element = new UnorderedList(new ListItem(new Link(new Content(“page 2”))));
<ul class="pagination">
<li><a href="#" >&laquo;</a></li>
<li class="active"><a href="?page=1" >1</a></li>
<li><a href="?page=2" >2</a></li>
<li><a href="?page=3" >3</a></li>
<li class="disabled"><span>...</span></li>
<li><a href="?page=20" >20</a></li>
<li><a href="?page=2" >&raquo;</a></li>
</ul>
@hudo
hudo / HomeControllerTests.cs
Last active July 17, 2017 13:55
Unit tests
public void User_can_write_story_when_authenticated()
{
var controller = new HomeController();
controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, "username")
@hudo
hudo / HomeController.cs
Created July 16, 2017 22:29
Controller
public IActionResult Index()
{
var model = new HomePageViewModel
{
Title = "Welcome!",
DisplayMode = StoriesDisplayMode.FeaturedList,
CanWriteStory = User.Identity.IsAuthenticated
};
return View(model);
}
@hudo
hudo / app.cs
Last active March 15, 2017 13:03
Santa gifts
class Program
{
static void Main(string[] args)
{
var children = new List<Child> { new Child("Hudo", true), new Child("Genna", true), new Child("Calippio", false), new Child("Javier", false), new Child("Cristian", true)};
var presents = new List<Present> { new Present("Tannoy speakers", 2), new Present("Candy", 1)};
presents
.SelectMany(x => Enumerable.Range(1, x.Quantity).Select(c => new Present(x.Name, 1)))
.Zip(children.Where(x => x.WasGood), (present, child) => new Assignment(child.Name, present.Name))