Skip to content

Commit

Permalink
Merge pull request #317 from PinguApps/301-list-teams
Browse files Browse the repository at this point in the history
Implemented list teams
  • Loading branch information
pingu2k4 authored Oct 18, 2024
2 parents fa0ff91 + 7e82153 commit a1543e2
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 59 deletions.
11 changes: 5 additions & 6 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 - 100 / 295](https://img.shields.io/badge/Server_&_Client-100%20%2F%20295-gold?style=for-the-badge)
![Server & Client - 102 / 295](https://img.shields.io/badge/Server_&_Client-102%20%2F%20295-gold?style=for-the-badge)

![Server - 53 / 202](https://img.shields.io/badge/Server-53%20%2F%20202-red?style=for-the-badge)
![Server - 54 / 202](https://img.shields.io/badge/Server-54%20%2F%20202-red?style=for-the-badge)

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

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

### Teams
<!-- ![0 / 26](https://progress-bar.dev/0/?scale=26&suffix=%20/%2026&width=120) -->
![Teams - 0 / 26](https://img.shields.io/badge/Teams-0%20%2F%2026-red?style=for-the-badge)
![Teams - 2 / 26](https://img.shields.io/badge/Teams-2%20%2F%2026-red?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
| [List Teams](https://appwrite.io/docs/references/1.6.x/client-rest/teams#list) | | |
| [List Teams](https://appwrite.io/docs/references/1.6.x/client-rest/teams#list) | | |
| [Create Team](https://appwrite.io/docs/references/1.6.x/client-rest/teams#create) |||
| [Get Team](https://appwrite.io/docs/references/1.6.x/client-rest/teams#get) |||
| [Update Name](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updateName) |||
Expand Down
25 changes: 1 addition & 24 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace PinguApps.Appwrite.Client;

/// <inheritdoc/>
public class AccountClient : IAccountClient, ISessionAware
public class AccountClient : SessionAwareClientBase, IAccountClient
{
private readonly IAccountApi _accountApi;
private readonly Config _config;
Expand All @@ -23,29 +23,6 @@ public AccountClient(IServiceProvider services, Config config)
_config = config;
}

string? ISessionAware.Session { get; set; }

ISessionAware? _sessionAware;
/// <summary>
/// Get the current session
/// </summary>
public string? Session => GetCurrentSession();

private string? GetCurrentSession()
{
if (_sessionAware is null)
{
_sessionAware = this;
}

return _sessionAware.Session;
}

private string GetCurrentSessionOrThrow()
{
return GetCurrentSession() ?? throw new Exception(ISessionAware.SessionExceptionMessage);
}

/// <inheritdoc/>
public async Task<AppwriteResult<User>> Get()
{
Expand Down
1 change: 1 addition & 0 deletions src/PinguApps.Appwrite.Client/Clients/AppwriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public void SetSession(string? session)
{
(this as ISessionAware).UpdateSession(session);
(Account as ISessionAware)!.UpdateSession(session);
(Teams as ISessionAware)!.UpdateSession(session);
}
}
5 changes: 4 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/IAppwriteClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace PinguApps.Appwrite.Client;
using PinguApps.Appwrite.Client.Clients;

namespace PinguApps.Appwrite.Client;

/// <summary>
/// The root of the Client SDK. Access all API sections from here
Expand All @@ -23,4 +25,5 @@ public interface IAppwriteClient
/// The sessio of the currently logged in user
/// </summary>
string? Session { get; }
ITeamsClient Teams { get; }
}
7 changes: 6 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ namespace PinguApps.Appwrite.Client.Clients;
/// </summary>
public interface ITeamsClient
{
[Obsolete("This method hasn't yet been implemented!")]
/// <summary>
/// Get a list of all the teams in which the current user is a member. You can use the parameters to filter your results.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/teams#list">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The teams list</returns>
Task<AppwriteResult<TeamsList>> ListTeams(ListTeamsRequest request);
[Obsolete("This method hasn't yet been implemented!")]
Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request);
Expand Down
22 changes: 22 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/SessionAwareClientBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace PinguApps.Appwrite.Client.Clients;
public abstract class SessionAwareClientBase : ISessionAware
{
string? ISessionAware.Session { get; set; }

/// <summary>
/// Get the current session
/// </summary>
public string? Session => GetCurrentSession();

protected string? GetCurrentSession()
{
return ((ISessionAware)this).Session;
}

public string GetCurrentSessionOrThrow()
{
return GetCurrentSession() ?? throw new Exception(ISessionAware.SessionExceptionMessage);
}
}
21 changes: 18 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Responses;

namespace PinguApps.Appwrite.Client.Clients;

/// <inheritdoc/>
public class TeamsClient : ITeamsClient
public class TeamsClient : SessionAwareClientBase, ITeamsClient
{
private readonly ITeamsApi _teamsApi;
private readonly Config _config;
Expand All @@ -22,8 +23,22 @@ public TeamsClient(IServiceProvider services, Config config)
_config = config;
}

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

var result = await _teamsApi.ListTeams(GetCurrentSessionOrThrow(), RequestUtils.GetQueryStrings(request.Queries), request.Search);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
26 changes: 13 additions & 13 deletions src/PinguApps.Appwrite.Client/Internals/ITeamsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ internal interface ITeamsApi : IBaseApi
{
[Get("/teams")]
[QueryUriFormat(UriFormat.Unescaped)]
Task<IApiResponse<TeamsList>> ListTeams([Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries, string? search);
Task<IApiResponse<TeamsList>> ListTeams([Header("x-appwrite-session")] string session, [Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries, string? search);

[Post("/teams")]
Task<IApiResponse<Team>> CreateTeam(CreateTeamRequest request);
Task<IApiResponse<Team>> CreateTeam([Header("x-appwrite-session")] string session, CreateTeamRequest request);

[Delete("/teams/{teamId}")]
Task<IApiResponse> DeleteTeam(string teamId);
Task<IApiResponse> DeleteTeam([Header("x-appwrite-session")] string session, string teamId);

[Get("/teams/{teamId}")]
Task<IApiResponse<Team>> GetTeam(string teamId);
Task<IApiResponse<Team>> GetTeam([Header("x-appwrite-session")] string session, string teamId);

[Put("/teams/{teamId}")]
Task<IApiResponse<Team>> UpdateName(string teamId, UpdateNameRequest request);
Task<IApiResponse<Team>> UpdateName([Header("x-appwrite-session")] string session, string teamId, UpdateNameRequest request);

[Get("/teams/{teamId}/memberships")]
[QueryUriFormat(UriFormat.Unescaped)]
Task<IApiResponse<MembershipsList>> ListTeamMemberships(string teamId, [Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries, string? search);
Task<IApiResponse<MembershipsList>> ListTeamMemberships([Header("x-appwrite-session")] string session, string teamId, [Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries, string? search);

[Post("/teams/{teamId}/memberships")]
Task<IApiResponse<Membership>> CreateTeamMembership(string teamId, CreateTeamMembershipRequest request);
Task<IApiResponse<Membership>> CreateTeamMembership([Header("x-appwrite-session")] string session, string teamId, CreateTeamMembershipRequest request);

[Delete("/teams/{teamId}/memberships/{membershipId}")]
Task<IApiResponse> DeleteTeamMembership(string teamId, string membershipId);
Task<IApiResponse> DeleteTeamMembership([Header("x-appwrite-session")] string session, string teamId, string membershipId);

[Get("/teams/{teamId}/memberships/{membershipId}")]
Task<IApiResponse<Membership>> GetTeamMembership(string teamId, string membershipId);
Task<IApiResponse<Membership>> GetTeamMembership([Header("x-appwrite-session")] string session, string teamId, string membershipId);

[Patch("/teams/{teamId}/memberships/{membershipId}")]
Task<IApiResponse<Membership>> UpdateMembership(string teamId, string membershipId, UpdateMembershipRequest request);
Task<IApiResponse<Membership>> UpdateMembership([Header("x-appwrite-session")] string session, string teamId, string membershipId, UpdateMembershipRequest request);

[Patch("/teams/{teamId}/memberships/{membershipId}/status")]
Task<IApiResponse<Membership>> UpdateTeamMembershipStatus(string teamId, string membershipId, UpdateTeamMembershipStatusRequest request);
Task<IApiResponse<Membership>> UpdateTeamMembershipStatus([Header("x-appwrite-session")] string session, string teamId, string membershipId, UpdateTeamMembershipStatusRequest request);

[Get("/teams/{teamId}/prefs")]
Task<IApiResponse<IReadOnlyDictionary<string, string>>> GetTeamPreferences(string teamId);
Task<IApiResponse<IReadOnlyDictionary<string, string>>> GetTeamPreferences([Header("x-appwrite-session")] string session, string teamId);

[Put("/teams/{teamId}/prefs")]
Task<IApiResponse<IReadOnlyDictionary<string, string>>> UpdatePreferences(string teamId, UpdatePreferencesRequest request);
Task<IApiResponse<IReadOnlyDictionary<string, string>>> UpdatePreferences([Header("x-appwrite-session")] string session, string teamId, UpdatePreferencesRequest request);
}
18 changes: 13 additions & 5 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Configuration;
using PinguApps.Appwrite.Shared.Requests.Account;
using PinguApps.Appwrite.Shared.Requests.Teams;

namespace PinguApps.Appwrite.Playground;
internal class App
Expand All @@ -19,14 +19,22 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var request = new DeletePushTargetRequest()
var request = new ListTeamsRequest()
{
TargetId = "670be3330025c0f23b93"
};

var response = await _client.Account.DeletePushTarget(request);
var clientResponse = await _client.Teams.ListTeams(request);

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

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

var serverResponse = await _server.Teams.ListTeams(request);

Console.WriteLine(serverResponse.Result.Match(
result => result.ToString(),
appwriteError => appwriteError.Message,
internalError => internalError.Message));
Expand Down
1 change: 1 addition & 0 deletions src/PinguApps.Appwrite.Server/Clients/IAppwriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public interface IAppwriteClient
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users">Appwrite Docs</see></para>
/// </summary>
IUsersClient Users { get; }
ITeamsClient Teams { get; }
}
7 changes: 6 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ namespace PinguApps.Appwrite.Server.Clients;
/// </summary>
public interface ITeamsClient
{
[Obsolete("This method hasn't yet been implemented!")]
/// <summary>
/// Get a list of all the teams in which the current user is a member. You can use the parameters to filter your results.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/teams#list">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The teams list</returns>
Task<AppwriteResult<TeamsList>> ListTeams(ListTeamsRequest request);
[Obsolete("This method hasn't yet been implemented!")]
Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request);
Expand Down
19 changes: 17 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Responses;
Expand All @@ -22,8 +23,22 @@ public TeamsClient(IServiceProvider services, Config config)
_config = config;
}

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

var result = await _teamsApi.ListTeams(RequestUtils.GetQueryStrings(request.Queries), request.Search);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AccountClientTests()
_mockHttp = new MockHttpMessageHandler();
var services = new ServiceCollection();

services.AddAppwriteClientForServer("PROJECT_ID", TestConstants.Endpoint, new RefitSettings
services.AddAppwriteClientForServer(TestConstants.ProjectId, TestConstants.Endpoint, new RefitSettings
{
HttpMessageHandlerFactory = () => _mockHttp
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void SetSession_UpdatesSession()
var mockAccountClient = new Mock<IAccountClient>();
var mockTeamsClient = new Mock<ITeamsClient>();
mockAccountClient.As<ISessionAware>();
mockTeamsClient.As<ISessionAware>();
var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object);

// Act
Expand All @@ -58,6 +59,7 @@ public void SetSession_UpdatesSessionInAccountClient()
var mockAccountClient = new Mock<IAccountClient>();
var mockTeamsClient = new Mock<ITeamsClient>();
mockAccountClient.As<ISessionAware>();
mockTeamsClient.As<ISessionAware>();
var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
namespace PinguApps.Appwrite.Client.Tests.Clients;

public static class AccountTestsExtensions
public static class ClientTestsExtensions
{
public static MockedRequest ExpectedHeaders(this MockedRequest request, bool addSessionHeaders = false)
{
Expand Down
Loading

0 comments on commit a1543e2

Please sign in to comment.