Skip to content

Commit

Permalink
Merge pull request #116 from ChrispyPeaches/islands-page
Browse files Browse the repository at this point in the history
Islands page
  • Loading branch information
ChrispyPeaches authored Apr 26, 2024
2 parents c0fd76a + 0474b0d commit 14588a4
Show file tree
Hide file tree
Showing 12 changed files with 653 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/FocusAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,39 @@ public async Task<ActionResult> EditUserSelectedDecor(
}
}

[HttpPut]
[Route("EditUserSelectedIsland")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> EditUserSelectedIslands(
EditUserSelectedIslandCommand command,
CancellationToken cancellationToken = default)
{

MediatrResult result = new();
try
{
result = await _mediator.Send(command, cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "[500] Error editing user selected island.");
return StatusCode((int)HttpStatusCode.InternalServerError);
}

switch (result.HttpStatusCode)
{
case null:
_logger.LogError($"[500] {result.Message}");
return StatusCode((int)HttpStatusCode.InternalServerError);
case HttpStatusCode.OK:
return Ok();
default:
_logger.LogError($"[{(int)result.HttpStatusCode}] {result.Message}");
return StatusCode((int)result.HttpStatusCode);
}
}

[HttpPut]
[Route("EditUserSelectedBadge")]
[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down
65 changes: 65 additions & 0 deletions src/FocusAPI/Methods/User/EditUserSelectedIsland.cs
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."
};
}
}
}
}

7 changes: 7 additions & 0 deletions src/FocusApp.Client/AppShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public AppShell()
Route = "DecorPage"
},

new ShellContent()
{
Title = "IslandsPage",
ContentTemplate = new DataTemplate(typeof(IslandsPage)),
Route = "IslandsPage"
},

new ShellContent
{
Title = "FriendProfilePage",
Expand Down
5 changes: 5 additions & 0 deletions src/FocusApp.Client/Clients/IAPIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ Task EditUserSelectedDecor(
EditUserSelectedDecorCommand command,
CancellationToken cancellationToken = default);

[Put("/User/EditUserSelectedIsland")]
Task EditUserSelectedIsland(
EditUserSelectedIslandCommand command,
CancellationToken cancellationToken = default);

[Put("/User/EditUserSelectedBadge")]
Task EditUserSelectedBadge(
EditUserSelectedBadgeCommand command,
Expand Down
76 changes: 76 additions & 0 deletions src/FocusApp.Client/Methods/User/EditUserSelectedIsland.cs
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
};
}
}
}
66 changes: 66 additions & 0 deletions src/FocusApp.Client/Methods/User/GetUserIslands.cs
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
};
}
}
}
}
7 changes: 5 additions & 2 deletions src/FocusApp.Client/Views/Social/DecorPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using FocusApp.Methods.User;
using FocusApp.Client.Resources;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Extensions.Logging;

namespace FocusApp.Client.Views.Social;

Expand All @@ -28,18 +29,20 @@ internal sealed class DecorPage : BasePage
private readonly PopupService _popupService;
private readonly FocusAppContext _localContext;
private readonly IMediator _mediator;
private readonly ILogger<DecorPage> _logger;

FlexLayout _decorContainer;
Image _selectedCheckmark;
Label _responseMessage;

public DecorPage(IAPIClient client, IAuthenticationService authenticationService, PopupService popupService, FocusAppContext localContext, IMediator mediator)
public DecorPage(IAPIClient client, IAuthenticationService authenticationService, PopupService popupService, FocusAppContext localContext, IMediator mediator, ILogger<DecorPage> logger)
{
_client = client;
_authenticationService = authenticationService;
_popupService = popupService;
_localContext = localContext;
_mediator = mediator;
_logger = logger;

// Instantiate container for decor
_decorContainer = new FlexLayout
Expand Down Expand Up @@ -171,7 +174,7 @@ private async Task<List<DecorItem>> FetchDecorFromLocalDb()
}
catch (Exception ex)
{
throw new Exception("Error when fetching decor from local DB.", ex);
_logger.LogError(ex, "Error occurred when fetching decor from local DB.");
}

return userDecor;
Expand Down
Loading

0 comments on commit 14588a4

Please sign in to comment.