public static Dictionary <string, object> ToDictionary (this _ApiEntityType from) { if (from is null) { return(null); } return(new Dictionary <string, object> { { nameof(from.Layers), String.Join(',', from.Layers.Select(x => $"{{\"{nameof(x.GeoId)}\"={x.GeoId}," + $"\"{nameof(x.IsHidden)}\"={x.IsHidden}," + $"\"{nameof(x.Order)}\"={x.Order}}}")) }, { nameof(from.Description), from.Description }, { nameof(from.IsArchived), from.IsArchived }, { nameof(from.IsMap), from.IsMap }, { nameof(from.MapParameters), String.Join(',', from.MapParameters.Select(x => $"{{\"{x.Key}\"={x.Value}}}")) }, { nameof(from.MapProvider), from.MapProvider }, { nameof(from.ProjectActorRoles), String.Join(',', from.ProjectActorRoles.Select(x => $"{{\"{x.Key}\"={x.Value}}}")) }, { nameof(from.ShowMap), from.ShowMap }, { nameof(from.Title), from.Title }, }); }
public async Task <IActionResult> Update([FromRoute] string id, [FromBody] _ApiEntityUpdateCommandType command) { try { var currentPrincipal = HttpContext.User; var currentUserName = currentPrincipal?.Identity?.Name; using var logScope = Logger.BeginScope("{User}", currentUserName); Logger.LogInformation(ApiLogEvent.ApiRequest, "Project update command. Id={Id}. Command={Command}", id, command.ToDictionary()); if (command is null || String.IsNullOrWhiteSpace(id)) { Logger.LogWarning(ApiLogEvent.ApiArgumentError, "Project update empty argument error."); return(BadRequest()); } var appCommand = command.ToEntity <_EntityUpdateCommandType>(id, currentPrincipal); var result = await Mediator.Send(appCommand); if (result is null || !result.Success) { Logger.LogWarning(ApiLogEvent.ApiErrorResponse, "Project update error response. Error={Error}.", result?.Errors); return(BadRequest()); } return(Ok()); } catch (Exception ex) when(Logger.WriteScopeWhenException (ApiLogEvent.ApiErrorResponse, ex)) { return(BadRequest()); } }
public static _EntityType ToEntity <TEntity> (this _ApiEntityType from, string id, ClaimsPrincipal currentPrincipal) where TEntity : _EntityType { if (from is null) { return(null); } var to = new _EntityType { Description = from.Description, Id = id, Title = from.Title, CurrentPrincipal = currentPrincipal, IsArchived = from.IsArchived, IsMap = from.IsMap, MapProvider = from.MapProvider, ShowMap = from.ShowMap, }; to.Layers.AddRange(from.Layers); from.MapParameters.ToList() .ForEach(x => to.MapParameters.TryAdd(x.Key, x.Value)); var allRoles = EnumerationClass.GetAll <ActorRole>(); foreach (var item in from.ProjectActorRoles) { var role = allRoles.FirstOrDefault(x => x.Id == item.Value); if (role != null) { to.ProjectActorRoles.TryAdd(item.Key, role); } } return(to); }