Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Created February 3, 2020 14:39
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/7afbaac56c26222bbedcf8b0dd2877a7 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/7afbaac56c26222bbedcf8b0dd2877a7 to your computer and use it in GitHub Desktop.
Repository implementation using Sqlite
using LocPoc.Contracts;
using System;
using System.Collections.Generic;
namespace LocPoc.Repository.Sqlite
{
public class LocationsRepository : ILocationsRepository
{
private readonly SqliteContext _context;
public LocationsRepository(SqliteContext context)
{
_context = context;
}
public Location Add(Location location)
{
location.Id = Guid.NewGuid().ToString();
_context.Locations.Add(location);
_context.SaveChanges();
return location;
}
public void Delete(string id)
{
var loc = _context.Locations.Find(id);
if (loc != null)
{
_context.Locations.Remove(loc);
}
_context.SaveChanges();
}
public Location Get(string id)
{
var loc = _context.Locations.Find(id);
return loc;
}
public IEnumerable<Location> GetAll()
{
return _context.Locations;
}
public Location Update(Location location)
{
var loc = _context.Locations.Find(location.Id);
if (loc != null)
{
loc.Name = location.Name;
loc.Description = location.Description;
loc.Latitude = location.Latitude;
loc.Longitude = location.Longitude;
}
_context.SaveChanges();
return loc;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment