Skip to content

Commit

Permalink
Merge pull request #1021 from ita-social-projects/develop
Browse files Browse the repository at this point in the history
Update master
  • Loading branch information
BohdanBybliv authored Jan 7, 2024
2 parents 8490f60 + 1b2ca89 commit b401390
Show file tree
Hide file tree
Showing 38 changed files with 304 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public async Task<Result<SubtitleDTO>> Handle(GetSubtitleByIdQuery request, Canc
{
string errorMsg = _stringLocalizerCannotFind["CannotFindSubtitleWithCorrespondingId", request.Id].Value;
_logger.LogError(request, errorMsg);
return Result.Fail(new Error(errorMsg));
var returner = Result.Fail(new Error(errorMsg));
return returner;
}

return Result.Ok(_mapper.Map<SubtitleDTO>(subtitle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<Result<IEnumerable<StreetcodeTagDTO>>> Handle(GetTagByStreetco
t => t.StreetcodeId == request.StreetcodeId,
include: q => q.Include(t => t.Tag));

if (tagIndexed is null)
if (tagIndexed is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind?["CannotFindAnyTagByTheStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<Result<IEnumerable<ArtDTO>>> Handle(GetArtsByStreetcodeIdQuery
include: scl => scl
.Include(sc => sc.Image) !);

if (arts is null)
if (arts is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnyArtWithCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<Result<IEnumerable<ImageDTO>>> Handle(GetImageByStreetcodeIdQu
f => f.Streetcodes.Any(s => s.Id == request.StreetcodeId),
include: q => q.Include(img => img.ImageDetails))).OrderBy(img => img.ImageDetails?.Alt);

if (images is null || images.Count() == 0)
if (images is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnImageWithTheCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<Result<IEnumerable<StreetcodeArtDTO>>> Handle(GetStreetcodeArt
.Include(a => a.Art)
.Include(i => i.Art.Image) !);

if (art is null)
if (art is null || request.StreetcodeId < 1)
{
string errorMsg = _stringLocalizerCannotFind["CannotFindAnyArtWithCorrespondingStreetcodeId", request.StreetcodeId].Value;
_logger.LogError(request, errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public string SaveFileInStorage(string base64, string name, string extension)

public void SaveFileInStorageBase64(string base64, string name, string extension)
{
byte[] imageBytes = Convert.FromBase64String(base64);
byte[] imageBytes = Convert.FromBase64String(base64.Trim());
Directory.CreateDirectory(_blobPath);
EncryptFile(imageBytes, extension, name);
}
Expand All @@ -87,26 +87,6 @@ public string UpdateFileInStorage(
return hashBlobStorageName;
}

public async Task CleanBlobStorage()
{
var base64Files = GetAllBlobNames();

var existingImagesInDatabase = await _repositoryWrapper.ImageRepository.GetAllAsync();
var existingAudiosInDatabase = await _repositoryWrapper.AudioRepository.GetAllAsync();

List<string> existingMedia = new ();
existingMedia.AddRange(existingImagesInDatabase.Select(img => img.BlobName));
existingMedia.AddRange(existingAudiosInDatabase.Select(img => img.BlobName));

var filesToRemove = base64Files.Except(existingMedia).ToList();

foreach (var file in filesToRemove)
{
Console.WriteLine($"Deleting {file}...");
DeleteFileInStorage(file);
}
}

private IEnumerable<string> GetAllBlobNames()
{
var paths = Directory.EnumerateFiles(_blobPath);
Expand Down
69 changes: 37 additions & 32 deletions Streetcode/Streetcode.BLL/Services/CacheService/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Streetcode.BLL.Interfaces.Cache;
using Streetcode.BLL.Interfaces.Logging;

Expand All @@ -14,54 +15,58 @@ namespace Streetcode.BLL.Services.CacheService
public class CacheService : ICacheService
{
private readonly IMemoryCache _cache;
private readonly ILoggerService _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ConcurrentDictionary<object, SemaphoreSlim> _locks = new ConcurrentDictionary<object, SemaphoreSlim>();

public CacheService(IMemoryCache cache, ILoggerService logger)
public CacheService(IMemoryCache cache, IServiceScopeFactory serviceScopeFactory )
{
_cache = cache;
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;
}

public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> getItemCallback, TimeSpan cacheDuration)
{
_logger.LogInformation(key + "GetOrSetAsync function start!");
if (_cache.TryGetValue(key, out T cachedItem))
using (var scope = _serviceScopeFactory.CreateAsyncScope())
{
_logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_logger.LogInformation(key + "TryGetValue function false");
SemaphoreSlim mylock = _locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));

await mylock.WaitAsync();

try
{
if (_cache.TryGetValue(key, out cachedItem))
var logger = scope.ServiceProvider.GetRequiredService<ILoggerService>();
logger.LogInformation(key + "GetOrSetAsync function start!");
if (_cache.TryGetValue(key, out T cachedItem))
{
_logger.LogInformation(key + "TryGetValue function true");
logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_logger.LogInformation(key + "TryGetValue function false");
T item = await getItemCallback();
logger.LogInformation(key + "TryGetValue function false");
SemaphoreSlim mylock = _locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));

await mylock.WaitAsync();

var cacheEntryOptions = new MemoryCacheEntryOptions
try
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
SlidingExpiration = cacheDuration,
Priority = CacheItemPriority.Normal,
};
if (_cache.TryGetValue(key, out cachedItem))
{
logger.LogInformation(key + "TryGetValue function true");
return cachedItem;
}

_cache.Set(key, item, cacheEntryOptions);
_logger.LogInformation(key + "Set function true");
return item;
}
finally
{
mylock.Release();
logger.LogInformation(key + "TryGetValue function false");
T item = await getItemCallback();

var cacheEntryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
SlidingExpiration = cacheDuration,
Priority = CacheItemPriority.Normal,
};

_cache.Set(key, item, cacheEntryOptions);
logger.LogInformation(key + "Set function true");
return item;
}
finally
{
mylock.Release();
}
}
}

Expand Down
39 changes: 0 additions & 39 deletions Streetcode/Streetcode.BLL/Util/DateToStringConverter.cs

This file was deleted.

23 changes: 0 additions & 23 deletions Streetcode/Streetcode.BLL/Util/IdComparer.cs

This file was deleted.

10 changes: 5 additions & 5 deletions Streetcode/Streetcode.DAL/Streetcode.DAL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
<ItemGroup>
<PackageReference Include="EfCore.SchemaCompare" Version="6.0.0" />
<PackageReference Include="MailKit" Version="3.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.11">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
12 changes: 0 additions & 12 deletions Streetcode/Streetcode.WebApi/Attributes/AuthorizeRoles.cs

This file was deleted.

20 changes: 3 additions & 17 deletions Streetcode/Streetcode.WebApi/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Resources;
using FluentResults;
using FluentResults;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Streetcode.BLL.MediatR.ResultVariations;

namespace Streetcode.WebApi.Controllers;
Expand All @@ -11,12 +9,7 @@ namespace Streetcode.WebApi.Controllers;
[Route("api/[controller]/[action]")]
public class BaseApiController : ControllerBase
{
private readonly IStringLocalizer _stringLocalizer;
private IMediator? _mediator;
public BaseApiController(IStringLocalizer<BaseApiController> stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}

public BaseApiController()
{
Expand All @@ -28,21 +21,14 @@ protected ActionResult HandleResult<T>(Result<T> result)
{
if (result.IsSuccess)
{
if(result is NullResult<T>)
if (result is NullResult<T>)
{
return Ok(result.Value);
}

return (result.Value is null) ?
NotFound(_stringLocalizer?["NotFound"].Value) : Ok(result.Value);
}

foreach (var item in result.Reasons)
{
if (item.Message.Contains(_stringLocalizer?["NotFound"].Value))
{
return Ok();
}
NotFound("Not Found") : Ok(result.Value);
}

return BadRequest(result.Reasons);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Streetcode.BLL.MediatR.Media.Audio.Update;
using Streetcode.BLL.MediatR.Media.Image.GetByStreetcodeId;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;

namespace Streetcode.WebApi.Controllers.Media;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<IActionResult> GetById([FromRoute] int id)
}

[HttpGet("{streetcodeId:int}")]
public async Task<IActionResult> GetAllByStreetcodeId([FromRoute] int streetcodeId)
public async Task<IActionResult> GetArtsByStreetcodeId([FromRoute] int streetcodeId)
{
return HandleResult(await Mediator.Send(new GetArtsByStreetcodeIdQuery(streetcodeId)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Streetcode.BLL.MediatR.Media.Image.Create;
using Streetcode.BLL.MediatR.Media.Image.Delete;
using Streetcode.BLL.MediatR.Media.Image.Update;
using Streetcode.WebApi.Attributes;
using Streetcode.DAL.Enums;
using Microsoft.Net.Http.Headers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Streetcode.BLL.MediatR.Partners.GetByStreetcodeIdToUpdate;
using Streetcode.DAL.Entities.Partners;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;

namespace Streetcode.WebApi.Controllers.Partners;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Streetcode.BLL.MediatR.Sources.SourceLink.Update;
using Streetcode.BLL.MediatR.Sources.SourceLink.Delete;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;
using Streetcode.BLL.MediatR.Sources.SourceLinkCategory.GetAll;
using Streetcode.BLL.MediatR.Sources.SourceLinkCategory.GetCategoryContentByStreetcodeId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Streetcode.BLL.MediatR.Streetcode.Fact.GetByStreetcodeId;
using Streetcode.BLL.MediatR.Streetcode.Fact.Update;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;

namespace Streetcode.WebApi.Controllers.Streetcode.TextContent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Streetcode.BLL.MediatR.Team.GetByRoleId;
using Streetcode.BLL.MediatR.Team.Update;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;

namespace Streetcode.WebApi.Controllers.Team
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Streetcode.BLL.MediatR.Users.Login;
using Streetcode.BLL.MediatR.Users.RefreshToken;
using Streetcode.DAL.Enums;
using Streetcode.WebApi.Attributes;

namespace Streetcode.WebApi.Controllers.Users
{
Expand Down
Loading

0 comments on commit b401390

Please sign in to comment.