Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get database #550

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(

## ⌛ Progress
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third, `blue` for 100% -->
![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)

Expand Down Expand Up @@ -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) | ❌ | ⬛ |
Expand Down
6 changes: 3 additions & 3 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,22 @@ public async Task<AppwriteResult> DeleteDatabase(DeleteDatabaseRequest request)
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request)
{
try
{
request.Validate(true);

var result = await _databasesApi.GetDatabase(request.DatabaseId);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<Database>();
}
Comment on lines +86 to +89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Error Handling severity potentially major

Log exception before returning the exception response.

Tell me more

Consider logging the exception before returning the exception response. This will help with debugging and monitoring the application. You can add a logging statement like _logger.LogError(e, "Error occurred while getting database"); before returning the exception response.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +86 to +89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Functionality severity potentially major

Overly broad exception handling

Tell me more

What is the issue?
The catch block catches all exceptions without distinguishing between expected API exceptions and unexpected runtime exceptions.

Why this matters
In a database retrieval operation, different types of failures (e.g., database not found vs network error) should be handled differently to provide appropriate error responses.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
11 changes: 8 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public interface IDatabasesClient
/// <returns>200 Success Response</returns>
Task<AppwriteResult> DeleteDatabase(DeleteDatabaseRequest request);

/// <summary>
/// Get a database by its unique ID. This endpoint response returns a JSON object with the database metadata.
/// <para><see href="https://appwrite.io/docs/references/cloud/server-rest/databases#get">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The database</returns>
Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<AttributeBoolean>> CreateBooleanAttribute(CreateBooleanAttributeRequest request);

Expand Down Expand Up @@ -98,9 +106,6 @@ public interface IDatabasesClient
[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Collection>> GetCollection(GetCollectionRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Document>> GetDocument(GetDocumentRequest request);

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}