1350 lines
73 KiB
C#
1350 lines
73 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using gehGassi.Common.Data;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.External.Services;
|
|
using gehGassi.Permissions;
|
|
using gehGassi.Web.Auth;
|
|
using gehGassi.Web.Auth.Attributes;
|
|
using gehGassi.Web.Helper;
|
|
using gehGassi.Web.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
using NetTopologySuite.Geometries;
|
|
using NetTopologySuite;
|
|
using gehGassi.Domain.Advertisements;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Listingen
|
|
/// </summary>
|
|
[Authorize]
|
|
public class ListingController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<BranchController> _localizer;
|
|
private readonly IListingService _listingService;
|
|
private readonly ILanguageService _languageService;
|
|
private readonly ICountryService _countryService;
|
|
private readonly IBranchService _branchService;
|
|
private readonly ICustomerService _customerService;
|
|
private readonly IPinService _pinService;
|
|
private readonly IGeoLocationService _geoLocationService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="listingService">Instanz eines IListingService</param>
|
|
/// <param name="languageService">Instanz eines ILanguageService</param>
|
|
/// <param name="countryService">Instanz eines ICountryService</param>
|
|
/// <param name="branchService">Instanz eines IBranchService</param>
|
|
/// <param name="customerService">Instanz eines ICustomerService</param>
|
|
/// <param name="pinService">Instanz eines IPinService</param>
|
|
/// <param name="geoLocationService">Instanz eines IGeoLocationService</param>
|
|
public ListingController(IMapper mapper, IStringLocalizer<BranchController> localizer, IListingService listingService, ILanguageService languageService, ICountryService countryService, IBranchService branchService,
|
|
ICustomerService customerService, IPinService pinService, IGeoLocationService geoLocationService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_listingService = listingService;
|
|
_languageService = languageService;
|
|
_countryService = countryService;
|
|
_branchService = branchService;
|
|
_customerService = customerService;
|
|
_pinService = pinService;
|
|
_geoLocationService = geoLocationService;
|
|
}
|
|
|
|
#region Admin / Poweruser
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Verwaltung von Listungen zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsRead)]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsRead)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetListings([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "listingType")
|
|
whereFilterPredicate.value = (ListingType)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "status")
|
|
whereFilterPredicate.value = (ListingStatus)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "paymentType")
|
|
whereFilterPredicate.value = (PaymentType)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "paymentStatus")
|
|
whereFilterPredicate.value = (PaymentStatus)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "geoMode")
|
|
whereFilterPredicate.value = (GeoMode)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var resultList = _listingService.FilterWithNames(dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage, includeDeleted: false, excludeDraft: true);
|
|
|
|
//Sortierung
|
|
resultList = dm.ApplySorting(resultList);
|
|
//Filter
|
|
resultList = dm.ApplyFiltering(resultList, out var countFiltered);
|
|
//Paging
|
|
resultList = dm.ApplyPaging(resultList);
|
|
|
|
var resultListVm = resultList.ToList().Select(item => _mapper.Map<ListingListVm>(item)).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.ListingTypeText = itemVm.ListingType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.StatusText = itemVm.Status.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PaymentTypeText = itemVm.PaymentType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PaymentStatusText = itemVm.PaymentStatus.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.GeoModeText = itemVm.GeoMode.GetDisplayName(AnnotationsLocalizer);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Listung
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsCreate)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Create()
|
|
{
|
|
var model = new ListingCrudVm()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
TextVms = new List<ListingTextVm>(),
|
|
CustomerName = _localizer["Common_None"],
|
|
BranchName = _localizer["Common_None"],
|
|
ListingType = ListingTypeVm.Base,
|
|
Status = ListingStatusVm.Draft,
|
|
PaymentType = PaymentTypeVm.None,
|
|
PaymentStatus = PaymentStatusVm.None,
|
|
GeoMode = GeoModeVm.None
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ListingTextVm() { Id = model.Id, Language = language, Name = string.Empty, Description = string.Empty, ImageLanguage = string.Empty, Image2Language = string.Empty});
|
|
}
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Listung
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsCreate)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(ListingCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
//TODO: Validierung ob die Listung eingetragen werden kann. Hängt vom Listungstyp, der Branche und dem Zeitraum ab
|
|
var availableResult = await _listingService.IsAvailableAsync(model.CustomerId, model.BranchId, (ListingType)model.ListingType, model.StartDate.Value, model.EndDate.Value, model.Address.CountryCode,"");
|
|
if (availableResult.Valid)
|
|
{
|
|
var item = _listingService.Create();
|
|
_mapper.Map(model, item);
|
|
item.Created = DateTimeOffset.UtcNow;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
item.Set("Image2Language", textVm.Language, textVm.Image2Language);
|
|
item.Set("UrlLanguage", textVm.Language, textVm.UrlLanguage);
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(textVm.Image2Language))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.Image2Language);
|
|
var fileName = Path.GetFileName(textVm.Image2Language);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("Image2Language", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.Image2Language);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
if (item.GeoMode != GeoMode.Location)
|
|
{
|
|
var location = await _geoLocationService.GetLocationAsync(item.Address, SelectedLanguage);
|
|
if (location.Success)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var geoLocation = geometryFactory.CreatePoint(new Coordinate(location.Longitude, location.Latitude));
|
|
item.Location = geoLocation;
|
|
}
|
|
await Task.Delay(1000);
|
|
}
|
|
|
|
var listingLocation = await _geoLocationService.GetLocationAsync(item.ListingAddress, SelectedLanguage);
|
|
if (listingLocation.Success)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var geoLocation = geometryFactory.CreatePoint(new Coordinate(listingLocation.Longitude, listingLocation.Latitude));
|
|
item.ListingLocation = geoLocation;
|
|
}
|
|
|
|
_listingService.Add(item);
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.Image))
|
|
{
|
|
//Nun das Bild umbenennen und dann kopieren. Ebenso Thumbnails erstellen
|
|
var extension = Path.GetExtension(model.Image);
|
|
var fileName = Path.GetFileName(model.Image);
|
|
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
|
|
//Altes Löschen
|
|
await FileService.DeleteAsync(FileServiceHelper.TempContainer, model.Image);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(model.Image2))
|
|
{
|
|
//Nun das Bild umbenennen und dann kopieren. Ebenso Thumbnails erstellen
|
|
var extension = Path.GetExtension(model.Image2);
|
|
var fileName = Path.GetFileName(model.Image2);
|
|
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image2 = filenameToUse;
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image2);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
|
|
//Altes Löschen
|
|
await FileService.DeleteAsync(FileServiceHelper.TempContainer, model.Image2);
|
|
}
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
else
|
|
{
|
|
if(availableResult.ErrorCode == ListingValidationError.AlreadyBooked)
|
|
result.ErrorMessage = _localizer["Err_Listing_AlreadyBooked"].Value;
|
|
else
|
|
result.ErrorMessage = String.Format(_localizer["Err_Listing_NotAvailable"].Value, availableResult.Booked, availableResult.Reserved);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
ModelState.Remove("LisingCountryName");
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
ModelState.Remove("ListingStateName");
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
ModelState.Remove("CustomerName");
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Listung
|
|
/// </summary>
|
|
/// <param name="id">Id der Listung</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsEdit)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(string id)
|
|
{
|
|
var item = await _listingService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
//TODO: Prüfen ob die Listung überhaupt noch bearbeitet werden kann
|
|
|
|
var model = _mapper.Map<ListingCrudVm>(item);
|
|
|
|
model.TextVms = new List<ListingTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ListingTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true),
|
|
Description = item.Get("Description", language, true),
|
|
ImageLanguage = item.Get("ImageLanguage", language, true),
|
|
Image2Language = item.Get("Image2Language", language, true),
|
|
UrlLanguage = item.Get("UrlLanguage", language, true)
|
|
});
|
|
}
|
|
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Listung
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsEdit)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(ListingCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _listingService.GetAsync(model.Id);
|
|
if (item is { Deleted: false })
|
|
{
|
|
if (item.Version.SequenceEqual(model.Version))
|
|
{
|
|
//TODO: Validierung ob die Listung eingetragen werden kann. Hängt vom Listungstyp, der Branche und dem Zeitraum ab
|
|
var availableResult = await _listingService.IsAvailableAsync(model.CustomerId, model.BranchId, (ListingType)model.ListingType, model.StartDate.Value, model.EndDate.Value, model.Address.CountryCode,model.Id);
|
|
if (availableResult.Valid)
|
|
{
|
|
var oldImage = item.Image;
|
|
var oldImage2 = item.Image2;
|
|
|
|
_mapper.Map(model, item);
|
|
|
|
if (item.GeoMode != GeoMode.Location)
|
|
{
|
|
var location = await _geoLocationService.GetLocationAsync(item.Address, SelectedLanguage);
|
|
if (location.Success)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var geoLocation = geometryFactory.CreatePoint(new Coordinate(location.Longitude, location.Latitude));
|
|
item.Location = geoLocation;
|
|
}
|
|
|
|
await Task.Delay(1000);
|
|
}
|
|
|
|
var listingLocation = await _geoLocationService.GetLocationAsync(item.ListingAddress, SelectedLanguage);
|
|
if (listingLocation.Success)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var geoLocation = geometryFactory.CreatePoint(new Coordinate(listingLocation.Longitude, listingLocation.Latitude));
|
|
item.ListingLocation = geoLocation;
|
|
}
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
var oldLangImage = item.Get("ImageLanguage", textVm.Language, true);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
var oldLangImage2 = item.Get("Image2Language", textVm.Language, true);
|
|
item.Set("Image2Language", textVm.Language, textVm.Image2Language);
|
|
item.Set("UrlLanguage", textVm.Language, textVm.UrlLanguage);
|
|
|
|
if (oldLangImage != textVm.ImageLanguage)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
if (oldLangImage2 != textVm.Image2Language)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage2))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage2);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.Image2Language))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.Image2Language);
|
|
var fileName = Path.GetFileName(textVm.Image2Language);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("Image2Language", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.Image2Language);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
if (oldImage != item.Image)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image);
|
|
var fileName = Path.GetFileName(item.Image);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
if (oldImage2 != item.Image2)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage2))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage2);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image2))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image2);
|
|
var fileName = Path.GetFileName(item.Image2);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image2 = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image2);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
else
|
|
{
|
|
if (availableResult.ErrorCode == ListingValidationError.AlreadyBooked)
|
|
result.ErrorMessage = _localizer["Err_Listing_AlreadyBooked"].Value;
|
|
else
|
|
result.ErrorMessage = String.Format(_localizer["Err_Listing_NotAvailable"].Value, availableResult.Booked, availableResult.Reserved);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Changed"].Value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Deleted"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
ModelState.Remove("LisingCountryName");
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
ModelState.Remove("ListingStateName");
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
ModelState.Remove("CustomerName");
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Listung
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <param name="forceDelete">Sollen die Daten physisch gelöscht werden?</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsDelete)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<string> ids, bool forceDelete = false)
|
|
{
|
|
var batchErrorHeader = $"<p><strong>{_localizer["Common_BatchDelete_Failed"].Value}</strong></p>";
|
|
var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true };
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var item = await _listingService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
|
|
await _pinService.ResetListingAsync(item.Id);
|
|
var usedCount = 0;
|
|
|
|
//var productsCount = await _productService.CountByAdvertisementCategoryAsync(item.Id);
|
|
//usedCount += productsCount;
|
|
|
|
if (usedCount == 0)
|
|
{
|
|
//TODO: Gibt es Resets die notwendig sind?
|
|
//await _productService.ResetAdvertisementCategoryAsync(item.Id, true);
|
|
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Name, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
if (forceDelete)
|
|
{
|
|
var postingPath = FileServiceHelper.GetListingPath(item.Id);
|
|
await FileService.ClearDirectoryAsync(FileServiceHelper.DocumentContainer, postingPath);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 100);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image2, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image2, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image2, 100);
|
|
|
|
var languages = item.GetLanguages("ImageLanguage");
|
|
foreach (var language in languages)
|
|
{
|
|
item.SetLanguage(language);
|
|
var imageLocalized = item.ImageLanguage;
|
|
if (!string.IsNullOrWhiteSpace(imageLocalized))
|
|
{
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 100);
|
|
}
|
|
var image2Localized = item.Image2Language;
|
|
if (!string.IsNullOrWhiteSpace(image2Localized))
|
|
{
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, image2Localized, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, image2Localized, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, image2Localized, 100);
|
|
}
|
|
}
|
|
|
|
_listingService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
item.Deleted = true;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (usedCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Name, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Name}</strong>", $"<strong>{usedCount}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"<strong>{id}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
if (batchResult.BatchResponseList.Any(c => c.Success))
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Customer / Kunde
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Listungsverwaltung für Kunden zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsRead)]
|
|
public IActionResult IndexCustomer()
|
|
{
|
|
var customerId = User.CustomerUniqueId();
|
|
return View(customerId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <param name="customerUniqueId">UniqueId des Kunden</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsRead)]
|
|
[CustomerAuthorize("customerUniqueId")]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> GetListingsCustomer([FromBody] DataManager dm, Guid customerUniqueId)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "listingType")
|
|
whereFilterPredicate.value = (ListingType)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "status")
|
|
whereFilterPredicate.value = (ListingStatus)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "paymentType")
|
|
whereFilterPredicate.value = (PaymentType)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "paymentStatus")
|
|
whereFilterPredicate.value = (PaymentStatus)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "geoMode")
|
|
whereFilterPredicate.value = (GeoMode)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
long customerId = -1;
|
|
var customer = await _customerService.GetByUniqueIdAsync(customerUniqueId);
|
|
if (customer != null)
|
|
customerId = customer.Id;
|
|
|
|
var resultList = _listingService.FilterWithNamesCustomer(customerId, dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage, includeDeleted: false, excludeDraft: true);
|
|
|
|
//Sortierung
|
|
resultList = dm.ApplySorting(resultList);
|
|
//Filter
|
|
resultList = dm.ApplyFiltering(resultList, out var countFiltered);
|
|
//Paging
|
|
resultList = dm.ApplyPaging(resultList);
|
|
|
|
var resultListVm = resultList.ToList().Select(item => _mapper.Map<ListingListVm>(item)).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.ListingTypeText = itemVm.ListingType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.StatusText = itemVm.Status.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PaymentTypeText = itemVm.PaymentType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PaymentStatusText = itemVm.PaymentStatus.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.GeoModeText = itemVm.GeoMode.GetDisplayName(AnnotationsLocalizer);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Buchen einer Listung für Kunden
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsBook)]
|
|
[CustomerAuthorize("customerUniqueId")]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult BookCustomer(Guid customerUniqueId)
|
|
{
|
|
var model = new ListingBookVm()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
CustomerId = User.CustomerId().Value,
|
|
TextVms = new List<ListingTextVm>(),
|
|
BranchName = _localizer["Common_None"],
|
|
ListingType = ListingTypeVm.Base,
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ListingTextVm() { Id = model.Id, Language = language, Name = string.Empty, Description = string.Empty, ImageLanguage = string.Empty, Image2Language = string.Empty});
|
|
}
|
|
|
|
return PartialView("_BookCustomer", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Buchen einer Listung für Kunden
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsBook)]
|
|
[CustomerAuthorize("CustomerId")]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> BookCustomer(ListingBookVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
//TODO: Validierung ob die Listung eingetragen werden kann. Hängt vom Listungstyp, der Branche und dem Zeitraum ab
|
|
var availableResult = await _listingService.IsAvailableAsync(model.CustomerId, model.BranchId, (ListingType)model.ListingType, model.StartDate.Value, model.EndDate.Value, model.Address.CountryCode, "");
|
|
if (availableResult.Valid)
|
|
{
|
|
//Passendes Produk?
|
|
|
|
var item = _listingService.Create();
|
|
_mapper.Map(model, item);
|
|
item.Created = DateTimeOffset.UtcNow;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
item.Set("UrlLanguage", textVm.Language, textVm.UrlLanguage);
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
_listingService.Add(item);
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.Image))
|
|
{
|
|
//Nun das Bild umbenennen und dann kopieren. Ebenso Thumbnails erstellen
|
|
var extension = Path.GetExtension(model.Image);
|
|
var fileName = Path.GetFileName(model.Image);
|
|
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
|
|
//Altes Löschen
|
|
await FileService.DeleteAsync(FileServiceHelper.TempContainer, model.Image);
|
|
}
|
|
|
|
//Jetzt Cart anlegen und Cart-Item
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
else
|
|
{
|
|
if (availableResult.ErrorCode == ListingValidationError.AlreadyBooked)
|
|
result.ErrorMessage = _localizer["Err_Listing_AlreadyBooked"].Value;
|
|
else
|
|
result.ErrorMessage = String.Format(_localizer["Err_Listing_NotAvailable"].Value, availableResult.Booked, availableResult.Reserved);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
result.Html = await PartialView("_BookCustomer", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Listung für Kunden
|
|
/// </summary>
|
|
/// <param name="id">Id der Listung</param>
|
|
/// <param name="customerUniqueId">UniqueId des Kunden</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[CustomerAuthorize("customerUniqueId")]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsEdit)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> EditCustomer(string id, Guid customerUniqueId)
|
|
{
|
|
var item = await _listingService.GetAsync(id);
|
|
if (item != null && item.CustomerId == User.CustomerId())
|
|
{
|
|
var model = _mapper.Map<ListingCrudVm>(item);
|
|
|
|
model.TextVms = new List<ListingTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ListingTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true),
|
|
Description = item.Get("Description", language, true),
|
|
ImageLanguage = item.Get("ImageLanguage", language, true),
|
|
Image2Language = item.Get("Image2Language", language, true),
|
|
UrlLanguage = item.Get("UrlLanguage", language, true)
|
|
});
|
|
}
|
|
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
return PartialView("_EditCustomer", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Listung für Kunden
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsEdit)]
|
|
[CustomerAuthorize("CustomerId")]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> EditCustomer(ListingCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _listingService.GetAsync(model.Id);
|
|
if (item is { Deleted: false })
|
|
{
|
|
if (item.Version.SequenceEqual(model.Version))
|
|
{
|
|
var oldImage = item.Image;
|
|
var oldImage2 = item.Image2;
|
|
|
|
_mapper.Map(model, item);
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
var oldLangImage = item.Get("ImageLanguage", textVm.Language, true);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
var oldLangImage2 = item.Get("Image2Language", textVm.Language, true);
|
|
item.Set("Image2Language", textVm.Language, textVm.Image2Language);
|
|
item.Set("UrlLanguage", textVm.Language, textVm.UrlLanguage);
|
|
|
|
if (oldLangImage != textVm.ImageLanguage)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
if (oldLangImage2 != textVm.Image2Language)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage2))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage2);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage2, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.Image2Language))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.Image2Language);
|
|
var fileName = Path.GetFileName(textVm.Image2Language);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("Image2Language", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.Image2Language);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
if (oldImage != item.Image)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image);
|
|
var fileName = Path.GetFileName(item.Image);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
if (oldImage2 != item.Image2)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage2))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage2);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage2, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image2))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image2);
|
|
var fileName = Path.GetFileName(item.Image2);
|
|
var filenameToUse = FileServiceHelper.GetListingPath(item.Id) + $"ls_{Guid.NewGuid():N}{extension}";
|
|
item.Image2 = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image2);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
var listingLocation = await _geoLocationService.GetLocationAsync(item.ListingAddress, SelectedLanguage);
|
|
if (listingLocation.Success)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var geoLocation = geometryFactory.CreatePoint(new Coordinate(listingLocation.Longitude, listingLocation.Latitude));
|
|
item.ListingLocation = geoLocation;
|
|
}
|
|
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Changed"].Value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Deleted"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
ModelState.Remove("StateName");
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
ModelState.Remove("LisingCountryName");
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
ModelState.Remove("ListingStateName");
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
ModelState.Remove("CustomerName");
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
result.Html = await PartialView("_EditCustomer", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Details einer Listung für einen Kunden
|
|
/// </summary>
|
|
/// <param name="id">Id der Listung</param>
|
|
/// <param name="customerUniqueId">UniqueId des Kunden</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsRead)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> DetailsCustomer(string id, Guid customerUniqueId)
|
|
{
|
|
var item = await _listingService.GetAsync(id);
|
|
if (item != null && item.CustomerId == User.CustomerId())
|
|
{
|
|
var model = _mapper.Map<ListingCrudVm>(item);
|
|
|
|
model.TextVms = new List<ListingTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ListingTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true),
|
|
Description = item.Get("Description", language, true),
|
|
ImageLanguage = item.Get("ImageLanguage", language, true),
|
|
Image2Language = item.Get("Image2Language", language, true),
|
|
UrlLanguage = item.Get("UrlLanguage", language, true)
|
|
});
|
|
}
|
|
|
|
var country = _countryService.GetCountry(model.Address.CountryCode);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
var state = _countryService.GetState(model.Address.CountryCode, model.Address.State);
|
|
model.StateName = state != null ? state.Name : "";
|
|
|
|
var listingCountry = _countryService.GetCountry(model.ListingAddress.CountryCode);
|
|
model.ListingCountryName = listingCountry != null ? listingCountry.Name : "";
|
|
|
|
var listingState = _countryService.GetState(model.ListingAddress.CountryCode, model.ListingAddress.State);
|
|
model.ListingStateName = listingState != null ? listingState.Name : "";
|
|
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
var customer = await _customerService.GetAsync(model.CustomerId);
|
|
model.CustomerName = customer != null ? customer.Name : "";
|
|
|
|
return PartialView("_DetailsCustomer", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stornieren einer Listung
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <param name="customerUniqueId">UniqueId des Kunden</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
[HasPermission(Permission.ListingsManage, Permission.ListingsCancel)]
|
|
[CustomerAuthorize("customerUniqueId")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> CancelCustomer(List<string> ids, Guid customerUniqueId)
|
|
{
|
|
var batchErrorHeader = $"<p><strong>{_localizer["Common_BatchCancel_Failed"].Value}</strong></p>";
|
|
var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true };
|
|
|
|
//TODO: Stornierung genau überdenken--- BEstellung?
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var item = await _listingService.GetAsync(id);
|
|
if (item != null && item.CustomerId == User.CustomerId())
|
|
{
|
|
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
|
|
await _pinService.CancelByListingAsync(item.Id);
|
|
var usedCount = 0;
|
|
|
|
//var productsCount = await _productService.CountByAdvertisementCategoryAsync(item.Id);
|
|
//usedCount += productsCount;
|
|
|
|
if (usedCount == 0)
|
|
{
|
|
//TODO: Gibt es Resets die notwendig sind?
|
|
//await _productService.ResetAdvertisementCategoryAsync(item.Id, true);
|
|
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Name, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
item.Status = ListingStatus.Cancelled;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
}
|
|
else
|
|
{
|
|
if (usedCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Name, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchCancel_InUse"], $"<strong>{item.Name}</strong>", $"<strong>{usedCount}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchCancel_NotFound"], $"<strong>{id}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
if (batchResult.BatchResponseList.Any(c => c.Success))
|
|
await _listingService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|