Skip to content

Commit

Permalink
DTO change
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Guldborg committed Jan 18, 2024
1 parent a7bb8f3 commit 8c081e3
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 44 deletions.
45 changes: 42 additions & 3 deletions Shifty.Api/Generated/AnalogCoreV2/AnalogCoreV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ public virtual async System.Threading.Tasks.Task ApiV2AccountResendVerificationE
/// <param name="pageLength">The length of a page</param>
/// <returns>Users, possible with filter applied</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<SimpleUserResponse> ApiV2AccountSearchAsync(int? pageNum, string filter, int? pageLength)
public virtual System.Threading.Tasks.Task<UserSearchResponse> ApiV2AccountSearchAsync(int? pageNum, string filter, int? pageLength)
{
return ApiV2AccountSearchAsync(pageNum, filter, pageLength, System.Threading.CancellationToken.None);
}
Expand All @@ -747,7 +747,7 @@ public virtual System.Threading.Tasks.Task<SimpleUserResponse> ApiV2AccountSearc
/// <param name="pageLength">The length of a page</param>
/// <returns>Users, possible with filter applied</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<SimpleUserResponse> ApiV2AccountSearchAsync(int? pageNum, string filter, int? pageLength, System.Threading.CancellationToken cancellationToken)
public virtual async System.Threading.Tasks.Task<UserSearchResponse> ApiV2AccountSearchAsync(int? pageNum, string filter, int? pageLength, System.Threading.CancellationToken cancellationToken)
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append("api/v2/account/search?");
Expand Down Expand Up @@ -807,7 +807,7 @@ public virtual async System.Threading.Tasks.Task<SimpleUserResponse> ApiV2Accoun
else
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<SimpleUserResponse>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<UserSearchResponse>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
Expand Down Expand Up @@ -2705,22 +2705,61 @@ public partial class ResendAccountVerificationEmailRequest

}

