Skip to content

Instantly share code, notes, and snippets.

Avatar

Lars Bergqvist LarsBergqvist

View GitHub Profile
@LarsBergqvist
LarsBergqvist / LocationsRepositoryAsync.cs
Last active Feb 10, 2020
An async implementation of the locations repository with Sqlite
View LocationsRepositoryAsync.cs
using LocPoc.Contracts;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LocPoc.Repository.Sqlite
{
public class LocationsRepositoryAsync : ILocationsRepositoryAsync
{
@LarsBergqvist
LarsBergqvist / ILocationsRepositoryAsync.cs
Created Feb 10, 2020
An interface for an async version of the locations repository
View ILocationsRepositoryAsync.cs
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LocPoc.Contracts
{
public interface ILocationsRepositoryAsync
{
Task<IEnumerable<Location>> GetAllAsync();
Task<Location> GetAsync(string id);
Task<Location> CreateAsync(Location location);
@LarsBergqvist
LarsBergqvist / LocationsController.cs
Last active Feb 10, 2020
LocationsController (simpified for readability)
View LocationsController.cs
namespace LocPoc.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class LocationsController : ControllerBase
{
ILocationsRepositoryAsync _locationsRepository;
public LocationsController(ILocationsRepositoryAsync locationsRepository)
{
_locationsRepository = locationsRepository ?? throw new ArgumentNullException(nameof(locationsRepository));
@LarsBergqvist
LarsBergqvist / SqliteContext.cs
Created Feb 3, 2020
DbContext implementation for Location entities with a Sqlite database
View SqliteContext.cs
using Microsoft.EntityFrameworkCore;
using LocPoc.Contracts;
namespace LocPoc.Repository.Sqlite
{
public class SqliteContext: DbContext
{
public SqliteContext(DbContextOptions<SqliteContext> options) : base(options)
{
}
@LarsBergqvist
LarsBergqvist / LocationsRepository.cs
Created Feb 3, 2020
Repository implementation using Sqlite
View LocationsRepository.cs
using LocPoc.Contracts;
using System;
using System.Collections.Generic;
namespace LocPoc.Repository.Sqlite
{
public class LocationsRepository : ILocationsRepository
{
private readonly SqliteContext _context;
View ILocationsRepository.cs
using System.Collections.Generic;
namespace LocPoc.Contracts
{
public interface ILocationsRepository
{
IEnumerable<Location> GetAll();
Location Get(string id);
Location Create(Location location);
Location Update(Location location);
@LarsBergqvist
LarsBergqvist / app-config.json
Created Feb 3, 2020
Angular application configuration: use real data from the API and use Google Maps
View app-config.json
{
"apiUrl": "https://localhost",
"apiPort": "5001",
"useFakeData": false,
"useMap": true,
"googleAPIKey": "ABzaDuAQbFxSalDaWfIAB7DEZAh730AFGGGlpg"
}
@LarsBergqvist
LarsBergqvist / googlemaps.service.ts
Created Feb 2, 2020
Service that uses load-google-maps-api
View googlemaps.service.ts
import { Injectable } from '@angular/core';
import * as loadGoogleMapsApi from 'load-google-maps-api';
@Injectable()
class GoogleMapsService {
constructor() { }
load(googleAPIKey: string): any {
return loadGoogleMapsApi({ key: googleAPIKey });
}
}
@LarsBergqvist
LarsBergqvist / app.module.ts
Created Feb 2, 2020
Provide for HTTP_INTERCEPTORS
View app.module.ts
{
provide: HTTP_INTERCEPTORS,
useClass: HttpInterceptorService,
multi: true,
deps: [MessageBrokerService, LoggingService]
},
View http-interceptor.service.ts
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ErrorOccurredMessage } from '../messages/error-occurred.message';
import { MessageBrokerService } from './message-broker.service';
import { LoggingService } from './logging-service';
@Injectable({
providedIn: 'root'
You can’t perform that action at this time.