Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active February 10, 2020 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsBergqvist/f605a7d3f077e970c939b5f5aa6afd5a to your computer and use it in GitHub Desktop.
Save LarsBergqvist/f605a7d3f077e970c939b5f5aa6afd5a to your computer and use it in GitHub Desktop.
An async implementation of the locations repository with Sqlite
using LocPoc.Contracts;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LocPoc.Repository.Sqlite
{
public class LocationsRepositoryAsync : ILocationsRepositoryAsync
{
private readonly SqliteContext _context;
public LocationsRepositoryAsync(SqliteContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public async Task<IEnumerable<Location>> GetAllAsync()
{
return await _context.Locations.ToListAsync();
}
public async Task<Location> GetAsync(string id)
{
var location = await _context.Locations.FindAsync(id);
return location;
}
public async Task<Location> CreateAsync(Location location)
{
location.Id = Guid.NewGuid().ToString();
await _context.Locations.AddAsync(location);
await _context.SaveChangesAsync();
return location;
}
public async Task<Location> UpdateAsync(Location location)
{
var loc = await _context.Locations.FindAsync(location.Id);
if (loc != null)
{
loc.Name = location.Name;
loc.Description = location.Description;
loc.Latitude = location.Latitude;
loc.Longitude = location.Longitude;
}
await _context.SaveChangesAsync();
return loc;
}
public async Task DeleteAsync(string id)
{
var loc = await _context.Locations.FindAsync(id);
if (loc != null)
{
_context.Locations.Remove(loc);
}
await _context.SaveChangesAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment