-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from ChrispyPeaches/islands-page
Islands page
- Loading branch information
Showing
12 changed files
with
653 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FocusAPI.Data; | ||
using FocusAPI.Models; | ||
using FocusCore.Commands.User; | ||
using FocusCore.Responses; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace FocusAPI.Methods.User; | ||
public class EditUserSelectedIsland | ||
{ | ||
public class Handler : IRequestHandler<EditUserSelectedIslandCommand, MediatrResult> | ||
{ | ||
FocusAPIContext _context; | ||
ILogger<Handler> _logger; | ||
public Handler(FocusAPIContext context, ILogger<Handler> logger) | ||
{ | ||
_context = context; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<MediatrResult> Handle(EditUserSelectedIslandCommand command, CancellationToken cancellationToken) | ||
{ | ||
Models.User? user = await _context.Users | ||
.FirstOrDefaultAsync(u => u.Id == command.UserId); | ||
|
||
if (user == null) | ||
return new MediatrResult | ||
{ | ||
HttpStatusCode = HttpStatusCode.InternalServerError, | ||
Message = "User not found" | ||
}; | ||
|
||
try | ||
{ | ||
Island island = await _context.Islands.Where(d => command.IslandId == d.Id).FirstOrDefaultAsync(cancellationToken); | ||
|
||
// Update the user's selected island | ||
user.SelectedIsland = island; | ||
user.SelectedIslandId = island.Id; | ||
|
||
await _context.SaveChangesAsync(); | ||
|
||
return new MediatrResult { HttpStatusCode = HttpStatusCode.OK }; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error saving changes to user selected island in database."); | ||
|
||
return new MediatrResult | ||
{ | ||
HttpStatusCode = HttpStatusCode.InternalServerError, | ||
Message = "Error saving changes to user selected island in database." | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/FocusApp.Client/Methods/User/EditUserSelectedIsland.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using FocusApp.Client.Clients; | ||
using FocusApp.Client.Helpers; | ||
using FocusApp.Shared.Data; | ||
using FocusApp.Shared.Models; | ||
using FocusCore.Commands.User; | ||
using FocusCore.Responses; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FocusApp.Methods.User; | ||
public class EditUserSelectedIsland | ||
{ | ||
public class Response | ||
{ | ||
public string Message { get; set; } | ||
public bool IsSuccessful { get; set; } | ||
} | ||
|
||
internal class Handler : IRequestHandler<EditUserSelectedIslandCommand, MediatrResult> | ||
{ | ||
FocusAppContext _localContext; | ||
ILogger<Handler> _logger; | ||
IAuthenticationService _authenticationService; | ||
IAPIClient _client; | ||
public Handler(FocusAppContext localContext, ILogger<Handler> logger, IAuthenticationService authenticationService, IAPIClient client) | ||
{ | ||
_localContext = localContext; | ||
_logger = logger; | ||
_authenticationService = authenticationService; | ||
_client = client; | ||
} | ||
|
||
public async Task<MediatrResult> Handle(EditUserSelectedIslandCommand command, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
// Update the user on the server | ||
await _client.EditUserSelectedIsland(command, cancellationToken); | ||
|
||
// Update the local database to reflect the changes made to the user | ||
Shared.Models.User? user = await _localContext.Users.FirstOrDefaultAsync(u => u.Id == command.UserId, cancellationToken); | ||
|
||
// Fetch island from local db if not null | ||
if (command.IslandId != null) | ||
{ | ||
Island? island = await _localContext.Islands.Where(i => command.IslandId == i.Id).FirstOrDefaultAsync(cancellationToken); | ||
|
||
// Update the user's selected island | ||
user.SelectedIsland = island; | ||
user.SelectedIslandId = island.Id; | ||
|
||
// Update the authentication service to reflect the new selected island | ||
_authenticationService.SelectedIsland = island; | ||
} | ||
|
||
await _localContext.SaveChangesAsync(cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error changing user selected island."); | ||
return new MediatrResult | ||
{ | ||
Message = "Error changing user selected island. Message: " + ex.Message, | ||
Success = false | ||
}; | ||
} | ||
|
||
return new MediatrResult | ||
{ | ||
Message = "User selected island changed successfully", | ||
Success = true | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using FocusApp.Client.Helpers; | ||
using FocusApp.Shared.Data; | ||
using FocusApp.Shared.Models; | ||
using FocusCore.Models; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace FocusApp.Client.Methods.User | ||
{ | ||
internal class GetUserIslands | ||
{ | ||
internal class Query : IRequest<Result> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid? selectedIslandId { get; set; } | ||
} | ||
|
||
internal class Result | ||
{ | ||
public List<IslandItem> Islands { get; set; } | ||
} | ||
|
||
internal class Handler : IRequestHandler<Query, Result> | ||
{ | ||
FocusAppContext _context; | ||
|
||
public Handler(FocusAppContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<Result> Handle( | ||
Query query, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
List<IslandItem> userIslands = new List<IslandItem>(); | ||
|
||
Guid userSelectedIslandId = Guid.Empty; | ||
if (query.selectedIslandId != null) | ||
{ | ||
userSelectedIslandId = query.selectedIslandId.Value; | ||
} | ||
|
||
userIslands = await _context.UserIslands | ||
.Include(i => i.Island) | ||
.Where(i => i.UserId == query.UserId) | ||
.Select(i => | ||
new IslandItem | ||
{ | ||
IslandId = i.IslandId, | ||
IslandName = i.Island.Name, | ||
IslandPicture = i.Island.Image, | ||
|
||
// Determine if island is currently selected | ||
isSelected = userSelectedIslandId == i.IslandId | ||
}) | ||
.ToListAsync(); | ||
|
||
return new Result | ||
{ | ||
Islands = userIslands | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.