public static Dictionary <string, object> ToDictionary <TEntity> (this DbGetEntityByIdRequest <TEntity> from) where TEntity : class { if (from is null) { return(null); } return(new Dictionary <string, object> { { nameof(from.Id), from.Id } }); }
public async Task <EntityResponse <Geo> > Handle (DbGetEntityByIdRequest <Geo> request, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}.", request.ToDictionary()); var validation = new DbGetEntityByIdRequestValidator <Geo>() .Validate(request); if (!validation.IsValid) { Logger.LogWarning (LogEvent.DatabaseRequestArgumentError, "Request validation error. Error={Error}.", validation.Errors); return(ErrorResult("Request validation error")); } var db = DbContext.Db; var dbGeoCollection = db.GetCollection <DbGeo> (Defaults.GeoCollectionName).AsQueryable(); var dbEntity = await dbGeoCollection .Where(t => t.Id == request.Id) .FirstOrDefaultAsync() .ConfigureAwait(false); if (dbEntity is null) { Logger.LogWarning(LogEvent.DatabaseEmptyResponse, "Database null response."); return(ErrorResult("Database null response")); } var entity = dbEntity.ToEntity <Geo>(); return(new EntityResponse <Geo>(entity)); } catch (Exception e) { Logger.LogWarning(LogEvent.DatabaseExceptionError, e, "Database exception error. Error={Error}.", e.Message); return(ErrorResult("Database exception error")); } }
public async Task <EntityResponse <GeoTask> > Handle (DbGetEntityByIdRequest <GeoTask> request, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}.", request.ToDictionary()); var validation = new DbGetEntityByIdRequestValidator <GeoTask>() .Validate(request); if (!validation.IsValid) { Logger.LogWarning (LogEvent.DatabaseRequestArgumentError, "Database request validation error. Error={Error}.", validation.Errors); return(ErrorResult("Database request validation error")); } var db = DbContext.Db; var dbGeoTaskCollection = db.GetCollection <DbGeoTask> (Defaults.TaskCollectionName).AsQueryable(); var actorCollection = db.GetCollection <Actor> (Defaults.ActorCollectionName).AsQueryable(); var dbEntity = await dbGeoTaskCollection .Where(t => t.Id == request.Id) .FirstOrDefaultAsync() .ConfigureAwait(false); if (dbEntity is null) { Logger.LogWarning(LogEvent.DatabaseEmptyResponse, "Database GeoTasks null response."); return(ErrorResult("Database GeoTasks null response")); } var actorIdSet = new HashSet <string>(dbEntity.AssistentActorsIds); dbEntity.ObserverActorsIds.ForEach(x => actorIdSet.Add(x)); actorIdSet.Add(dbEntity.CreatedById); actorIdSet.Add(dbEntity.ResponsibleActorId); var actorResponse = await Mediator .Send(new DbListRequest <Actor>(actorIdSet)) .ConfigureAwait(false); if (!actorResponse.Success) { Logger.LogWarning(LogEvent.DatabaseEmptyResponse, "Database Actors null response."); return(ErrorResult("Database Actors null response.")); } var actors = actorResponse.Entities.ToDictionary(x => x.Id); var geoTask = dbEntity.ToGeoTask(actors); return(new EntityResponse <GeoTask>(geoTask)); } catch (Exception e) { Logger.LogWarning(LogEvent.DatabaseExceptionError, e, "Database exception error. Error={Error}.", e.Message); return(ErrorResult("Database exception error")); } }