Skip to content

Commit

Permalink
Merge pull request #554 from PinguApps/375-get-attribute
Browse files Browse the repository at this point in the history
Completed all attribute endpoints
  • Loading branch information
pingu2k4 authored Dec 6, 2024
2 parents c9df8ed + 906e588 commit 1cb7243
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 37 deletions.
10 changes: 5 additions & 5 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 - 156 / 317](https://img.shields.io/badge/Server_&_Client-156%20%2F%20317-gold?style=for-the-badge)
![Server & Client - 158 / 317](https://img.shields.io/badge/Server_&_Client-158%20%2F%20317-gold?style=for-the-badge)

![Server - 96 / 224](https://img.shields.io/badge/Server-96%20%2F%20224-gold?style=for-the-badge)
![Server - 98 / 224](https://img.shields.io/badge/Server-98%20%2F%20224-gold?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,7 +275,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Update Preferences](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updatePrefs) |||

### Databases
![Databases - 31 / 47](https://img.shields.io/badge/Databases-31%20%2F%2047-gold?style=for-the-badge)
![Databases - 33 / 47](https://img.shields.io/badge/Databases-33%20%2F%2047-forestgreen?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -309,8 +309,8 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Update String Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#updateStringAttribute) |||
| [Create URL Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#createUrlAttribute) |||
| [Update URL Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#updateUrlAttribute) |||
| [Get Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#getAttribute) || |
| [Delete Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#deleteAttribute) || |
| [Get Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#getAttribute) || |
| [Delete Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#deleteAttribute) || |
| [Update Relationship Attribute](https://appwrite.io/docs/references/1.6.x/server-rest/databases#updateRelationshipAttribute) |||
| [List Documents](https://appwrite.io/docs/references/1.6.x/client-rest/databases#listDocuments) |||
| [Create Document](https://appwrite.io/docs/references/1.6.x/client-rest/databases#createDocument) |||
Expand Down
30 changes: 4 additions & 26 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,16 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var key = "url";
var newKey = $"new_{key}";

var createRequest = new CreateRelationshipAttributeRequest()
{
DatabaseId = "6748b44d000b2b0e73ac",
CollectionId = "6748bb30002a12d4708f",
Key = key,
RelatedCollectionId = "674f57a30011310c0b3c"
};

var createResponse = await _server.Databases.CreateRelationshipAttribute(createRequest);

Console.WriteLine(createResponse.Result.Match(
result => result.ToString(),
appwriteError => appwriteError.Message,
internalError => internalError.Message));

Console.WriteLine("#############################################################################################");
Console.ReadKey();

var updateRequest = new UpdateRelationshipAttributeRequest()
var request = new DeleteAttributeRequest()
{
DatabaseId = "6748b44d000b2b0e73ac",
CollectionId = "6748bb30002a12d4708f",
Key = key,
NewKey = newKey
Key = "test23"
};

var updateResponse = await _server.Databases.UpdateRelationshipAttribute(updateRequest);
var response = await _server.Databases.DeleteAttribute(request);

Console.WriteLine(updateResponse.Result.Match(
Console.WriteLine(response.Result.Match(
result => result.ToString(),
appwriteError => appwriteError.Message,
internalError => internalError.Message));
Expand Down
34 changes: 30 additions & 4 deletions src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,39 @@ public async Task<AppwriteResult<AttributeUrl>> UpdateUrlAttribute(UpdateURLAttr
}
}

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

var result = await _databasesApi.DeleteAttribute(request.DatabaseId, request.CollectionId, request.Key);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse();
}
}

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

var result = await _databasesApi.GetAttribute(request.DatabaseId, request.CollectionId, request.Key);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<Attribute>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<AttributeRelationship>> UpdateRelationshipAttribute(UpdateRelationshipAttributeRequest request)
Expand Down
14 changes: 12 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/IDatabasesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,20 @@ public interface IDatabasesClient
/// <returns>The url attribute</returns>
Task<AppwriteResult<AttributeUrl>> UpdateUrlAttribute(UpdateURLAttributeRequest request);

[Obsolete("Endpoint not yet implemented.")]
/// <summary>
/// Deletes an attribute.
/// <para><see href="https://appwrite.io/docs/references/cloud/server-rest/databases#deleteAttribute">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteAttribute(DeleteAttributeRequest request);

[Obsolete("Endpoint not yet implemented.")]
/// <summary>
/// Get attribute by ID.
/// <para><see href="https://appwrite.io/docs/references/cloud/server-rest/databases#getAttribute">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The attribute</returns>
Task<AppwriteResult<Attribute>> GetAttribute(GetAttributeRequest request);

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/PinguApps.Appwrite.Shared/Responses/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace PinguApps.Appwrite.Shared.Responses;
/// <param name="Array">Is attribute an array?</param>
/// <param name="CreatedAt">Attribute creation date in ISO 8601 format</param>
/// <param name="UpdatedAt">Attribute update date in ISO 8601 format</param>
[JsonConverter(typeof(AttributeJsonConverter))]
public abstract record Attribute(
[property: JsonPropertyName("key")] string Key,
[property: JsonPropertyName("type")] string Type,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 DeleteAttribute_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new DeleteAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Respond(HttpStatusCode.NoContent);

// Act
var result = await _appwriteClient.Databases.DeleteAttribute(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task DeleteAttribute_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new DeleteAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

// Act
var result = await _appwriteClient.Databases.DeleteAttribute(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task DeleteAttribute_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new DeleteAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Databases.DeleteAttribute(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 GetAttribute_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new GetAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Respond(TestConstants.AppJson, TestConstants.AttributeResponse);

// Act
var result = await _appwriteClient.Databases.GetAttribute(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task GetAttribute_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new GetAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

// Act
var result = await _appwriteClient.Databases.GetAttribute(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task GetAttribute_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new GetAttributeRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
Key = "attributeKey"
};

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/attributes/{request.Key}")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Databases.GetAttribute(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
14 changes: 14 additions & 0 deletions tests/PinguApps.Appwrite.Shared.Tests/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,20 @@ public static class TestConstants
}
""";

public const string AttributeResponse = """
{
"key": "isEnabled",
"type": "boolean",
"status": "available",
"error": "string",
"required": true,
"array": false,
"$createdAt": "2020-10-15T06:38:00.000+00:00",
"$updatedAt": "2020-10-15T06:38:00.000+00:00",
"default": false
}
""";

public const string IndexResponse = """
{
"key": "index1",
Expand Down

0 comments on commit 1cb7243

Please sign in to comment.