From 25a78e2a300fd316f0fa67b573046380edea9ab2 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 28 Nov 2024 18:31:08 +0000 Subject: [PATCH 1/3] implemented get database --- src/PinguApps.Appwrite.Playground/App.cs | 6 +++--- .../Clients/DatabasesClient.cs | 17 +++++++++++++++-- .../Clients/IDatabasesClient.cs | 11 ++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 146e2fe5..d805a54e 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -17,12 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, public async Task Run(string[] args) { - var request = new DeleteDatabaseRequest() + var request = new GetDatabaseRequest() { - DatabaseId = "6748b1cb000b8513a348" + DatabaseId = "6748b44d000b2b0e73ac" }; - var response = await _server.Databases.DeleteDatabase(request); + var response = await _server.Databases.GetDatabase(request); Console.WriteLine(response.Result.Match( result => result.ToString(), diff --git a/src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs b/src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs index a486541d..cb0c435c 100644 --- a/src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs @@ -72,9 +72,22 @@ public async Task DeleteDatabase(DeleteDatabaseRequest request) } } - [ExcludeFromCodeCoverage] /// - public Task> GetDatabase(GetDatabaseRequest request) => throw new NotImplementedException(); + public async Task> GetDatabase(GetDatabaseRequest request) + { + try + { + request.Validate(true); + + var result = await _databasesApi.GetDatabase(request.DatabaseId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// diff --git a/src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs b/src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs index acdd5cd4..de076a54 100644 --- a/src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs @@ -41,6 +41,14 @@ public interface IDatabasesClient /// 200 Success Response Task DeleteDatabase(DeleteDatabaseRequest request); + /// + /// Get a database by its unique ID. This endpoint response returns a JSON object with the database metadata. + /// Appwrite Docs + /// + /// The request content + /// The database + Task> GetDatabase(GetDatabaseRequest request); + [Obsolete("Endpoint not yet implemented.")] Task> CreateBooleanAttribute(CreateBooleanAttributeRequest request); @@ -98,9 +106,6 @@ public interface IDatabasesClient [Obsolete("Endpoint not yet implemented.")] Task> GetCollection(GetCollectionRequest request); - [Obsolete("Endpoint not yet implemented.")] - Task> GetDatabase(GetDatabaseRequest request); - [Obsolete("Endpoint not yet implemented.")] Task> GetDocument(GetDocumentRequest request); From 23003d87a55f43b1b94059418f4bac368e67effb Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 28 Nov 2024 18:31:16 +0000 Subject: [PATCH 2/3] added tests for get database --- .../DatabasesClientTests.GetDatabase.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.GetDatabase.cs diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.GetDatabase.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.GetDatabase.cs new file mode 100644 index 00000000..7e5a022f --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.GetDatabase.cs @@ -0,0 +1,72 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Databases; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Server.Tests.Clients.Databases; +public partial class DatabasesClientTests +{ + [Fact] + public async Task GetDatabase_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new GetDatabaseRequest + { + DatabaseId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") + .ExpectedHeaders() + .Respond(TestConstants.AppJson, TestConstants.DatabaseResponse); + + // Act + var result = await _appwriteClient.Databases.GetDatabase(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetDatabase_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new GetDatabaseRequest + { + DatabaseId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + // Act + var result = await _appwriteClient.Databases.GetDatabase(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task GetDatabase_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new GetDatabaseRequest + { + DatabaseId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Databases.GetDatabase(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 5995bae999b4e6364d38a5b9393d008d1d76547d Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Thu, 28 Nov 2024 18:32:02 +0000 Subject: [PATCH 3/3] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 63b459d2..4f376af8 100644 --- a/README.md +++ b/README.md @@ -141,9 +141,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## ⌛ Progress -![Server & Client - 128 / 317](https://img.shields.io/badge/Server_&_Client-128%20%2F%20317-gold?style=for-the-badge) +![Server & Client - 129 / 317](https://img.shields.io/badge/Server_&_Client-129%20%2F%20317-gold?style=for-the-badge) -![Server - 68 / 224](https://img.shields.io/badge/Server-68%20%2F%20224-red?style=for-the-badge) +![Server - 69 / 224](https://img.shields.io/badge/Server-69%20%2F%20224-red?style=for-the-badge) ![Client - 60 / 93](https://img.shields.io/badge/Client-60%20%2F%2093-gold?style=for-the-badge) @@ -275,13 +275,13 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Update Preferences](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updatePrefs) | ✅ | ✅ | ### Databases -![Databases - 3 / 47](https://img.shields.io/badge/Databases-3%20%2F%2047-red?style=for-the-badge) +![Databases - 4 / 47](https://img.shields.io/badge/Databases-4%20%2F%2047-red?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| | [List Databases](https://appwrite.io/docs/references/1.6.x/server-rest/databases#list) | ❌ | ✅ | | [Create Databases](https://appwrite.io/docs/references/1.6.x/server-rest/databases#create) | ❌ | ✅ | -| [Get Database](https://appwrite.io/docs/references/1.6.x/server-rest/databases#get) | ❌ | ⬛ | +| [Get Database](https://appwrite.io/docs/references/1.6.x/server-rest/databases#get) | ❌ | ✅ | | [Update Database](https://appwrite.io/docs/references/1.6.x/server-rest/databases#update) | ❌ | ⬛ | | [Delete Database](https://appwrite.io/docs/references/1.6.x/server-rest/databases#delete) | ❌ | ✅ | | [List Collections](https://appwrite.io/docs/references/1.6.x/server-rest/databases#listCollections) | ❌ | ⬛ |