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 { /// /// Controller der Zugriff auf Listungen via API ermöglicht /// [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ApiController] [ApiVersion(1)] [Route("api/listings")] [Route("api/v{v:apiVersion}/listings")] public class ApiListingsController : ApiBaseController { private readonly ILogger _logger; private readonly IGeoLocationService _geoLocationService; private readonly IListingService _listingService; private readonly IBranchService _branchService; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz eines ILogger /// Instanz von LocalizationOptions /// Instanz eines IDogOwnerService /// Instanz eines IGeoLocationService /// Instanz eines IListingService /// Instanz eines IBranchService public ApiListingsController(IMapper mapper, ILogger logger, IOptions localizationOptions, IAppUserService appUserService, IGeoLocationService geoLocationService, IListingService listingService, IBranchService branchService) : base(mapper, localizationOptions, appUserService) { _logger = logger; _geoLocationService = geoLocationService; _listingService = listingService; _branchService = branchService; } /// /// 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 /// /// Abfragemodel /// HTTP 200 wenn erfolgreich [Route("GetOverview")] [HttpPost] public async Task GetOverview(QueryDto model) { var clientOffset = GetClientDateOffset(); if (User?.Identity != null && User.Identity.IsAuthenticated) { var response = new ListResponseDto(); //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>(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>(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); } /// /// Gibt eine Liste von Listungen in der Nähe zurück /// /// Abfragemodel /// HTTP 200 wenn erfolgreich [Route("GetNearListings")] [HttpPost] public async Task GetNearListings(QueryDto model) { var clientOffset = GetClientDateOffset(); if (User?.Identity != null && User.Identity.IsAuthenticated) { var response = new ListResponseDto(); //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>(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); } /// /// Gibt eine Listung zurück. /// Diese muss laufen, darf nicht gelöscht sein und das Enddatum darf nicht erreicht sein /// /// Id der gesuchten Listung /// Gewünschte Sprache /// HTTP 200 OK wenn erfolgreich [Route("GetRunningListing")] [HttpGet] public async Task 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(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); } /// /// Gibt eine Liste von Listungen in einer Branche zurück /// /// Abfragemodel /// HTTP 200 wenn erfolgreich [Route("GetByBranch")] [HttpPost] public async Task GetByBranch(ListingQueryDto model) { var clientOffset = GetClientDateOffset(); if (User?.Identity != null && User.Identity.IsAuthenticated) { var response = new ListResponseDto(); //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>(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); } } }