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 { /// /// Controller der der Website Zugriff auf bestimmte Daten ermöglicht /// [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; /// /// Erstellt eine neue Instanz des Controllers /// /// Instanz eines IMapper /// Instanz von LocalizationOptions /// Instanz eines IUserService /// Instanz eines IAppUserService /// Instanz eines IFaqService public WebSiteApiDataController(IMapper mapper, IOptions localizationOptions, IUserService userService, IAppUserService appUserService, IFaqService faqService) : base(mapper, localizationOptions, userService) { _appUserService = appUserService; _faqService = faqService; } /// /// Returns a list of locations of currently active app users. /// /// Number of records to skip /// Number of records to take /// Type of app users to select. DogOwner = 10, DogWalker = 20, Both = 30 /// Optional: provide a timestamp as date time offset UTC to get only records with a last update date newer than the time stamp /// List of locations [HttpGet] [Route("GetAppUsers")] [Produces(typeof(AppUserLocationItemDto))] public async Task GetAppUsers(int skip, int take, AppUserTypeDto type = AppUserTypeDto.Both, DateTimeOffset? timeStamp = null) { var resultList = new List(); 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); } /// /// Returns a list of FAQs by type. /// /// FAQ-Type. DogOwner = 10, DogWalker = 20, Both = 30 /// List of faq [HttpGet] [Route("GetFaqs")] [Produces(typeof(FaqDto))] public async Task GetFaqs(FaqType type = FaqType.Both) { var result = new List(); 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() }; 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); } } }