Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 50 additions & 45 deletions Controllers/EditionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using vopen_api.Models;
using vopen_api.Repositories;

Expand All @@ -14,80 +16,83 @@ public class EditionsController : ControllerBase
private readonly EditionsRepository editionsRepository;
private readonly LegacyGlobalRepository legacyGlobalRepository;

public EditionsController(EditionsRepository editionsRepository, LegacyGlobalRepository legacyGlobalRepository)
private readonly ILogger logger;
private readonly CacheService cacheService;

public EditionsController(EditionsRepository editionsRepository, LegacyGlobalRepository legacyGlobalRepository, ILogger<LegaciesController> logger, IMemoryCache memoryCache)
{
this.editionsRepository = editionsRepository;
this.legacyGlobalRepository = legacyGlobalRepository;

this.logger = logger;
this.cacheService = new CacheService(memoryCache);
}

// GET api/v1/editions
[HttpGet]
public async Task<IEnumerable<EditionDTO>> GetAllAsync()
{
var language = this.GetLanguage();
return await this.editionsRepository.GetAllByLanguage(language);
var cacheKey = $"Editions-GetByIdAsync";
var cacheResult = this.cacheService.GetValue<IEnumerable<EditionDTO>>(cacheKey);

if (cacheResult != null)
{
this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache");
return cacheResult;
}

cacheResult = await this.editionsRepository.GetAllByLanguage(language);
this.cacheService.SetValue(cacheKey, cacheResult);

return cacheResult;
}

// GET api/v1/editions/5
[HttpGet("{id}")]
public async Task<IActionResult> GetByIdAsync(string id)
{

if (id == "vopen-global-legacy")
{
return Ok(this.legacyGlobalRepository.GetEdition(id));
{
return Ok(GetEditionFromLegacy(id));
}

var language = this.GetLanguage();
var language = this.GetLanguage();
var cacheKey = $"Editions-GetByIdAsync-{id}-{language}";
var cacheResult = this.cacheService.GetValue<EditionDTO>(cacheKey);

if (cacheResult != null) {
this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache");
return Ok(cacheResult);
}

var result = await this.editionsRepository.GetByLanguageAndId(language, id);

if (result == null)
{
return NotFound();
}

this.cacheService.SetValue(cacheKey, result);
return Ok(result);
}

// POST api/v1/edition
//[HttpPost]
//public async Task<IActionResult> Post([FromBody] EditionDTO newEdition)
//{
// var result = await this.editionsRepository.Create(newEdition);

// if (result == null)
// {
// return null;
// }

// return CreatedAtAction(nameof(GetByIdAsync), new { id = newEdition.Id }, newEdition);
//}

//// PUT api/v1/edition/5
//[HttpPut("{id}")]
//public async Task<IActionResult> Put(String id, [FromBody] EditionDTO updatedEdition)
//{
// if (id != updatedEdition.Id)
// {
// return BadRequest();
// }

// var result = await this.editionsRepository.Update(updatedEdition);

// if (result == null)
// {
// return NotFound();
// }

// return Ok(result);
//}

//// DELETE api/v1/event/5
//[HttpDelete("{id}")]
//public async Task<IActionResult> Delete(string id)
//{
// await this.editionsRepository.Delete(id);
// return NoContent();
//}
private EditionDTO GetEditionFromLegacy(string id) {

var cacheKey = $"Editions-GetByIdAsync-legacy-{id}";
var cacheResult = this.cacheService.GetValue<EditionDTO>(cacheKey);

if (cacheResult != null)
{
this.logger.LogInformation($"Retrieving data with key '{cacheKey}' from cache");
return cacheResult;
}

cacheResult = this.legacyGlobalRepository.GetEdition(id);
this.cacheService.SetValue(cacheKey, cacheResult);
return cacheResult;
}

private string GetLanguage()
{
Expand Down