using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Dto.DogWalkers;
using gehGassi.Dto;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using gehGassi.Core.Services;
using gehGassi.Domain.Common;
using gehGassi.Domain.Dogs;
using gehGassi.Dto.Common;
using gehGassi.Dto.Walks;
using System.Collections.Generic;
using System.Text.Json;
using gehGassi.Dto.Lookup;
using gehGassi.Domain.Walks;
using System;
using gehGassi.Dto.Dogs;
using gehGassi.Web.Auth;
using Asp.Versioning;
namespace gehGassi.Web.Controllers.Api
{
///
/// Controller der Zugriff auf DogWalker via Api ermöglicht
///
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[ApiVersion(1)]
[Route("api/dogwalkers")]
[Route("api/v{v:apiVersion}/dogwalkers")]
public class ApiDogWalkersController : ApiBaseController
{
private readonly IWalkingTimeService _walkingTimeService;
///
/// Erstellt eine Instanz
///
/// Instanz eines IMapper
/// Instanz von LocalizationOptions
/// Instanz eines IDogOwnerService
/// Instanz eines IWalkingTimeService
public ApiDogWalkersController(IMapper mapper, IOptions localizationOptions, IAppUserService appUserService, IWalkingTimeService walkingTimeService) : base(mapper, localizationOptions, appUserService)
{
_walkingTimeService = walkingTimeService;
}
///
/// Gibt Detail-Infos zu einem DogWalker zurück
///
/// Id des DogWalkers
///
[Route("GetDogWalker")]
[HttpGet]
public async Task GetDogWalker(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(walker);
var baseAddress = GetBaseAddress();
if (!string.IsNullOrWhiteSpace(dto.Photo))
dto.Photo = $"{baseAddress}/file/documents/thumbnails/{400}/{dto.Photo}";
var appUserId = User.AppUserId();
dto.Locked = walker.Locked;
dto.Blocked = await AppUserService.IsBlockedAsync(appUserId, walker.Id);
return Ok(dto);
}
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Abfragen von Dogwalkern
///
/// Abfragemodel
/// Liste von Dogwalkern
[HttpPost]
[Route("GetDogWalkers")]
public async Task GetDogWalkers(DogWalkerQueryDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var baseAddress = GetBaseAddress();
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 sortList = Mapper.Map>(model.DynamicSortOrder);
var queryResult = await AppUserService.GetWalkersAsync(model);
response.Total = queryResult.total;
var resultList = queryResult.list;
var dtoList = Mapper.Map>(resultList);
foreach (var dtoItem in dtoList)
{
if(!string.IsNullOrWhiteSpace(dtoItem.Photo))
dtoItem.Photo = $"{baseAddress}/file/documents/thumbnails/{400}/{dtoItem.Photo}";
}
response.List = dtoList;
response.Take = model.Take;
response.Skip = model.Skip;
return Ok(response);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Abfragen von Dogwalkern - Lookup als Rückgabe
///
/// Abfragemodel
/// Liste von Dogwalkern
[HttpPost]
[Route("GetDogWalkersLookup")]
public async Task GetDogWalkersLookup(DogWalkerQueryDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var baseAddress = GetBaseAddress();
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 sortList = Mapper.Map>(model.DynamicSortOrder);
var queryResult = await AppUserService.GetWalkersAsync(model);
response.Total = queryResult.total;
var resultList = queryResult.list;
var dtoList = Mapper.Map>(resultList);
foreach (var dtoItem in dtoList)
{
if(!string.IsNullOrWhiteSpace(dtoItem.Photo))
dtoItem.Photo = $"{baseAddress}/file/documents/thumbnails/{200}/{dtoItem.Photo}";
}
response.List = dtoList;
response.Take = model.Take;
response.Skip = model.Skip;
return Ok(response);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Abfrage der Walkingtimes eines Dogwalkers
///
/// Id des Dogwalkers
/// Letztes Update oder null, wenn noch keines
/// Liste der Anfragen
[HttpGet]
[Route("GetWalkingTimesForSync")]
public async Task GetWalkingTimesForSync(string dogWalkerId, DateTimeOffset? lastUpdate)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var requests = await _walkingTimeService.GetForSyncAppAsync(dogWalkerId, lastUpdate);
var dtoList = Mapper.Map>(requests);
return Ok(dtoList);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Anlegen einer WalkingTime
///
/// WalkingTimeDto
/// HTTP 200 OK, Felher sonst
[HttpPost]
[Route("CreateWalkingTime")]
public async Task CreateWalk(WalkingTimeDto model)
{
var clientOffset = GetClientDateOffset();
var result = new CreateResponseDto
{
Status = CreateStatusDto.Error,
Value = null
};
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
var existing = await _walkingTimeService.GetAsync(model.Id);
if (existing == null)
{
var walkingTime = Mapper.Map(model);
_walkingTimeService.Add(walkingTime);
await _walkingTimeService.CommitAsync(User.Identity.Name);
result.Status = CreateStatusDto.Success;
result.Value = Mapper.Map(walkingTime);
}
else
{
result.Status = CreateStatusDto.Exists;
}
return Ok(result);
}
}
return BadRequest(CommunicationErrors.Common_Model_Invalid);
}
///
/// Aktualisieren einer WalkingTime
///
/// WalkingTimeDto
/// HTTP 200 OK, Felher sonst
[HttpPost]
[Route("UpdateWalkingTime")]
public async Task UpdateWalk(WalkingTimeDto model)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
var walkingTime = await _walkingTimeService.GetAsync(model.Id);
if (walkingTime != null)
{
if (model.UpdatedAt > walkingTime.UpdatedAt)
{
Mapper.Map(model, walkingTime);
await _walkingTimeService.CommitAsync(User.Identity.Name);
}
return Ok();
}
return NotFound(CommunicationErrors.Common_NotFound);
}
return BadRequest(CommunicationErrors.Common_Model_Invalid);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Löschen einer Walkingtime
///
/// WalkingTimeDto
/// Id des App-Users
/// 200OK
[HttpPost]
[Route("DeleteWalkingTime")]
public async Task DeleteWalkingTime(WalkingTimeDto model, [FromQuery] string appUserId)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
var walkingTime = await _walkingTimeService.GetAsync(model.Id);
if (walkingTime != null)
{
var appUser = await AppUserService.GetAsync(appUserId);
if (appUser != null)
{
if (walkingTime.DogWalkerId == appUser.Id)
{
//TODO: Löschen von verbundenen Daten!
_walkingTimeService.Remove(walkingTime);
await _walkingTimeService.CommitAsync(User.Identity.Name);
return Ok();
}
}
}
return NotFound(CommunicationErrors.Common_NotFound);
}
return BadRequest(CommunicationErrors.Common_Model_Invalid);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
///
/// Gibt alle Walkingtimes eines Dogwalkers zurück
///
/// Id des DogWalkers
/// Liste Walkingtimes
[HttpGet]
[Route("GetWalkingTimes")]
public async Task GetWalkingTimes(string dogWalkerId)
{
var clientOffset = GetClientDateOffset();
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var walkingTimes = await _walkingTimeService.GetAllAsync(dogWalkerId);
var walkingTimesDto = Mapper.Map>(walkingTimes);
return Ok(walkingTimesDto);
}
return NotFound(CommunicationErrors.Common_NotFound);
}
}
}