public static DbGeoTask ToDbGeoTask(this GeoTask from) { if (from is null) { return(null); } var to = new DbGeoTask { CreatedAt = from.CreatedAt, Description = from.Description, CreatedById = from.CreatedBy?.Id, Id = from.Id, IsArchived = from.IsArchived, PlanFinishAt = from.PlanFinishAt, PlanStartAt = from.PlanStartAt, ProjectId = from.ProjectId, Status = from.Status, ResponsibleActorId = from.ResponsibleActor?.Id, StatusChangedAt = from.StatusChangedAt, Title = from.Title }; to.AssistentActorsIds.AddRange (from.AssistentActors.Select(x => x.Id)); to.GeosIds.AddRange(from.GeosIds); to.History.AddRange(from.History); to.ObserverActorsIds.AddRange (from.ObserverActors.Select(x => x.Id)); return(to); }
public static GeoTask ToGeoTask(this DbGeoTask from, Dictionary <string, Actor> actors) { actors.TryGetValue(from.CreatedById, out var creator); actors.TryGetValue(from.ResponsibleActorId, out var responsible); var assistents = from.AssistentActorsIds .Select(x => { actors.TryGetValue(x, out var actor); return(actor); }) .Where(x => x != null) .ToList(); var observers = from.ObserverActorsIds .Select(x => { actors.TryGetValue(x, out var actor); return(actor); }) .Where(x => x != null) .ToList(); var to = new GeoTask { CreatedAt = from.CreatedAt, Description = from.Description, Id = from.Id, IsArchived = from.IsArchived, PlanFinishAt = from.PlanFinishAt, PlanStartAt = from.PlanStartAt, ProjectId = from.ProjectId, Status = from.Status, StatusChangedAt = from.StatusChangedAt, Title = from.Title, CreatedBy = creator, ResponsibleActor = responsible, }; to.AssistentActors.AddRange(assistents); to.ObserverActors.AddRange(observers); to.History.AddRange(from.History); to.GeosIds.AddRange(from.GeosIds); return(to); }
public async Task <CreateResult> Handle (DbCreateCommand <GeoTask> request, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}", request.ToDictionary()); var validation = new DbCreateGeoTaskCommandValidator() .Validate(request); if (!validation.IsValid) { Logger.LogWarning (LogEvent.DatabaseRequestArgumentError, "Database request validation error. Error={Error}.", validation.Errors); return(ErrorResult("Database request validation error.")); } DbGeoTask dbEntity = request.Entity.ToDbGeoTask(); await DbContext.Db.GetCollection <DbGeoTask> (Defaults.TaskCollectionName) .InsertOneAsync(dbEntity) .ConfigureAwait(false); var newId = dbEntity?.Id; // Error while creating the entity if (String.IsNullOrEmpty(newId)) { return(ErrorResult("Unknown error.")); } return(new CreateResult(newId)); } catch (Exception e) { Logger.LogWarning(LogEvent.DatabaseExceptionError, e, "Database exception error. Error={Error}.", e.Message); return(ErrorResult("Database exception error")); } }
public async Task <UpdateResult> Handle (DbUpdateCommand <GeoTask> command, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}.", command.ToDictionary()); var validation = new DbUpdateGeoTaskCommandValidator() .Validate(command); if (!validation.IsValid) { Logger.LogWarning (LogEvent.DatabaseRequestArgumentError, "Database command validation error. Error={Error}.", validation.Errors); return(ErrorResult("Database command validation error.")); } DbGeoTask dbEntity = command.Entity.ToDbGeoTask(); var updated = await DbContext.Db.GetCollection <DbGeoTask> (Defaults.TaskCollectionName) .FindOneAndReplaceAsync(x => x.Id == dbEntity.Id, dbEntity).ConfigureAwait(false); return(new UpdateResult { Success = !String.IsNullOrEmpty(updated?.Id), }); } catch (Exception e) { Logger.LogWarning(LogEvent.DatabaseExceptionError, e, "Database exception error. Error={Error}.", e.Message); return(ErrorResult("Database exception error")); } }