Skip to content

Commit

Permalink
Add vehicle and availability
Browse files Browse the repository at this point in the history
  • Loading branch information
BSanchidrian committed Nov 8, 2024
1 parent 06e52ff commit 190d1ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
15 changes: 10 additions & 5 deletions tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Google.Apis.Auth.OAuth2;
using System.Text.Json;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
Expand Down Expand Up @@ -35,12 +36,14 @@ private async Task InsertOffersRequestsAsync(List<HelpRequest> requests)
{
var headerRow = new List<object>
{
"Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People",
"Name", "Location", "ContactInfo", "People Needed",
"Created At", "ID", "Status", "Town","Description", "Help Types", "Availability", "Vehicle",
"Number of People", "Name", "Location", "ContactInfo", "People Needed",
};

var offerRequests = requests.Where(x => x.Type == "ofrece");

var rows = new List<IList<object>> { headerRow };
rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "ofrece").Select(request => new List<object>
rows.AddRange(offerRequests.OrderBy(x => x.Id).Select(request => new List<object>
{
request.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss"),
request.Id,
Expand All @@ -49,6 +52,8 @@ private async Task InsertOffersRequestsAsync(List<HelpRequest> requests)
request.Town?.Name ?? "",
request.Description ?? "",
string.Join(", ", request.HelpType ?? []),
string.Join(", ", request.Resources?.Availability ?? []),
request.Resources?.Vehicle ?? "NO",
request.NumberOfPeople ?? 0,
request.Name ?? "",
request.Location ?? "",
Expand Down Expand Up @@ -80,7 +85,7 @@ private async Task InsertNeedsRequestsAsync(List<HelpRequest> requests)
var headerRow = new List<object>
{
"Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People",
"Name", "Location", "ContactInfo", "People Needed",
"Name", "Location", "ContactInfo", "People Needed",
};

var rows = new List<IList<object>> { headerRow };
Expand Down
44 changes: 43 additions & 1 deletion tools/DanaCrawler/DanaCrawler/HelpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Supabase.Postgrest.Attributes;
using Supabase.Postgrest.Models;

Expand Down Expand Up @@ -63,9 +64,50 @@ internal sealed class HelpRequest : BaseModel
[Column("contact_info")]
public string ContactInfo { get; init; }

[Column("resources")]
[Newtonsoft.Json.JsonConverter(typeof(ResourcesJsonConverter))]
public Resources? Resources { get; init; }

[Column("created_at")]
public DateTime CreatedAt { get; init; }

[JsonProperty("towns")]
public Town Town { get; set; }
}

internal sealed class Resources
{
[JsonPropertyName("vehicle")]
public string? Vehicle { get; set; }
[JsonPropertyName("availability")]
public List<string> Availability { get; set; } = new();
[JsonPropertyName("radius")]
public string? Radius { get; set; }
[JsonPropertyName("experience")]
public string? Experience { get; set; } = string.Empty;
}

internal sealed class ResourcesJsonConverter : Newtonsoft.Json.JsonConverter<Resources>
{
public override Resources ReadJson(JsonReader reader, Type objectType, Resources existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
// If we get a string, parse it as JSON
string jsonString = (string)reader.Value;
return JsonConvert.DeserializeObject<Resources>(jsonString);
}
else if (reader.TokenType == JsonToken.StartObject)
{
// If we get a JSON object directly, deserialize it
return serializer.Deserialize<Resources>(reader);
}

return null;
}

public override void WriteJson(JsonWriter writer, Resources value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}

0 comments on commit 190d1ac

Please sign in to comment.