Skip to content

Instantly share code, notes, and snippets.

@rasxod
Created November 11, 2020 18:14
Show Gist options
  • Save rasxod/0de389ceb25f5653f95c684b06f1bfe8 to your computer and use it in GitHub Desktop.
Save rasxod/0de389ceb25f5653f95c684b06f1bfe8 to your computer and use it in GitHub Desktop.
using SQLite;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace sendsms
{
[Table("Friends")]
public class Friend
{
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class FriendAsyncRepository
{
SQLiteAsyncConnection database;
public FriendAsyncRepository(string databasePath)
{
database = new SQLiteAsyncConnection(databasePath);
}
public async Task CreateTable()
{
await database.CreateTableAsync<Friend>();
}
public async Task<List<Friend>> GetItemsAsync()
{
return await database.Table<Friend>().ToListAsync();
}
public async Task<Friend> GetItemAsync(int id)
{
return await database.GetAsync<Friend>(id);
}
public async Task<int> DeleteItemAsync(Friend item)
{
return await database.DeleteAsync(item);
}
public async Task<int> SaveItemAsync(Friend item)
{
if (item.Id != 0)
{
await database.UpdateAsync(item);
return item.Id;
}
else
{
return await database.InsertAsync(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment