331 lines
12 KiB
C#
331 lines
12 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Controller der Zugriff auf DogWalker via Api ermöglicht
|
|
/// </summary>
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
[ApiController]
|
|
[ApiVersion(1)]
|
|
[Route("api/dogwalkers")]
|
|
[Route("api/v{v:apiVersion}/dogwalkers")]
|
|
public class ApiDogWalkersController : ApiBaseController
|
|
{
|
|
private readonly IWalkingTimeService _walkingTimeService;
|
|
|
|
/// <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>
|
|
/// <param name="walkingTimeService">Instanz eines IWalkingTimeService</param>
|
|
public ApiDogWalkersController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService, IWalkingTimeService walkingTimeService) : base(mapper, localizationOptions, appUserService)
|
|
{
|
|
_walkingTimeService = walkingTimeService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt Detail-Infos zu einem DogWalker zurück
|
|
/// </summary>
|
|
/// <param name="dogWalkerId">Id des DogWalkers</param>
|
|
/// <returns></returns>
|
|
[Route("GetDogWalker")]
|
|
[HttpGet]
|
|
public async Task<IActionResult> 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<DogWalkerInfoDto>(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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abfragen von Dogwalkern
|
|
/// </summary>
|
|
/// <param name="model">Abfragemodel</param>
|
|
/// <returns>Liste von Dogwalkern</returns>
|
|
[HttpPost]
|
|
[Route("GetDogWalkers")]
|
|
public async Task<IActionResult> GetDogWalkers(DogWalkerQueryDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var baseAddress = GetBaseAddress();
|
|
var response = new ListResponseDto<DogWalkerInfoDto>();
|
|
|
|
//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<List<DynamicSortOrder>>(model.DynamicSortOrder);
|
|
|
|
var queryResult = await AppUserService.GetWalkersAsync(model);
|
|
|
|
response.Total = queryResult.total;
|
|
var resultList = queryResult.list;
|
|
|
|
var dtoList = Mapper.Map<List<DogWalkerInfoDto>>(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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abfragen von Dogwalkern - Lookup als Rückgabe
|
|
/// </summary>
|
|
/// <param name="model">Abfragemodel</param>
|
|
/// <returns>Liste von Dogwalkern</returns>
|
|
[HttpPost]
|
|
[Route("GetDogWalkersLookup")]
|
|
public async Task<IActionResult> GetDogWalkersLookup(DogWalkerQueryDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var baseAddress = GetBaseAddress();
|
|
var response = new ListResponseDto<DogWalkerLookupDto>();
|
|
|
|
//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<List<DynamicSortOrder>>(model.DynamicSortOrder);
|
|
|
|
var queryResult = await AppUserService.GetWalkersAsync(model);
|
|
|
|
response.Total = queryResult.total;
|
|
var resultList = queryResult.list;
|
|
|
|
var dtoList = Mapper.Map<List<DogWalkerLookupDto>>(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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abfrage der Walkingtimes eines Dogwalkers
|
|
/// </summary>
|
|
/// <param name="dogWalkerId">Id des Dogwalkers</param>
|
|
/// <param name="lastUpdate">Letztes Update oder null, wenn noch keines</param>
|
|
/// <returns>Liste der Anfragen</returns>
|
|
[HttpGet]
|
|
[Route("GetWalkingTimesForSync")]
|
|
public async Task<IActionResult> 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<List<WalkingTimeDto>>(requests);
|
|
return Ok(dtoList);
|
|
}
|
|
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer WalkingTime
|
|
/// </summary>
|
|
/// <param name="model">WalkingTimeDto</param>
|
|
/// <returns>HTTP 200 OK, Felher sonst</returns>
|
|
[HttpPost]
|
|
[Route("CreateWalkingTime")]
|
|
public async Task<IActionResult> CreateWalk(WalkingTimeDto model)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
var result = new CreateResponseDto<WalkingTimeDto>
|
|
{
|
|
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<WalkingTime>(model);
|
|
_walkingTimeService.Add(walkingTime);
|
|
await _walkingTimeService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Status = CreateStatusDto.Success;
|
|
result.Value = Mapper.Map<WalkingTimeDto>(walkingTime);
|
|
}
|
|
else
|
|
{
|
|
result.Status = CreateStatusDto.Exists;
|
|
}
|
|
return Ok(result);
|
|
}
|
|
}
|
|
|
|
return BadRequest(CommunicationErrors.Common_Model_Invalid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aktualisieren einer WalkingTime
|
|
/// </summary>
|
|
/// <param name="model">WalkingTimeDto</param>
|
|
/// <returns>HTTP 200 OK, Felher sonst</returns>
|
|
[HttpPost]
|
|
[Route("UpdateWalkingTime")]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Walkingtime
|
|
/// </summary>
|
|
/// <param name="model">WalkingTimeDto</param>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <returns>200OK</returns>
|
|
[HttpPost]
|
|
[Route("DeleteWalkingTime")]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt alle Walkingtimes eines Dogwalkers zurück
|
|
/// </summary>
|
|
/// <param name="dogWalkerId">Id des DogWalkers</param>
|
|
/// <returns>Liste Walkingtimes</returns>
|
|
[HttpGet]
|
|
[Route("GetWalkingTimes")]
|
|
public async Task<IActionResult> GetWalkingTimes(string dogWalkerId)
|
|
{
|
|
var clientOffset = GetClientDateOffset();
|
|
|
|
if (User?.Identity != null && User.Identity.IsAuthenticated)
|
|
{
|
|
var walkingTimes = await _walkingTimeService.GetAllAsync(dogWalkerId);
|
|
var walkingTimesDto = Mapper.Map<List<WalkingTimeDto>>(walkingTimes);
|
|
|
|
return Ok(walkingTimesDto);
|
|
}
|
|
|
|
return NotFound(CommunicationErrors.Common_NotFound);
|
|
}
|
|
}
|
|
}
|