DatabaseManager.cs 3.88 KB
using Contentful.Core;
using Contentful.Core.Configuration;
using Contentful.Core.Models;
using Contentful.Core.Search;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using UnityEngine;
using WebKit;
using Sirenix.OdinInspector;

[System.Serializable]
public class User
{
    [JsonProperty("sys")]
    public SystemProperties sys { get; set; }

    public string firstName;
    public string lastName;
    public string email;
}

[System.Serializable]
public class Race
{
    [JsonProperty("sys")]
    public SystemProperties sys { get; set; }

    public string time;
    public string fuckinIdk;
}


public class DatabaseManager : MonoBehaviour
{
    [FoldoutGroup("Contentful Initializer")]
    [SerializeField]
    private string _spaceId, _deliveryToken, _managementToken;

    private ContentfulClient _client;
    private ContentfulManagementClient _mClient;

    void Start()
    {
        ContentfulOptions options = new ContentfulOptions()
        {
            ManagementApiKey = _managementToken,
            DeliveryApiKey = _deliveryToken,
            SpaceId = _spaceId
        };

        _client = new ContentfulClient(new HttpClient(), options);
        _mClient = new ContentfulManagementClient(new HttpClient(), options);
    }




    #region GetSingle

    [FoldoutGroup("Get Single User")]
    [Button("Get Just Dave")]
    public void FetchUser()
    {
        StartCoroutine(GetSpecificUserCoroutine("dave@globacore.com"));
    }

    private IEnumerator GetSpecificUserCoroutine(string email)
    {
        var query = new QueryBuilder<User>().ContentTypeIs("user").FieldEquals(userLambda => userLambda.email, email);
        var fetchUserRequest = _client.GetEntries(query);

        while (!fetchUserRequest.IsCompleted) yield return null;

        fetchedUser = fetchUserRequest.Result.First();
    }

    [FoldoutGroup("Get Single User")]
    [SerializeField]
    private User fetchedUser;

    #endregion



    #region GetAll

    [FoldoutGroup("Get All Users")]
    [Button("Get All Users As List")]
    public void GetAllUsers()
    {
        StartCoroutine(GetAllUserCoroutine());
    }

    private IEnumerator GetAllUserCoroutine()
    {
        var query = new QueryBuilder<User>().ContentTypeIs("user");
        var fetchUsersRequest = _client.GetEntries(query);

        while (!fetchUsersRequest.IsCompleted) yield return null;

        allUsers = fetchUsersRequest.Result.ToList();
    }

    [FoldoutGroup("Get All Users")]
    [SerializeField]
    private List<User> allUsers;

    #endregion




    #region Create

    [FoldoutGroup("Create New User")]
    [SerializeField]
    public User userToCreate = new User();

    [FoldoutGroup("Create New User")]
    [Button("Create User")]
    public void CreateUser()
    {
        StartCoroutine(CreateUserCoroutine());
    }
    
    private IEnumerator CreateUserCoroutine()
    {
        var userEntry = new Entry<dynamic>();
        userEntry.SystemProperties = new SystemProperties();
        userEntry.SystemProperties.Id = "User";

        userEntry.Fields = new
        {
            firstName = new Dictionary<string, string>()
            {
                {"en-US", userToCreate.firstName }
            },
            lastName = new Dictionary<string, string>()
            {
                {"en-US", userToCreate.lastName }
            },
            email = new Dictionary<string, string>()
            {
                {"en-US", userToCreate.email }
            },
        };

        var createEntry = _mClient.CreateEntry(userEntry, "user");
        while (!createEntry.IsCompleted) yield return null;

        var createdEntry = createEntry.Result;

        var publishEntry = _mClient.PublishEntry(createdEntry.SystemProperties.Id, 1);
        while (!publishEntry.IsCompleted) yield return null;

        print("dun did it");
    }

    #endregion
}