Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from wizicer/master
Browse files Browse the repository at this point in the history
add List/Create/Update command
  • Loading branch information
PRCV1 authored Oct 24, 2022
2 parents 66fdba2 + d2dab05 commit cb0b072
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
27 changes: 27 additions & 0 deletions pocketbase-csharp-sdk/Models/ItemBaseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using pocketbase_csharp_sdk.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace pocketbase_csharp_sdk.Models
{
public class ItemBaseModel
{
[JsonPropertyName("@collectionId")]
public string? CollectionId { get; set; }

[JsonPropertyName("@collectionName")]
public string? CollectionName { get; set; }

[JsonPropertyName("@expand")]
public JsonObject? Expand { get; set; }

public string? Id { get; set; }

[JsonConverter(typeof(DateTimeConverter))]
public DateTime? Created { get; set; }

[JsonConverter(typeof(DateTimeConverter))]
public DateTime? Updated { get; set; }
}

}
11 changes: 11 additions & 0 deletions pocketbase-csharp-sdk/Models/PagedCollectionModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace pocketbase_csharp_sdk.Models
{
public class PagedCollectionModel<T>
{
public int Page { get; set; }
public int PerPage { get; set; }
public int TotalItems { get; set; }
public T[]? Items { get; set; }
}

}
99 changes: 95 additions & 4 deletions pocketbase-csharp-sdk/Services/BaseService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using pocketbase_csharp_sdk.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -11,17 +12,107 @@ public abstract class BaseService<T> where T : class
protected abstract string BasePath { get; }

private readonly PocketBase client;
private readonly string[] itemProperties;

public BaseService(PocketBase client)
{
this.client = client;
this.itemProperties = this.GetPropertyNames().ToArray();
}

protected async Task<IEnumerable<T>> GetFullListAsync(int batch = 100, string? filter = null, string? sort = null, IDictionary<string, object>? query = null, IDictionary<string, string>? headers = null)
public async Task<PagedCollectionModel<T>> ListAsync(
int? page = null,
int? perPage = null,
string? sort = null,
string? filter = null,
string? expand = null,
IDictionary<string, string>? headers = null)
{
var response = await client.SendAsync<IEnumerable<T>>(BasePath, HttpMethod.Get, headers: headers, query: query);
return default;
var query = new Dictionary<string, object?>()
{
{ "filter", filter },
{ "page", page },
{ "perPage", perPage },
{ "sort", sort },
{ "expand", expand },
};
var url = this.BasePath + "/records";
var pagedCollection = await client.SendAsync<PagedCollectionModel<T>>(
url,
HttpMethod.Get,
headers: headers,
query: query);
if (pagedCollection is null) throw new ClientException(url);

return pagedCollection;
}

public async Task<T> CreateAsync(
T item,
string? expand = null,
IDictionary<string, string>? headers = null)
{
var query = new Dictionary<string, object?>()
{
{ "expand", expand },
};
var body = ConstructBody(item);
var url = this.BasePath + "/records";
var ret = await client.SendAsync<T>(
url,
HttpMethod.Post,
body: body,
headers: headers,
query: query);
if (ret is null) throw new ClientException(url);

return ret;
}

public async Task<T> UpdateAsync(
string id,
T item,
string? expand = null,
IDictionary<string, string>? headers = null)
{
var query = new Dictionary<string, object?>()
{
{ "expand", expand },
};
var body = ConstructBody(item);
var url = this.BasePath + "/records/" + id;
var pagedCollection = await client.SendAsync<T>(
url,
HttpMethod.Patch,
body: body,
headers: headers,
query: query);
if (pagedCollection is null) throw new ClientException(url);

return pagedCollection;
}

private IEnumerable<string> GetPropertyNames()
=> from prop in typeof(ItemBaseModel).GetProperties()
select prop.Name;

private Dictionary<string, object> ConstructBody(T item)
{
var body = new Dictionary<string, object>();

foreach (var prop in typeof(T).GetProperties())
{
if (this.itemProperties.Contains(prop.Name)) continue;
var propValue = prop.GetValue(item, null);
if (propValue is not null) body.Add(toCamelCase(prop.Name), propValue);
}

string toCamelCase(string str)
{
return char.ToLowerInvariant(str[0]) + str.Substring(1);
}

return body;
}
}
}

0 comments on commit cb0b072

Please sign in to comment.