Skip to content

Commit

Permalink
Fix range issue and primary key not being retrieved via properies
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Schultz committed Nov 16, 2020
1 parent 64a2612 commit 9d58bcc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
12 changes: 10 additions & 2 deletions Postgrest/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Task Delete()
public Task Delete(T model)
{
method = HttpMethod.Delete;
Filter(model.PrimaryKeyColumn, Operator.Equals, Helpers.GetPropertyValue<string>(model, model.PrimaryKeyColumn));
Filter(model.PrimaryKeyColumn, Operator.Equals, model.PrimaryKeyValue.ToString());
var request = Send(method, null, null);
Clear();
return request;
Expand Down Expand Up @@ -236,6 +236,14 @@ public Task<T> Single()
var result = await request;
tsc.SetResult(result.Models.FirstOrDefault());
}
catch (RequestException e)
{
// No rows returned
if (e.Response.StatusCode == System.Net.HttpStatusCode.NotAcceptable)
tsc.SetResult(null);
else
tsc.SetException(e);
}
catch (Exception e)
{
tsc.SetException(e);
Expand Down Expand Up @@ -305,7 +313,7 @@ public string GenerateUrl()
return builder.Uri.ToString();
}

public Dictionary<string, string> PrepareRequestData(object data) => JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(data));
public Dictionary<string, string> PrepareRequestData(object data) => JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(data, Client.Instance.SerializerSettings));

public Dictionary<string, string> PrepareRequestHeaders(Dictionary<string, string> headers = null)
{
Expand Down
4 changes: 3 additions & 1 deletion Postgrest/Converters/RangeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Postgrest.Extensions;

namespace Postgrest.Converters
{
Expand All @@ -19,7 +20,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
Range val = (Range)value;
writer.WriteValue(val.ToPostgresString());
}

public static Range ParseIntRange(string value)
Expand Down
1 change: 1 addition & 0 deletions Postgrest/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class Helpers
public static T GetPropertyValue<T>(object obj, string propName) => (T)obj.GetType().GetProperty(propName).GetValue(obj, null);

public static T GetCustomAttribute<T>(object obj) where T : Attribute => (T)Attribute.GetCustomAttribute(obj.GetType(), typeof(T));
public static T GetCustomAttribute<T>(Type type) where T : Attribute => (T)Attribute.GetCustomAttribute(type, typeof(T));

private static readonly HttpClient client = new HttpClient();

Expand Down
31 changes: 28 additions & 3 deletions Postgrest/Models/BaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,39 @@ public abstract class BaseModel
public virtual Task<ModeledResponse<T>> Update<T>() where T : BaseModel, new() => Client.Instance.Builder<T>().Update((T)this);
public virtual Task Delete<T>() where T : BaseModel, new() => Client.Instance.Builder<T>().Delete((T)this);

[JsonIgnore]
public object PrimaryKeyValue
{
get
{
var props = this.GetType().GetProperties();
foreach (var prop in props)
{
var hasAttr = Attribute.GetCustomAttribute(prop, typeof(PrimaryKeyAttribute));
if (hasAttr is PrimaryKeyAttribute pka)
{
return prop.GetValue(this);
}
}

throw new Exception("Models must specify their Primary Key via the [PrimaryKey] Attribute");
}
}

[JsonIgnore]
public string PrimaryKeyColumn
{
get
{
var attrs = Helpers.GetCustomAttribute<PrimaryKeyAttribute>(this);
if (attrs != null)
return attrs.ColumnName;
var props = this.GetType().GetProperties();
foreach (var prop in props)
{
var hasAttr = Attribute.GetCustomAttribute(prop, typeof(PrimaryKeyAttribute));
if (hasAttr is PrimaryKeyAttribute pka)
{
return pka.ColumnName;
}
}

throw new Exception("Models must specify their Primary Key via the [PrimaryKey] Attribute");
}
Expand Down
17 changes: 17 additions & 0 deletions PostgrestExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,24 @@ static async Task<int> Main(string[] args)
}
}

var newUser = new User
{
Username = "Ash Ketchum",
AgeRange = new Range(20, 25),
Catchphrase = "Gotta catch them all",
Status = "ONLINE"
};

var exists = await client.Builder<User>().Filter("username", Postgrest.Constants.Operator.Equals, "Ash Ketchum").Single();

if (exists == null)
{
await client.Builder<User>().Insert(newUser);
}
else
{
await exists.Delete<User>();
}

return 0;
}
Expand Down

0 comments on commit 9d58bcc

Please sign in to comment.