Advertisement
Guest User

Untitled

a guest
May 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using DbInterface;
  4. using Model;
  5. using LiteDB;
  6.  
  7. namespace DbImplementation
  8. {
  9.     public class MapRepository : IMapRepository
  10.     {
  11.         private string _dbConnection = Connection.DB_CONNECTION;
  12.  
  13.         public void Create(Map map)
  14.         {
  15.             using (var db = new LiteDatabase(_dbConnection))
  16.             {
  17.                 var maps = db.GetCollection<Map>("maps");
  18.                 maps.Insert(map);
  19.             }
  20.         }
  21.  
  22.         public void Delete(int id)
  23.         {
  24.             using (var db = new LiteDatabase(_dbConnection))
  25.             {
  26.                 var maps = db.GetCollection<Map>("maps");
  27.                 maps.Delete(Map => Map.Id == id);
  28.             }
  29.         }
  30.  
  31.         public Map Get(int id)
  32.         {
  33.             using (var db = new LiteDatabase(_dbConnection))
  34.             {
  35.                 var maps = db.GetCollection<Map>("maps");
  36.                 return maps.FindById(id);
  37.             }
  38.         }
  39.  
  40.         public IEnumerable<Map> GetAll()
  41.         {
  42.             using (var db = new LiteDatabase(_dbConnection))
  43.             {
  44.                 return (IEnumerable<Map>)db.GetCollection<Map>("maps").FindAll();
  45.             }
  46.         }
  47.  
  48.         public void Update(int id, Map map)
  49.         {
  50.             using (var db = new LiteDatabase(_dbConnection))
  51.             {
  52.                 var maps = db.GetCollection<Map>("maps");
  53.                 var theMap = maps.FindById(id);
  54.                 theMap.positions = map.positions;
  55.                 theMap.SecretKey = map.SecretKey;
  56.                 maps.Update(map);
  57.             }
  58.         }
  59.     }
  60.  
  61.     public class Connection
  62.     {
  63.         public static string DB_CONNECTION = @"C:/tmp/test.db";
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement