Skip to content

Commit

Permalink
Merge pull request #14 from seatsio/bver/add-channels-endpoints
Browse files Browse the repository at this point in the history
Add channels support
  • Loading branch information
bverbeken authored Jun 10, 2020
2 parents c5d4ca4 + c61d7a9 commit 2bda4cb
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 11 deletions.
34 changes: 34 additions & 0 deletions SeatsioDotNet.Test/Events/AssignObjectsToChannelTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using SeatsioDotNet.Events;
using Xunit;


namespace SeatsioDotNet.Test.Events
{
public class AssignObjectsToChannelTest : SeatsioClientTest
{

[Fact]
public void assignObjectsToChannel() {
var chartKey1 = CreateTestChart();
var event1 = Client.Events.Create(chartKey1);
var channels = new Dictionary<string, Channel>()
{
{ "channelKey1", new Channel("channel 1", "#FFFF00", 1) },
{ "channelKey2", new Channel("channel 2", "#00FFFF", 2) }
};
Client.Events.UpdateChannels(event1.Key, channels);

Client.Events.AssignObjectsToChannel(event1.Key, new
{
channelKey1 = new [] {"A-1", "A-2"},
channelKey2 = new [] {"A-3"},
});

var retrievedChannels = Client.Events.Retrieve(event1.Key).Channels;
Assert.Equal(new [] {"A-1", "A-2"}, retrievedChannels[0].Objects);
Assert.Equal(new [] {"A-3"}, retrievedChannels[1].Objects);
}
}
}
45 changes: 45 additions & 0 deletions SeatsioDotNet.Test/Events/UpdateChannelsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using SeatsioDotNet.Events;
using Xunit;


namespace SeatsioDotNet.Test.Events
{
public class UpdateChannelsTest : SeatsioClientTest
{

[Fact]
public void UpdateChannels() {
var chartKey1 = CreateTestChart();
var event1 = Client.Events.Create(chartKey1);

var channels = new Dictionary<string, Channel>()
{
{ "channelKey1", new Channel("channel 1", "#FFFF00", 1) },
{ "channelKey2", new Channel("channel 2", "#00FFFF", 2) }
};

Client.Events.UpdateChannels(event1.Key, channels);

var retrievedEvent = Client.Events.Retrieve(event1.Key);
Assert.Equal(2, retrievedEvent.Channels.Count);

var channel1 = retrievedEvent.Channels[0];
Assert.Equal("channelKey1", channel1.Key);
Assert.Equal("channel 1", channel1.Name);
Assert.Equal("#FFFF00", channel1.Color);
Assert.Equal(1, channel1.Index);
Assert.Empty(channel1.Objects);


var channel2 = retrievedEvent.Channels[1];
Assert.Equal("channelKey2", channel2.Key);
Assert.Equal("channel 2", channel2.Name);
Assert.Equal("#00FFFF", channel2.Color);
Assert.Equal(2, channel2.Index);
Assert.Empty(channel1.Objects);
}

}
}
35 changes: 35 additions & 0 deletions SeatsioDotNet/Events/Channel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using RestSharp.Deserializers;

namespace SeatsioDotNet.Events
{
public class Channel
{
public string Key { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public int Index { get; set; }
public IEnumerable<string> Objects { get; set; }

public Channel() {}

public Channel(string name, string color, int index)
{
Name = name;
Color = color;
Index = index;
}

public object AsJsonObject()
{
return new
{
name = Name,
index = Index,
color = Color
};
}

}
}
4 changes: 3 additions & 1 deletion SeatsioDotNet/Events/Event.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using RestSharp.Deserializers;

namespace SeatsioDotNet.Events
{
Expand All @@ -14,5 +15,6 @@ public class Event
public ForSaleConfig ForSaleConfig { get; set; }
public DateTimeOffset? CreatedOn { get; set; }
public DateTimeOffset? UpdatedOn { get; set; }
public List<Channel> Channels { get; set; }
}
}
}
57 changes: 47 additions & 10 deletions SeatsioDotNet/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private Event Create(string chartKey, string eventKey, bool? bookWholeTables, Di
var restRequest = new RestRequest("/events", Method.POST).AddJsonBody(requestBody);
return AssertOk(_restClient.Execute<Event>(restRequest));
}

