148 lines
5.6 KiB
C#
148 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Asp.Versioning;
|
|
using AutoMapper;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Dto;
|
|
using gehGassi.Dto.DogWalkers;
|
|
using gehGassi.Dto.Lookup;
|
|
using gehGassi.Dto.News;
|
|
using gehGassi.Web.Auth;
|
|
using gehGassi.Web.Helper;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using NuGet.Packaging;
|
|
|
|
namespace gehGassi.Web.Controllers.Api
|
|
{
|
|
/// <summary>
|
|
/// Controller der Lookups via Api ermöglicht
|
|
/// </summary>
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
[ApiController]
|
|
[ApiVersion(1)]
|
|
[Route("api/lookup")]
|
|
[Route("api/v{v:apiVersion}/lookup")]
|
|
public class ApiLookupController : ApiBaseController
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
|
|
/// <param name="appUserService">Instanz eines IDogOwnerService</param>
|
|
public ApiLookupController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService) : base(mapper, localizationOptions, appUserService)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von DogOwnern und DogWalkern basierend auf dem Filter zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filter der in Vor- oder Nachnamen enthalten sein muss</param>
|
|
/// <param name="take">wie viele Datensätze maximal sollen geliefert werden?</param>
|
|
/// <returns></returns>
|
|
[Route("Entities")]
|
|
[HttpGet]
|
|
public async Task<IActionResult> LookupEntities(string filter, int take)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
var result = new List<AppUserLookupDto>();
|
|
filter = filter.ToLower();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
|
|
//TODO: hier noch die Relationen berücksichtigen. Mal sehen wie.
|
|
|
|
var appUsersQuery = AppUserService.Query(c => string.Concat(c.FirstName, " ", c.LastName).ToLower().Contains(filter) && c.Deleted == false && c.Locked == false && c.Id != User.AppUserId());
|
|
|
|
var appUsers = await appUsersQuery.Take(take).ToListAsync().ConfigureAwait(false);
|
|
|
|
result.AddRange(Mapper.Map<List<AppUserLookupDto>>(appUsers));
|
|
|
|
var baseAddress = GetBaseAddress();
|
|
foreach (var item in result)
|
|
{
|
|
if(!string.IsNullOrWhiteSpace(item.Photo))
|
|
item.Photo = $"{baseAddress}/file/documents/thumbnails/{100}/{item.Photo}";
|
|
}
|
|
|
|
//Es wird bewusst keine sortierung gemacht. Diese erfolgt in der App
|
|
return Ok(result);
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von DogWalkern basierend auf der aktuellen Position zurück
|
|
/// </summary>
|
|
/// <param name="lng">Längengrad</param>
|
|
/// <param name="take">wie viele Datensätze maximal sollen geliefert werden?</param>
|
|
/// <param name="ignoreId">Id des App-Users der ignoriert werden soll, falls ein DogOwner auch Walker ist</param>
|
|
/// <param name="lat">Breitengrad</param>
|
|
/// <returns></returns>
|
|
[Route("DogWalkers")]
|
|
[HttpGet]
|
|
public async Task<IActionResult> DogWalkers(string ignoreId, double lat, double lng, int take)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
var result = new List<DogWalkerLookupDto>();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
|
|
var walkers = await AppUserService.GetWalkersForAppAsync(ignoreId, lat, lng, take);
|
|
|
|
result.AddRange(Mapper.Map<List<DogWalkerLookupDto>>(walkers));
|
|
|
|
var baseAddress = GetBaseAddress();
|
|
foreach (var item in result)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(item.Photo))
|
|
item.Photo = $"{baseAddress}/file/documents/thumbnails/{100}/{item.Photo}";
|
|
}
|
|
|
|
//Es wird bewusst keine sortierung gemacht. Diese erfolgt in der App
|
|
return Ok(result);
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt Detail-Infos zu einem DogWalker zurück
|
|
/// </summary>
|
|
/// <param name="dogWalkerId">Id des DogWalkers</param>
|
|
/// <returns></returns>
|
|
[Route("DogWalker")]
|
|
[HttpGet]
|
|
public async Task<IActionResult> DogWalker(string dogWalkerId)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
|
|
var walker = await AppUserService.GetWalkerAsync(dogWalkerId);
|
|
if (walker != null)
|
|
{
|
|
var dto = Mapper.Map<DogWalkerInfoDto>(walker);
|
|
var baseAddress = GetBaseAddress();
|
|
if (!string.IsNullOrWhiteSpace(dto.Photo))
|
|
dto.Photo = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.Photo}";
|
|
|
|
return Ok(dto);
|
|
}
|
|
}
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
}
|
|
}
|