gehgassi_backend/gehGassi.Web/Controllers/Api/ApiListingsController.cs

249 lines
12 KiB
C#

using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Dto.News;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using gehGassi.Dto.Common;
using gehGassi.Dto;
using gehGassi.Dto.Listings;
using gehGassi.Web.Models;
using System.Collections.Generic;
using System.Linq;
using gehGassi.Core.Services;
using gehGassi.Dto.Advertisements;
using Asp.Versioning;
namespace gehGassi.Web.Controllers.Api
{
/// <summary>
/// Controller der Zugriff auf Listungen via API ermöglicht
/// </summary>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[ApiVersion(1)]
[Route("api/listings")]
[Route("api/v{v:apiVersion}/listings")]
public class ApiListingsController : ApiBaseController
{
private readonly ILogger<ApiListingsController> _logger;
private readonly IGeoLocationService _geoLocationService;
private readonly IListingService _listingService;
private readonly IBranchService _branchService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="logger">Instanz eines ILogger</param>
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
/// <param name="appUserService">Instanz eines IDogOwnerService</param>
/// <param name="geoLocationService">Instanz eines IGeoLocationService</param>
/// <param name="listingService">Instanz eines IListingService</param>
/// <param name="branchService">Instanz eines IBranchService</param>
public ApiListingsController(IMapper mapper, ILogger<ApiListingsController> logger, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService,
IGeoLocationService geoLocationService, IListingService listingService, IBranchService branchService) : base(mapper, localizationOptions, appUserService)
{
_logger = logger;
_geoLocationService = geoLocationService;
_listingService = listingService;
_branchService = branchService;
}
/// <summary>
/// Gibt eine Liste von Branchen welche online sind mit x Listungen zurück.
/// Dabei wird je Branche auf den Listungstyp für die Sortierung rücksicht genommen
/// </summary>
/// <param name="model">Abfragemodel</param>
/// <returns>HTTP 200 wenn erfolgreich</returns>
[Route("GetOverview")]
[HttpPost]
public async Task<IActionResult> GetOverview(QueryDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var response = new ListResponseDto<BranchWithListingsDto>();
//Geodaten holen.
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
var (country, state) = await GetGeoDataAsync(model);
//Schritt 1: Branchen holen...
var branches = await _branchService.GetOnlineAsync(model.Language, LocalizationOptions.Value.DefaultCulture);
var dtoBranchesList = Mapper.Map<List<BranchWithListingsDto>>(branches);
//Schritt 2: für alle Branchen die Listungen holen. Dabei wird Skip / Take berücksichtigt
foreach (var branchDto in dtoBranchesList)
{
var listings = await _listingService.GetByBranchForAppAsync(branchDto.Id, string.Empty, string.Empty, country, state, model.Location.Lat.Value, model.Location.Lng.Value, model.Language, LocalizationOptions.Value.DefaultCulture, model.Skip, model.Take);
var dtoListings = Mapper.Map<List<ListingDto>>(listings.list);
branchDto.Listings = dtoListings;
}
//Jene Branchen mit keinen einträgen entfernen
dtoBranchesList.RemoveAll(c => c.Listings.Count == 0);
response.Total = dtoBranchesList.Sum(c => c.Listings.Count);
var baseAddress = GetBaseAddress();
foreach (var branchDto in dtoBranchesList)
{
foreach (var branchDtoListing in branchDto.Listings)
{
if (!string.IsNullOrWhiteSpace(branchDtoListing.Image))
branchDtoListing.Image = $"{baseAddress}/file/documents/thumbnails/{200}/{branchDtoListing.Image}";
if (!string.IsNullOrWhiteSpace(branchDtoListing.Image2))
branchDtoListing.Image2 = $"{baseAddress}/file/documents/thumbnails/{200}/{branchDtoListing.Image2}";
if (!string.IsNullOrWhiteSpace(branchDtoListing.ImageLanguage))
branchDtoListing.ImageLanguage = $"{baseAddress}/file/documents/thumbnails/{200}/{branchDtoListing.ImageLanguage}";
if (!string.IsNullOrWhiteSpace(branchDtoListing.Image2Language))
branchDtoListing.Image2Language = $"{baseAddress}/file/documents/thumbnails/{200}/{branchDtoListing.Image2Language}";
}
}
response.List = dtoBranchesList;
response.Take = model.Take;
response.Skip = model.Skip;
return Ok(response);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
/// <summary>
/// Gibt eine Liste von Listungen in der Nähe zurück
/// </summary>
/// <param name="model">Abfragemodel</param>
/// <returns>HTTP 200 wenn erfolgreich</returns>
[Route("GetNearListings")]
[HttpPost]
public async Task<IActionResult> GetNearListings(QueryDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var response = new ListResponseDto<ListingDto>();
//Geodaten holen.
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
var (country, state) = await GetGeoDataAsync(model);
var queryResult = await _listingService.GetNearForAppAsync(country, state, model.Location.Lat.Value, model.Location.Lng.Value, model.Language, LocalizationOptions.Value.DefaultCulture, model.Skip, model.Take);
response.Total = queryResult.total;
var dtoList = Mapper.Map<List<ListingDto>>(queryResult.list);
var baseAddress = GetBaseAddress();
foreach (var dtoItem in dtoList)
{
if (!string.IsNullOrWhiteSpace(dtoItem.Image))
dtoItem.Image = $"{baseAddress}/file/documents/thumbnails/{200}/{dtoItem.Image}";
if (!string.IsNullOrWhiteSpace(dtoItem.Image2))
dtoItem.Image2 = $"{baseAddress}/file/documents/thumbnails/{200}/{dtoItem.Image2}";
if (!string.IsNullOrWhiteSpace(dtoItem.ImageLanguage))
dtoItem.ImageLanguage = $"{baseAddress}/file/documents/thumbnails/{200}/{dtoItem.ImageLanguage}";
if (!string.IsNullOrWhiteSpace(dtoItem.Image2Language))
dtoItem.Image2Language = $"{baseAddress}/file/documents/thumbnails/{200}/{dtoItem.Image2Language}";
}
response.List = dtoList;
response.Take = model.Take;
response.Skip = model.Skip;
return Ok(response);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
/// <summary>
/// Gibt eine Listung zurück.
/// Diese muss laufen, darf nicht gelöscht sein und das Enddatum darf nicht erreicht sein
/// </summary>
/// <param name="id">Id der gesuchten Listung</param>
/// <param name="language">Gewünschte Sprache</param>
/// <returns>HTTP 200 OK wenn erfolgreich</returns>
[Route("GetRunningListing")]
[HttpGet]
public async Task<IActionResult> GetRunningListing(string id, string language)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var listing = await _listingService.GetRunningWithNamesAsync(id, language, LocalizationOptions.Value.DefaultCulture);
if (listing != null)
{
var dto = Mapper.Map<ListingDto>(listing);
var baseAddress = GetBaseAddress();
if (!string.IsNullOrWhiteSpace(dto.Image))
dto.Image = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.Image}";
if (!string.IsNullOrWhiteSpace(dto.Image2))
dto.Image2 = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.Image2}";
if (!string.IsNullOrWhiteSpace(dto.ImageLanguage))
dto.ImageLanguage = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.ImageLanguage}";
if (!string.IsNullOrWhiteSpace(dto.Image2Language))
dto.Image2Language = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.Image2Language}";
return Ok(dto);
}
}
return NotFound(CommunicationErrors.Common_NotFound);
}
/// <summary>
/// Gibt eine Liste von Listungen in einer Branche zurück
/// </summary>
/// <param name="model">Abfragemodel</param>
/// <returns>HTTP 200 wenn erfolgreich</returns>
[Route("GetByBranch")]
[HttpPost]
public async Task<IActionResult> GetByBranch(ListingQueryDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var response = new ListResponseDto<ListingDto>();
//Geodaten holen.
//Entweder wurde lat und lng geliefert, oder wir müssen über den benutzer und den app-mode entscheiden
var (country, state) = await GetGeoDataAsync(model);
var queryResult = await _listingService.GetByBranchForAppAsync(model.BranchId, model.Filter, model.ListingCountry, country, state, model.Location.Lat.Value, model.Location.Lng.Value, model.Language, LocalizationOptions.Value.DefaultCulture, model.Skip, model.Take);
response.Total = queryResult.total;
var dtoList = Mapper.Map<List<ListingDto>>(queryResult.list);
var baseAddress = GetBaseAddress();
foreach (var dtoItem in dtoList)
{
if (!string.IsNullOrWhiteSpace(dtoItem.Image))
dtoItem.Image = $"{baseAddress}/file/documents/thumbnails/{400}/{dtoItem.Image}";
if (!string.IsNullOrWhiteSpace(dtoItem.Image2))
dtoItem.Image2 = $"{baseAddress}/file/documents/thumbnails/{400}/{dtoItem.Image2}";
if (!string.IsNullOrWhiteSpace(dtoItem.ImageLanguage))
dtoItem.ImageLanguage = $"{baseAddress}/file/documents/thumbnails/{400}/{dtoItem.ImageLanguage}";
if (!string.IsNullOrWhiteSpace(dtoItem.Image2Language))
dtoItem.Image2Language = $"{baseAddress}/file/documents/thumbnails/{400}/{dtoItem.Image2Language}";
}
response.List = dtoList;
response.Take = model.Take;
response.Skip = model.Skip;
return Ok(response);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
}
}