Skip to content

Instantly share code, notes, and snippets.

View StefanRiedmann's full-sized avatar

Stefan StefanRiedmann

View GitHub Profile
@StefanRiedmann
StefanRiedmann / microsoft-login.ts
Created October 6, 2020 12:16
To generate a URL for making an OAuth login with Microsoft
/**
* Generate the redirect URL for a user to login with a Microsoft account.
* - The clientId is the id of the Active Directory application.
* - The redirect URL must be configured in that application (https!)
* - If an email is given, it will be preselected in the login box.
*/
getMsOauthUrl(clientId: string, redirect: string, state: string, email?: string): string
{
let url = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?` +
`client_id=${clientId}` +
@StefanRiedmann
StefanRiedmann / BillingApiController.cs
Last active October 21, 2020 14:52
BillingApiController.cs
[ApiController]
[Route("api/[controller]")]
public class BillingController : ControllerBase
{
private readonly IBillingApiService service;
public BillingController(IBillingApiService service)
{
this.service = service;
}
@StefanRiedmann
StefanRiedmann / IBillingApiService.cs
Last active October 2, 2020 21:38
IBillingApiService.cs
public interface IBillingApiService
{
/// <summary>
/// The resolve endpoint enables the publisher to exchange the marketplace purchase identification token
/// to a persistent purchased SaaS subscription ID and its details.
/// </summary>
Task<MicrosoftPurchase> ResolvePurchase(string marketplaceToken);
/// <summary>
/// Retrieves a list of all purchased SaaS subscriptions for all offers published by the publisher in marketplace.
@StefanRiedmann
StefanRiedmann / ReadGardenInfos.cs
Created May 7, 2020 01:44
Read and print Garden summaries for a list of years.
public static void ReadGardenInfos(GardenContext db, params int[] years)
{
var gardenInfos = db.GardenInfos.AsNoTracking()
.Include(gi => gi.Garden)
.Where(gi => years.Contains(gi.Year))
.OrderBy(gi => gi.Garden.Name).ThenBy(gi => gi.Year)
.ToList();
gardenInfos.ForEach(info => Console.WriteLine($"Crops in '{info.Garden.Name}' ({info.Year}): {info.AllGardenCrops}"));
}
@StefanRiedmann
StefanRiedmann / Program.cs
Created May 7, 2020 01:38
Entity Framework Core preparing schema and views
class Program
{
static void Main(string[] args)
{
using (var db = new GardenContext())
{
db.Database.Migrate();
CreateViews(db.Database);
}
}
@StefanRiedmann
StefanRiedmann / GardenContext.cs
Created May 7, 2020 01:28
GardenContext with view
public class GardenContext : DbContext
{
public DbSet<Garden> Gardens { get; set; }
public DbSet<Bed> Beds { get; set; }
public DbSet<Crop> Crops { get; set; }
public DbSet<CropAssignment> CropAssignments { get; set; }
public DbSet<GardenInfo> GardenInfos { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<GardenInfo>(d =>
@StefanRiedmann
StefanRiedmann / GardenInfo.sql
Created May 6, 2020 21:05
SQL Query to get crop information per garden and year
select distinct
Gardens.GardenId
, Gardens.Name
, ca.Year
,substring(
(
select ' | '+Crops.Name as [text()]
from Crops
join CropAssignments on Crops.CropId = CropAssignments.CropId
join Beds on Beds.BedId = CropAssignments.BedId
@StefanRiedmann
StefanRiedmann / Program.cs
Created April 23, 2020 16:56
Garden program that can generate sample data and query it from the db
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EFCoreBasic
{
class Program
{
static void Main(string[] args)
{
@StefanRiedmann
StefanRiedmann / Model.cs
Created April 23, 2020 16:52
DataModels and DbContext for the garden database
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace EFCoreBasic
{
public class GardenContext : DbContext
{
public DbSet<Garden> Gardens { get; set; }
public DbSet<Bed> Beds { get; set; }
@StefanRiedmann
StefanRiedmann / PersonContext.cs
Created March 14, 2018 20:30
DbContext for EntityFramework Core
using Microsoft.EntityFrameworkCore;
namespace TestProject
{
public class PersonContext : DbContext
{
DbSet<Person> Persons { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{