Skip to content

Commit

Permalink
Let's build
Browse files Browse the repository at this point in the history
  • Loading branch information
Webreaper committed Sep 4, 2022
1 parent 9c220d9 commit 6af77fc
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 28 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Damselfly.Core.Constants/ConfigSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ConfigSettings
public const bool DefaultEnableRolesAndAuth = true;

public const string ThumbSize = "ThumbSize";
public const string RecentTags = "RecentTags";
public const string Theme = "Theme";
public const string SimilarityThreshold = "SimilarityThreshold";
public const string ZoomLevel = "ZoomLevel";
Expand Down
3 changes: 3 additions & 0 deletions Damselfly.Core.DbModels/Interfaces/IBasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ public interface IUserBasketService : IBasketService
Task<int> CopyImages(int destBasketId);
bool IsSelected(Image image);
Task Clear();
Task<Basket> Create(string name);
Task<ICollection<Basket>> GetUserBaskets();
Task<Basket> SwitchToDefaultBasket();
}
4 changes: 2 additions & 2 deletions Damselfly.Core.DbModels/Interfaces/IConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Damselfly.Core.ScopedServices.Interfaces
{
public interface IConfigService
{
void Set(string name, string valuel);
void Set(string name, string value);
string Get(string name, string defaultIfNotExists = null);
EnumType Get<EnumType>(string name, EnumType defaultIfNotExists = default) where EnumType : struct;
bool GetBool(string name, bool defaultIfNotExists = default);
int GetInt(string name, int defaultIfNotExists = default);
Task<List<ConfigSetting>> GetAllSettings();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public class ClientBasketService : IUserBasketService, IBasketService
private readonly RestClient httpClient;
private readonly IImageCacheService _imageCache;
private readonly NotificationsService _notifications;
private readonly IUserService _userService;

public ClientBasketService(RestClient client, NotificationsService notifications, IImageCacheService imageCache, ILogger<ClientBasketService> logger)
public ClientBasketService(RestClient client, NotificationsService notifications,
IImageCacheService imageCache,
IUserService userService,
ILogger<ClientBasketService> logger)
{
httpClient = client;
_userService = userService;
_imageCache = imageCache;
_notifications = notifications;
_logger = logger;
Expand All @@ -35,9 +40,18 @@ private async Task HandleServerBasketChange(BasketChanged change)
{
if( CurrentBasket.BasketId == change.BasketId )
{
var basket = await GetBasketById(change.BasketId);
Basket newBasket;

await SetCurrentBasket(basket);
if (change.ChangeType == BasketChangeType.BasketDeleted)
{
newBasket = await GetDefaultBasket( _userService.UserId );
}
else
{
newBasket = await GetBasketById(change.BasketId);
}

await SetCurrentBasket(newBasket);
}
}

Expand Down Expand Up @@ -92,9 +106,19 @@ public async Task<Basket> GetBasketById(int basketId)

public async Task<Basket> SwitchToBasket(int basketId)
{
var newBasket = await GetBasketById(basketId);
await SetCurrentBasket(newBasket);
return newBasket;
try
{
var newBasket = await GetBasketById(basketId);

await SetCurrentBasket(newBasket);

return newBasket;
}
catch ( Exception ex )
{
_logger.LogError($"Attempted to switch to unknown basket ID {basketId}");
throw;
}
}

public async Task<Basket> SwitchToDefaultBasket(int? userId)
Expand Down Expand Up @@ -123,6 +147,11 @@ public async Task SetImageBasketState( int basketId, bool newState, ICollection<
// We don't notify the state changed here - it'll be notified from the server
}

public async Task<Basket> Create(string name )
{
return await Create(name, _userService.UserId);
}

public async Task<Basket> Create(string name, int? userId)
{
var payload = new BasketCreateRequest { Name = name, UserId = userId };
Expand All @@ -134,9 +163,19 @@ public async Task Save(Basket basket)
var response = await httpClient.CustomPutAsJsonAsync<Basket>($"/api/basket", basket);
}

public async Task<Basket> GetDefaultBasket()
{
return await GetDefaultBasket(_userService.UserId);
}

public async Task<Basket> GetDefaultBasket(int? userId)
{
return await httpClient.CustomGetFromJsonAsync<Basket>($"/api/baskets/{userId}");
return await httpClient.CustomGetFromJsonAsync<Basket>($"/api/baskets/{_userService.UserId}");
}

public async Task<ICollection<Basket>> GetUserBaskets()
{
return await GetUserBaskets(_userService.UserId);
}

public async Task<ICollection<Basket>> GetUserBaskets(int? userId)
Expand Down Expand Up @@ -168,5 +207,10 @@ public bool IsSelected(Image image)
{
return BasketImages.Any(x => x.ImageId == image.ImageId);
}

public async Task<Basket> SwitchToDefaultBasket()
{
return await SwitchToDefaultBasket(_userService.UserId);
}
}

16 changes: 12 additions & 4 deletions Damselfly.Core/ScopedServices/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,18 @@ public async Task<Basket> GetDefaultBasket( int? userId )
using var scope = _scopeFactory.CreateScope();
using var db = scope.ServiceProvider.GetService<ImageContext>();

// Get the first default basket
defaultBasket = db.Baskets
.Include( x => x.BasketEntries )
.FirstOrDefault(x => x.Name == s_DefaultBasket );
// Get the first shared
defaultBasket = await db.Baskets
.Include(x => x.BasketEntries)
.FirstOrDefaultAsync(x => x.UserId == null);

if (defaultBasket == null)
{
// Get the first default basket
defaultBasket = await db.Baskets
.Include(x => x.BasketEntries)
.FirstOrDefaultAsync(x => x.Name == s_DefaultBasket);
}

if (defaultBasket == null)
{
Expand Down
5 changes: 3 additions & 2 deletions Damselfly.Core/ScopedServices/UserTagFavouritesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Damselfly.Core.ScopedServices.Interfaces;
using System.Threading.Tasks;
using Damselfly.Core.Interfaces;
using Damselfly.Core.Constants;

namespace Damselfly.Core.ScopedServices;

Expand All @@ -29,7 +30,7 @@ public UserTagRecentsService(ExifService exifService, IConfigService configServi

_exifService.OnUserTagsAdded += AddRecentTags;

string recents = configService.Get("RecentTags");
string recents = configService.Get(ConfigSettings.RecentTags);

if( ! string.IsNullOrEmpty( recents ) )
{
Expand Down Expand Up @@ -59,7 +60,7 @@ private async void AddRecentTags(ICollection<string> newRecents)
recentTags.Clear();
recentTags.AddRange(newRecent);

_configService.Set("RecentTags", string.Join(",", recentTags));
_configService.Set(ConfigSettings.RecentTags, string.Join(",", recentTags));
NotifyRecentsChanged();
}

Expand Down
11 changes: 9 additions & 2 deletions Damselfly.Web.Client/Shared/BasketManager.razor
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@

await LoadBasketList();

// Restore the last-known basket
var selectedBasketId = userConfigService.GetInt(ConfigSettings.SelectedBasketId );

if (selectedBasketId > 0 && basketService.CurrentBasket.BasketId != selectedBasketId )
await basketService.SwitchToBasket(selectedBasketId);
if( baskets.Any( x => x.BasketId == selectedBasketId ))
{
if (selectedBasketId > 0 && basketService.CurrentBasket.BasketId != selectedBasketId )
await basketService.SwitchToBasket(selectedBasketId);
}
else
await basketService.SwitchToDefaultBasket();


StateHasChanged();
}
Expand Down
5 changes: 3 additions & 2 deletions Damselfly.Web.Client/Shared/ScrollMonitor.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@using Damselfly.Core.DbModels
@using Microsoft.AspNetCore.Identity
@using Damselfly.Core.ScopedServices

@inject IConfigService configService
@inject IJSRuntime JsRuntime
@inject ILogger<ScrollMonitor> logger

@code{
[Parameter]
Expand All @@ -19,8 +21,7 @@

private void SaveScrollState(int scrollTop)
{
// WASM: Fix logging
// Logging.LogVerbose($"Saving scroll position to {scrollTop}");
logger.LogTrace($"Saving scroll position to {scrollTop}");
configService.Set(ScrollConfigName, scrollTop.ToString());

OnScrollPositionChanged?.Invoke(scrollTop);
Expand Down
3 changes: 3 additions & 0 deletions Damselfly.Web.Server/Controllers/BasketController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public async Task<Basket> GetBasketById(int basketId)
{
var basket = await _service.GetBasketById(basketId);

if (basket == null)
throw new ArgumentException("No such basket!");

foreach (var be in basket.BasketEntries)
be.Image = null;

Expand Down
13 changes: 13 additions & 0 deletions Damselfly.Web.Server/Controllers/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public async Task<List<ConfigSetting>> GetAll()

return settings;
}

[HttpGet("/api/config/user/{userId}")]
public async Task<ICollection<ConfigSetting>> GetAllUserSettings(int userId)
{
var settings = new List<ConfigSetting>();

var allValues = await _configService.GetAllUserSettings( userId );
if (allValues != null)
settings.AddRange(allValues);

return settings;
}

[HttpGet("/api/config/{name}")]
public ConfigSetting Get( string name )
{
Expand Down
11 changes: 2 additions & 9 deletions scripts/makeserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ esac
serverdist="${PWD}/server"
zipname="${serverdist}/damselfly-server-${PLATFORM}-${version}.zip"

echo "*** Building Server for ${PLATFORM} with runtime ${runtime} into ${zipname}"

# /p:PublishTrimmed=true /p:EnableCompressionInSingleFile= /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:EnableCompressionInSingleFile= /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true
dotnet publish Damselfly.Web -r $runtime -f net${dotnetversion} -c Release --self-contained true /p:Version=$version

echo "*** Building WASM Server for ${PLATFORM} with runtime ${runtime}"
echo "*** Building Damselfly for ${PLATFORM} with runtime ${runtime}"

dotnet publish Damselfly.Web.Server -r $runtime -f net${dotnetversion} -c Release --self-contained true /p:Version=$version

outputdir="Damselfly.Web/bin/Release/net${dotnetversion}/${runtime}/publish"

# echo "*** Copying output..."
# wasmOutputdir="Damselfly.Web.Server/bin/Release/net${dotnetversion}/${runtime}/publish"
# cp -vnpr $wasmOutputdir/* $outputdir
outputdir="Damselfly.Web.Server/bin/Release/net${dotnetversion}/${runtime}/publish"

# Hack to get the libcvextern.so into the linux build.
case $PLATFORM in
Expand Down

0 comments on commit 6af77fc

Please sign in to comment.