gehgassi_backend/gehGassi.Web/Controllers/WebsiteApi/WebSiteApiDataController.cs

137 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Asp.Versioning;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Dto.Common;
using gehGassi.Dto.Faqs;
using gehGassi.Dto.WebSite;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace gehGassi.Web.Controllers.WebsiteApi
{
/// <summary>
/// Controller der der Website Zugriff auf bestimmte Daten ermöglicht
/// </summary>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "ApiUser")]
[ApiController]
[ApiVersion(1)]
[Route("api/website/data")]
[Route("api/website/v{v:apiVersion}/data")]
public class WebSiteApiDataController : WebSiteApiBaseController
{
private readonly IAppUserService _appUserService;
private readonly IFaqService _faqService;
/// <summary>
/// Erstellt eine neue Instanz des Controllers
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
/// <param name="userService">Instanz eines IUserService</param>
/// <param name="appUserService">Instanz eines IAppUserService</param>
/// <param name="faqService">Instanz eines IFaqService</param>
public WebSiteApiDataController(IMapper mapper, IOptions<LocalizationOptions> localizationOptions, IUserService userService,
IAppUserService appUserService, IFaqService faqService) : base(mapper, localizationOptions, userService)
{
_appUserService = appUserService;
_faqService = faqService;
}
/// <summary>
/// Returns a list of locations of currently active app users.
/// </summary>
/// <param name="skip">Number of records to skip</param>
/// <param name="take">Number of records to take</param>
/// <param name="type">Type of app users to select. DogOwner = 10, DogWalker = 20, Both = 30</param>
/// <param name="timeStamp">Optional: provide a timestamp as date time offset UTC to get only records with a last update date newer than the time stamp</param>
/// <returns>List of locations</returns>
[HttpGet]
[Route("GetAppUsers")]
[Produces(typeof(AppUserLocationItemDto))]
public async Task<IActionResult> GetAppUsers(int skip, int take, AppUserTypeDto type = AppUserTypeDto.Both, DateTimeOffset? timeStamp = null)
{
var resultList = new List<AppUserLocationItemDto>();
var result = new AppUserLocationDto()
{
Skip = skip,
Take = take,
Items = resultList,
TimeStamp = timeStamp
};
var total = await _appUserService.CountAsync(false, (AppUserType)type, timeStamp);
result.Total = total;
var list = await _appUserService.GetAllAsync(skip, take, (AppUserType)type, timeStamp, false);
foreach (var appUser in list)
{
var item = new AppUserLocationItemDto()
{
Type = (AppUserTypeDto)appUser.Type,
Zip = appUser.Address.Zip,
City = appUser.Address.City,
Country = appUser.Address.CountryCode,
Lat = appUser.Location.Y,
Lng = appUser.Location.X
};
resultList.Add(item);
}
return Ok(result);
}
/// <summary>
/// Returns a list of FAQs by type.
/// </summary>
/// <param name="type">FAQ-Type. DogOwner = 10, DogWalker = 20, Both = 30</param>
/// <returns>List of faq</returns>
[HttpGet]
[Route("GetFaqs")]
[Produces(typeof(FaqDto))]
public async Task<IActionResult> GetFaqs(FaqType type = FaqType.Both)
{
var result = new List<FaqDto>();
var faqList = await _faqService.GetAllAsync(type);
foreach (var faq in faqList)
{
var item = new FaqDto()
{
Title = faq.Title,
Order = faq.Order,
Type = (FaqTypeDto)faq.Type,
Translations = new List<FaqTextDto>()
};
var languages = faq.GetLanguages("Question");
foreach (var language in languages)
{
var question = faq.Get("Question", language);
var answer = faq.Get("Answer", language);
var faqText = new FaqTextDto()
{
Language = language,
Question = question,
Answer = answer
};
item.Translations.Add(faqText);
}
result.Add(item);
}
return Ok(result);
}
}
}