Skip to content

Commit

Permalink
Merge pull request #338 from PinguApps/update-preferences
Browse files Browse the repository at this point in the history
Implemented Update preferences
  • Loading branch information
pingu2k4 authored Oct 20, 2024
2 parents 64bf8aa + 3354d4e commit 9a2256b
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 23 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(

## ⌛ Progress
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third, `blue` for 100% -->
![Server & Client - 124 / 318](https://img.shields.io/badge/Server_&_Client-124%20%2F%20318-gold?style=for-the-badge)
![Server & Client - 126 / 318](https://img.shields.io/badge/Server_&_Client-126%20%2F%20318-gold?style=for-the-badge)

![Server - 65 / 225](https://img.shields.io/badge/Server-65%20%2F%20225-red?style=for-the-badge)
![Server - 66 / 225](https://img.shields.io/badge/Server-66%20%2F%20225-red?style=for-the-badge)

![Client - 59 / 93](https://img.shields.io/badge/Client-59%20%2F%2093-gold?style=for-the-badge)
![Client - 60 / 93](https://img.shields.io/badge/Client-60%20%2F%2093-gold?style=for-the-badge)

### 🔑 Key
| Icon | Definition |
Expand Down Expand Up @@ -256,7 +256,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Update Phone Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhoneVerification) |||

### Teams
![Teams - 24 / 26](https://img.shields.io/badge/Teams-24%20%2F%2026-forestgreen?style=for-the-badge)
![Teams - 26 / 26](https://img.shields.io/badge/Teams-24%20%2F%2026-blue?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -272,7 +272,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Delete Team Membership](https://appwrite.io/docs/references/1.6.x/client-rest/teams#deleteMembership) |||
| [Update Team Membership Status](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updateMembershipStatus) |||
| [Get Team Memberships](https://appwrite.io/docs/references/1.6.x/client-rest/teams#getPrefs) |||
| [Update Preferences](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updatePrefs) | | |
| [Update Preferences](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updatePrefs) | | |

### Databases
![Databases - 0 / 47](https://img.shields.io/badge/Databases-0%20%2F%2047-red?style=for-the-badge)
Expand Down
11 changes: 8 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Teams;
Expand Down Expand Up @@ -112,6 +111,12 @@ public interface ITeamsClient
/// <param name="request">The request content</param>
/// <returns>The preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetTeamPreferences(GetTeamPreferencesRequest request);
[Obsolete("This method hasn't yet been implemented!")]

/// <summary>
/// Update the team's preferences by its unique ID. The object you pass is stored as is and replaces any previous value. The maximum allowed prefs size is 64kB and throws an error if exceeded.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/teams#updatePrefs">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request);
}
18 changes: 15 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Internals;
Expand Down Expand Up @@ -229,7 +228,20 @@ public async Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetTeamPr
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request)
{
try
{
request.Validate(true);

var result = await _teamsApi.UpdatePreferences(GetCurrentSessionOrThrow(), request.TeamId, request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<IReadOnlyDictionary<string, string>>();
}
}
}
20 changes: 12 additions & 8 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var request = new GetTeamPreferencesRequest()
var request = new UpdatePreferencesRequest()
{
TeamId = "67142b78001c379958cb"
TeamId = "67142b78001c379958cb",
Preferences = new Dictionary<string, string>()
{
{"key", "value"}
}
};

var clientResponse = await _client.Teams.GetTeamPreferences(request);
//var clientResponse = await _client.Teams.UpdatePreferences(request);

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

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

var serverResponse = await _server.Teams.GetTeamPreferences(request);
var serverResponse = await _server.Teams.UpdatePreferences(request);

Console.WriteLine(serverResponse.Result.Match(
result => result.ToString(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public interface ITeamsClient
/// <param name="request">The request content</param>
/// <returns>The preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetTeamPreferences(GetTeamPreferencesRequest request);
[Obsolete("This method hasn't yet been implemented!")]

/// <summary>
/// Update the team's preferences by its unique ID. The object you pass is stored as is and replaces any previous value. The maximum allowed prefs size is 64kB and throws an error if exceeded.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/teams#updatePrefs">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request);
}
18 changes: 15 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
Expand Down Expand Up @@ -229,7 +228,20 @@ public async Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetTeamPr
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult<IReadOnlyDictionary<string, string>>> UpdatePreferences(UpdatePreferencesRequest request)
{
try
{
request.Validate(true);

var result = await _teamsApi.UpdatePreferences(request.TeamId, request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<IReadOnlyDictionary<string, string>>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System.Net;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Tests;
using PinguApps.Appwrite.Shared.Utils;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Teams;
public partial class TeamsClientTests
{
[Fact]
public async Task UpdatePreferences_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders(true)
.WithJsonContent(request, _jsonSerializerOptions)
.Respond(TestConstants.AppJson, TestConstants.PreferencesResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

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

[Fact]
public async Task UpdatePreferences_ShouldReturnError_WhenSessionIsNull()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsInternalError);
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message);
}

[Fact]
public async Task UpdatePreferences_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders(true)
.WithJsonContent(request, _jsonSerializerOptions)
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

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

[Fact]
public async Task UpdatePreferences_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders(true)
.WithJsonContent(request, _jsonSerializerOptions)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(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,90 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Tests;
using PinguApps.Appwrite.Shared.Utils;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Teams;
public partial class TeamsClientTests
{
[Fact]
public async Task UpdatePreferences_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders()
.WithJsonContent(request, _jsonSerializerOptions)
.Respond(TestConstants.AppJson, TestConstants.PreferencesResponse);

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

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

[Fact]
public async Task UpdatePreferences_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders()
.WithJsonContent(request, _jsonSerializerOptions)
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

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

[Fact]
public async Task UpdatePreferences_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdatePreferencesRequest
{
TeamId = IdUtils.GenerateUniqueId(),
Preferences = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};

_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/teams/{request.TeamId}/prefs")
.ExpectedHeaders()
.WithJsonContent(request, _jsonSerializerOptions)
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Teams.UpdatePreferences(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}

0 comments on commit 9a2256b

Please sign in to comment.