/// <summary>
/// Represents a search result
/// </summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v10.0.0.0))")]
public partial class UserSearchResponse
{
/// <summary>
/// The users that match the query
/// </summary>
[Newtonsoft.Json.JsonProperty("users", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.ICollection<SimpleUserResponse> Users { get; set; } = new System.Collections.ObjectModel.Collection<SimpleUserResponse>();

/// <summary>
/// The number of users that match the query
/// </summary>
[Newtonsoft.Json.JsonProperty("totalUsers", Required = Newtonsoft.Json.Required.Always)]
public int TotalUsers { get; set; }

}

/// <summary>
/// Basic User details
/// </summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v10.0.0.0))")]
public partial class SimpleUserResponse
{
/// <summary>
/// User Id
/// </summary>
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int Id { get; set; }

/// <summary>
/// User's Display Name
/// </summary>
[Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Name { get; set; }

/// <summary>
/// User's Email
/// </summary>
[Newtonsoft.Json.JsonProperty("email", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Email { get; set; }

/// <summary>
/// User's User group relationship
/// </summary>
[Newtonsoft.Json.JsonProperty("userGroup", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public UserGroup UserGroup { get; set; }

/// <summary>
/// User's State
/// </summary>
[Newtonsoft.Json.JsonProperty("state", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public UserState State { get; set; }
Expand Down
48 changes: 18 additions & 30 deletions Shifty.App/Components/UserTable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<MudContainer MaxWidth="MaxWidth.Medium" Style="margin-top: 20px;">
<MudTable
T="UserSearchResponse"
ServerData="@(new Func<TableState, Task<TableData<UserSearchResponse>>>(LoadUsers))"
T="SimpleUserResponse"
ServerData="@(new Func<TableState, Task<TableData<SimpleUserResponse>>>(LoadUsers))"
@ref="_table"
EditTrigger="TableEditTrigger.EditButton"
ApplyButtonPosition="TableApplyButtonPosition.End"
Expand All @@ -36,22 +36,22 @@
<MudTh>Id</MudTh>
<MudTh>Name</MudTh>
<MudTh>Email</MudTh>
<MudTh>Role</MudTh>
<MudTh>UserGroup</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Id">@context.Id</MudTd>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Email">@context.Email</MudTd>
<MudTd DataLabel="Role">@context.Role</MudTd>
<MudTd DataLabel="UserGroup">@context.UserGroup</MudTd>
</RowTemplate>
<RowEditingTemplate>
<MudTd DataLabel="Id">@context.Id</MudTd>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="Email">@context.Email</MudTd>
<MudTd DataLabel="Role">
<MudSelect T="UserRole" Label="UserRole" @bind-Value="context.Role">
@foreach (var role in Enum.GetValues<UserRole>()) {
<MudSelectItem T="UserRole" Value="@role">@role</MudSelectItem>
<MudTd DataLabel="UserGroup">
<MudSelect T="UserGroup" Label="UserGroup" @bind-Value="context.UserGroup">
@foreach (var group in Enum.GetValues<UserGroup>()) {
<MudSelectItem T="UserGroup" Value="@group">@group</MudSelectItem>
}
</MudSelect>
</MudTd>
Expand All @@ -66,21 +66,21 @@

@code
{
private MudTable<UserSearchResponse> _table;
private MudTable<SimpleUserResponse> _table;
private string searchString = "";
UserRole RoleBeforeEdit;
UserGroup UserGroupBeforeEdit;

private async Task<TableData<UserSearchResponse>> LoadUsers(TableState state)
private async Task<TableData<SimpleUserResponse>> LoadUsers(TableState state)
{
var result = await _userService.SearchUsers(searchString, state.Page, state.PageSize);

return result.Match(
Succ: users => {
return new TableData<UserSearchResponse>(){ Items = users.ToList(), TotalItems = 15};;
Succ: res => {
return new TableData<SimpleUserResponse>(){ Items = res.Users.AsEnumerable(), TotalItems = res.TotalUsers};;
},
Fail: error => {
Snackbar.Add(error.Message, Severity.Error);
return new TableData<UserSearchResponse>(){ Items = new List<UserSearchResponse>(), TotalItems = 0};
return new TableData<SimpleUserResponse>(){ Items = new List<SimpleUserResponse>(), TotalItems = 0};
}
);
}
Expand All @@ -93,9 +93,9 @@

private void UpdateUserGroup(object element)
{
var user = (UserSearchResponse)element;
var user = (SimpleUserResponse)element;

var result = _userService.UpdateUserGroupAsync(user.Id, UserRoleToUserGroup(user.Role));
var result = _userService.UpdateUserGroupAsync(user.Id, user.UserGroup);

result.Match(
Succ: user => {
Expand All @@ -112,26 +112,14 @@

private void BackupUser(object element)
{
RoleBeforeEdit = ((UserSearchResponse)element).Role;
UserGroupBeforeEdit = ((SimpleUserResponse)element).UserGroup;
StateHasChanged();
}

private void ResetUserOnCancel(object user)
{
((UserSearchResponse)user).Role = RoleBeforeEdit;
((SimpleUserResponse)user).UserGroup = UserGroupBeforeEdit;
StateHasChanged();
Snackbar.Add("Canceled editing", Severity.Warning);
}

public static UserGroup UserRoleToUserGroup(UserRole role)
{
return role switch
{
UserRole.Board => UserGroup.Board,
UserRole.Manager => UserGroup.Manager,
UserRole.Barista => UserGroup.Barista,
UserRole.Customer => UserGroup.Customer,
_ => throw new Exception("Invalid user role")
};
}
}
5 changes: 3 additions & 2 deletions Shifty.App/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using LanguageExt;
using LanguageExt.ClassInstances.Pred;
using LanguageExt.Common;
using Shifty.Api.Generated.AnalogCoreV1;
using Shifty.Api.Generated.AnalogCoreV2;
Expand Down Expand Up @@ -30,9 +31,9 @@ public async Task<Either<Error, TokenDto>> LoginAsync(string username, string pa
return await TryAsync(_v1client.ApiV1AccountLoginAsync(loginDto: dto)).ToEither();
}

public async Task<Try<ICollection<UserSearchResponse>>> SearchUserAsync(string query, int page, int pageSize)
public async Task<Try<UserSearchResponse>> SearchUserAsync(string query, int page, int pageSize)
{
return await TryAsync(_v2client.ApiV2AccountSearchAsync(query, page, pageSize));
return await TryAsync(_v2client.ApiV2AccountSearchAsync(filter: query, pageNum: page, pageLength: pageSize));
}

public async Task<Try<Task>> UpdateUserGroupAsync(int userId, UserGroup group)
Expand Down
2 changes: 1 addition & 1 deletion Shifty.App/Repositories/IAccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Shifty.App.Repositories
public interface IAccountRepository
{
public Task<Either<Error, TokenDto>> LoginAsync(string username, string password);
public Task<Try<ICollection<UserSearchResponse>>> SearchUserAsync(string query, int page, int pageSize);
public Task<Try<UserSearchResponse>> SearchUserAsync(string query, int page, int pageSize);
public Task<Try<Task>> UpdateUserGroupAsync(int userId, UserGroup group);
}
}
2 changes: 1 addition & 1 deletion Shifty.App/Services/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Shifty.App.Services
{
public interface IUserService
{
Task<Try<ICollection<UserSearchResponse>>> SearchUsers(string query, int pageNumber = 0, int pageSize = 10);
Task<Try<UserSearchResponse>> SearchUsers(string query, int pageNumber = 0, int pageSize = 10);
Task<Try<Task>> UpdateUserGroupAsync(int userId, UserGroup group);
}
}
2 changes: 1 addition & 1 deletion Shifty.App/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public UserService(IAccountRepository accountRepository)
_accountRepository = accountRepository;
}

public Task<Try<ICollection<UserSearchResponse>>> SearchUsers(string query, int pageNumber = 0, int pageSize = 10)
public Task<Try<UserSearchResponse>> SearchUsers(string query, int pageNumber = 0, int pageSize = 10)
{
return _accountRepository.SearchUserAsync(query, pageNumber, pageSize);
}
Expand Down
68 changes: 62 additions & 6 deletions Shifty.GenerateApi/OpenApiSpecs/AnalogCoreV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SimpleUserResponse"
"$ref": "#/components/schemas/UserSearchResponse"
}
}
}
Expand Down Expand Up @@ -1451,25 +1451,81 @@
}
}
},
"UserSearchResponse": {
"type": "object",
"description": "Represents a search result",
"example": {
"users": [
{
"id": 12232,
"name": "John Doe",
"email": "[email protected]",
"userGroup": "Barista",
"state": "Active"
}
],
"totalUsers": 1
},
"additionalProperties": false,
"required": [
"users",
"totalUsers"
],
"properties": {
"users": {
"type": "array",
"description": "The users that match the query",
"example": "[\n {\n \"id\": 12232,\n \"name\": \"John Doe\",\n \"email\": \"[email protected]\",\n \"userGroup\": \"Barista\",\n \"state\": \"Active\"\n }\n ],",
"items": {
"$ref": "#/components/schemas/SimpleUserResponse"
}
},
"totalUsers": {
"type": "integer",
"description": "The number of users that match the query",
"format": "int32",
"example": 1
}
}
},
"SimpleUserResponse": {
"type": "object",
"description": "Basic User details",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
"format": "int32"
"description": "User Id",
"format": "int32",
"example": 1
},
"name": {
"type": "string"
"type": "string",
"description": "User's Display Name",
"example": "Name"
},
"email": {
"type": "string"
"type": "string",
"description": "User's Email",
"example": "[email protected]"
},
"userGroup": {
"$ref": "#/components/schemas/UserGroup"
"description": "User's User group relationship",
"example": "Barista",
"oneOf": [
{
"$ref": "#/components/schemas/UserGroup"
}
]
},
"state": {
"$ref": "#/components/schemas/UserState"
"description": "User's State",
"example": "Active",
"oneOf": [
{
"$ref": "#/components/schemas/UserState"
}
]
}
}
},
Expand Down

0 comments on commit 8c081e3

Please sign in to comment.