Skip to content

Commit

Permalink
Use Automapper in CooperationChoices handling (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkikos authored Oct 21, 2024
1 parent de34053 commit a58ffc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
17 changes: 15 additions & 2 deletions aspnetcore/src/api/Automapper/MappingProfile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using api.Models.ProfileEditor.Items;
using api.Models.Elasticsearch;
using api.Models.ProfileEditor.Items;
using api.Models.Elasticsearch;
using AutoMapper;
using api.Models.Ttv;
using api.Models.ProfileEditor;
using System.Linq;

public class MappingProfile : Profile
{
Expand Down Expand Up @@ -31,5 +34,15 @@ public MappingProfile()
CreateMap<ProfileEditorActor, ElasticsearchActor>();
CreateMap<ProfileEditorPreferredIdentifier, ElasticsearchPreferredIdentifier>();
CreateMap<ProfileEditorActivityAndReward, ElasticsearchActivityAndReward>();
CreateMap<DimReferencedatum, ProfileEditorCooperationItem>()
.ForMember(dst => dst.Id, opt => opt.MapFrom(src => src.DimUserChoices.First().Id))
.ForMember(dst => dst.Selected, opt => opt.MapFrom(src => src.DimUserChoices.First().UserChoiceValue));

CreateMap<DimUserChoice, ElasticsearchCooperation>()
.ForMember(dst => dst.Id, opt => opt.MapFrom(src => src.DimReferencedataIdAsUserChoiceLabelNavigation.Id))
.ForMember(dst => dst.NameFi, opt => opt.MapFrom(src => src.DimReferencedataIdAsUserChoiceLabelNavigation.NameFi))
.ForMember(dst => dst.NameEn, opt => opt.MapFrom(src => src.DimReferencedataIdAsUserChoiceLabelNavigation.NameEn))
.ForMember(dst => dst.NameSv, opt => opt.MapFrom(src => src.DimReferencedataIdAsUserChoiceLabelNavigation.NameSv))
.ForMember(dst => dst.Order, opt => opt.MapFrom(src => src.DimReferencedataIdAsUserChoiceLabelNavigation.Order));
}
}
25 changes: 6 additions & 19 deletions aspnetcore/src/api/Background/BackgroundProfileData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading.Tasks;
using api.Models.Log;
using System.Threading.Tasks;
using api.Models.Log;
using api.Models.Ttv;
using api.Models.Elasticsearch;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using AutoMapper.QueryableExtensions;

namespace api.Services
{
Expand Down Expand Up @@ -52,24 +53,10 @@ await localUserProfileService.GetProfileDataAsync(
elasticsearchPerson.id = orcidId;

// Add cooperation choices
List<DimUserChoice> userChoices =
List<ElasticsearchCooperation> userChoices =
await localTtvContext.DimUserChoices.TagWith("Get user choices for Elasticsearch")
.Where(duc => duc.DimUserProfileId == userprofileId && duc.UserChoiceValue == true)
.Include(duc => duc.DimReferencedataIdAsUserChoiceLabelNavigation).AsNoTracking().ToListAsync();

foreach (DimUserChoice uc in userChoices)
{
elasticsearchPerson.cooperation.Add(
new ElasticsearchCooperation()
{
Id = uc.DimReferencedataIdAsUserChoiceLabelNavigation.Id,
NameFi = uc.DimReferencedataIdAsUserChoiceLabelNavigation.NameFi,
NameEn = uc.DimReferencedataIdAsUserChoiceLabelNavigation.NameEn,
NameSv = uc.DimReferencedataIdAsUserChoiceLabelNavigation.NameSv,
Order = uc.DimReferencedataIdAsUserChoiceLabelNavigation.Order
}
);
}
.Where(duc => duc.DimUserProfileId == userprofileId && duc.UserChoiceValue == true).AsNoTracking().ProjectTo<ElasticsearchCooperation>(mapper.ConfigurationProvider).ToListAsync();
elasticsearchPerson.cooperation.AddRange(userChoices);

return elasticsearchPerson;
}
Expand Down
27 changes: 6 additions & 21 deletions aspnetcore/src/api/Controllers/CooperationChoicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using System;
using AutoMapper;

namespace api.Controllers
{
Expand All @@ -28,15 +29,17 @@ public class CooperationChoicesController : TtvControllerBase
private readonly ITtvSqlService _ttvSqlService;
private readonly IUtilityService _utilityService;
private readonly IMemoryCache _cache;
private readonly IMapper _mapper;

public CooperationChoicesController(TtvContext ttvContext, IUserProfileService userProfileService,
ITtvSqlService ttvSqlService, IUtilityService utilityService, IMemoryCache memoryCache)
ITtvSqlService ttvSqlService, IUtilityService utilityService, IMemoryCache memoryCache, IMapper mapper)
{
_ttvContext = ttvContext;
_userProfileService = userProfileService;
_ttvSqlService = ttvSqlService;
_utilityService = utilityService;
_cache = memoryCache;
_mapper = mapper;
}

/// <summary>
Expand Down Expand Up @@ -90,31 +93,13 @@ public async Task<IActionResult> Get()
}
await _ttvContext.SaveChangesAsync();


// Collect data for API response.
List<ProfileEditorCooperationItem> cooperationItems = new();
foreach (DimReferencedatum dimReferenceDataUserChoice in dimReferenceDataUserChoices)
{
DimUserChoice dimUserChoice = dimReferenceDataUserChoice.DimUserChoices.First();
cooperationItems.Add(
new ProfileEditorCooperationItem()
{
Id = dimUserChoice.Id,
NameFi = dimUserChoice.DimReferencedataIdAsUserChoiceLabelNavigation.NameFi,
NameEn = dimUserChoice.DimReferencedataIdAsUserChoiceLabelNavigation.NameEn,
NameSv = dimUserChoice.DimReferencedataIdAsUserChoiceLabelNavigation.NameSv,
Selected = dimUserChoice.UserChoiceValue,
Order = dimUserChoice.DimReferencedataIdAsUserChoiceLabelNavigation.Order
}
);
}
// Map to API response.
List<ProfileEditorCooperationItem> cooperationItems = _mapper.Map<List<ProfileEditorCooperationItem>>(dimReferenceDataUserChoices);

// Save response in cache
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromSeconds(Constants.Cache.MEMORY_CACHE_EXPIRATION_SECONDS));

// Save data in cache. Cache key is ORCID ID.
_cache.Set(cacheKey, cooperationItems, cacheEntryOptions);

return Ok(new ApiResponseCooperationGet(success: true, reason: "", data: cooperationItems, fromCache: false));
Expand Down

0 comments on commit a58ffc6

Please sign in to comment.