Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.Web.Script.Serialization

<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class EditingWcfService

    Private _sessionKey As String = "MySessionKey"

    Public ReadOnly Property SessionDataSource As List(Of Customer)
        Get

            If HttpContext.Current.Session(_sessionKey) Is Nothing Then
                HttpContext.Current.Session(_sessionKey) = Enumerable.Range(1, 10000).[Select](Function(cu) New Customer With {
                    .CustomerID = cu,
                    .SomeDate = DateTime.Now.Date.AddDays(cu),
                    .Address = "Address " & cu,
                    .CompanyName = "CompanyName " & cu,
                    .ContactName = "ContactName " & cu,
                    .ContactTitle = "ContactTitle " & cu
                }).ToList()
            End If

            Return CType(HttpContext.Current.Session(_sessionKey), List(Of Customer))
        End Get
    End Property

    <WebGet>
    Public Function GetCustomers() As CustomersResult
        Return New CustomersResult With {
            .Data = SessionDataSource,
            .Count = SessionDataSource.Count()
        }
    End Function

    <WebGet>
    Public Sub UpdateCustomers(ByVal customersJSON As String)
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
        Dim updatedCustomers As List(Of Customer) = CType(serializer.Deserialize(customersJSON, GetType(List(Of Customer))), List(Of Customer))

        For Each updatedCustomer As Customer In updatedCustomers
            Dim dbCustomer As Customer = SessionDataSource.FirstOrDefault(Function(c) c.CustomerID = updatedCustomer.CustomerID)
            dbCustomer.CompanyName = updatedCustomer.CompanyName
            dbCustomer.ContactName = updatedCustomer.ContactName
            dbCustomer.ContactTitle = updatedCustomer.ContactTitle
            dbCustomer.Address = updatedCustomer.Address
        Next
    End Sub

    <WebGet>
    Public Function InsertCustomers(ByVal customersJSON As String) As CustomersResult
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
        Dim insertedCustomers As List(Of Customer) = CType(serializer.Deserialize(customersJSON, GetType(List(Of Customer))), List(Of Customer))

        For Each insertedCustomer As Customer In insertedCustomers
            Dim lastId = SessionDataSource.Max(Function(cu) cu.CustomerID)
            Dim newCustomer As Customer = New Customer() With {
                .CustomerID = lastId + 1,
                .CompanyName = insertedCustomer.CompanyName,
                .ContactName = insertedCustomer.ContactName,
                .ContactTitle = insertedCustomer.ContactTitle,
                .Address = insertedCustomer.Address
            }
            insertedCustomer.CustomerID = newCustomer.CustomerID
            SessionDataSource.Add(newCustomer)
        Next

        Return New CustomersResult With {
            .Data = insertedCustomers
        }
    End Function

    <WebGet>
    Public Function DeleteCustomers(ByVal customersJSON As String) As Object
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
        Dim customers As List(Of Customer) = CType(serializer.Deserialize(customersJSON, GetType(List(Of Customer))), List(Of Customer))

        For Each removedCustomer As Customer In customers
            Dim customer As Customer = SessionDataSource.Find(Function(c) c.CustomerID = removedCustomer.CustomerID)
            SessionDataSource.Remove(customer)
        Next

        Return customersJSON
    End Function

End Class


<DataContract>
Public Class Customer
    <DataMember>
    Public Property CustomerID As Integer
    <DataMember>
    Public Property SomeDate As DateTime
    <DataMember>
    Public Property CompanyName As String
    <DataMember>
    Public Property ContactName As String
    <DataMember>
    Public Property ContactTitle As String
    <DataMember>
    Public Property Address As String
    <DataMember>
    Public Property __type As String
End Class

Public Class CustomersResult
    Public Property Data As List(Of Customer)
    Public Property Count As Integer
End Class

Public Class CustomersEditResult
    Public Property models As List(Of Customer)
End Class