public Event[] Create(string chartKey, EventCreationParams[] eventCreationParams)
{
Dictionary<string, object> requestBody = new Dictionary<string, object>();
Expand All @@ -72,18 +72,18 @@ public Event[] Create(string chartKey, EventCreationParams[] eventCreationParams
{
e.Add("eventKey", param.Key);
}
if (param.BookWholeTables != null)
if (param.BookWholeTables != null)
{
e.Add("bookWholeTables", param.BookWholeTables);
}
if (param.TableBookingModes != null)
if (param.TableBookingModes != null)
{
e.Add("tableBookingModes", param.TableBookingModes);
}
}
events.Add(e);
}
requestBody.Add("events", events.ToArray());
var restRequest = new RestRequest("/events/actions/create-multiple", Method.POST).AddJsonBody(requestBody);
requestBody.Add("events", events.ToArray());
var restRequest = new RestRequest("/events/actions/create-multiple", Method.POST).AddJsonBody(requestBody);
return AssertOk(_restClient.Execute<MultipleEvents>(restRequest)).events.ToArray();
}

Expand Down Expand Up @@ -270,7 +270,7 @@ private Dictionary<string, object> ChangeObjectStatusRequest(IEnumerable<string>
request.Add("events", events);
return request;
}

private Dictionary<string, object> ChangeObjectStatusRequest(IEnumerable<ObjectProperties> objects, string status, string holdToken, string orderId, bool? keepExtraData)
{
var requestBody = new Dictionary<string, object>()
Expand Down Expand Up @@ -304,7 +304,7 @@ public BestAvailableResult ChangeObjectStatus(string eventKey, BestAvailable bes
{"status", status},
{"bestAvailable", bestAvailable.AsDictionary()}
};

if (holdToken != null)
{
requestBody.Add("holdToken", holdToken);
Expand Down Expand Up @@ -385,6 +385,43 @@ private Dictionary<string, object> ForSaleRequest(IEnumerable<string> objects, I
return request;
}

public void UpdateChannels(string eventKey, Dictionary<string, Channel> channels)
{
var requestBody = UpdateChannelsRequest(channels);
var restRequest = new RestRequest("/events/{key}/channels/update", Method.POST)
.AddUrlSegment("key", eventKey)
.AddJsonBody(requestBody);
AssertOk(_restClient.Execute<object>(restRequest));
}

private Dictionary<string, object> UpdateChannelsRequest(Dictionary<string, Channel> channels)
{
var channelsJson = new Dictionary<string, object>();
foreach(KeyValuePair<string, Channel> entry in channels)
{
channelsJson.Add(entry.Key, entry.Value.AsJsonObject());
}
var request = new Dictionary<string, object>();
request.Add("channels", channelsJson);
return request;
}

public void AssignObjectsToChannel(string eventKey, object channelsConfig)
{
var requestBody = AssignObjectsToChannelsRequest(channelsConfig);
var restRequest = new RestRequest("/events/{key}/channels/assign-objects", Method.POST)
.AddUrlSegment("key", eventKey)
.AddJsonBody(requestBody);
AssertOk(_restClient.Execute<object>(restRequest));
}

private Dictionary<string, object> AssignObjectsToChannelsRequest(object channelsConfig)
{
var request = new Dictionary<string, object>();
request.Add("channelConfig", channelsConfig);
return request;
}

public IEnumerable<Event> ListAll()
{
return List().All();
Expand Down Expand Up @@ -443,10 +480,10 @@ public Lister<StatusChange> StatusChangesForObject(string eventKey, string objec
request => (RestRequest) request.AddUrlSegment("key", eventKey).AddUrlSegment("objectId", objectLabel)
));
}

private class ChangeObjectStatusInBatchResult
{
public List<ChangeObjectStatusResult> Results { get; set; }
}
}
}
}

0 comments on commit 2bda4cb

Please sign in to comment.