DatabaseManager.cs
3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
}