コード例 #1
0
        public async Task HandleAsync(ResolveRemark command)
        {
            File file = null;

            await _handler.Validate(async() =>
            {
                if (command.ValidatePhoto)
                {
                    var resolvedFile = _fileResolver.FromBase64(command.Photo.Base64, command.Photo.Name, command.Photo.ContentType);
                    if (resolvedFile.HasNoValue)
                    {
                        Logger.Error($"File cannot be resolved from base64, photoName:{command.Photo.Name}, " +
                                     $"contentType:{command.Photo.ContentType}, userId:{command.UserId}");
                        throw new ServiceException(OperationCodes.CannotConvertFile);
                    }
                    file        = resolvedFile.Value;
                    var isImage = _fileValidator.IsImage(file);
                    if (isImage == false)
                    {
                        Logger.Warning($"File is not an image! name:{file.Name}, contentType:{file.ContentType}, " +
                                       $"userId:{command.UserId}");
                        throw new ServiceException(OperationCodes.InvalidFile);
                    }
                }
                var remark = await _remarkService.GetAsync(command.RemarkId);
                if (remark.Value.Group == null)
                {
                    return;
                }
                await _groupService.ValidateIfRemarkCanBeResolvedOrFailAsync(remark.Value.Group.Id, command.UserId);
            })
            .Run(async() =>
            {
                Location location = null;
                if (command.Latitude != 0 && command.Longitude != 0)
                {
                    location = Location.Create(command.Latitude, command.Longitude, command.Address);
                }
                await _remarkStateService.ResolveAsync(command.RemarkId, command.UserId, command.Description,
                                                       location, file, command.ValidateLocation);
            })
            .OnSuccess(async() =>
            {
                var remark   = await _remarkService.GetAsync(command.RemarkId);
                var state    = remark.Value.GetLatestStateOf(RemarkState.Names.Resolved).Value;
                var resource = _resourceFactory.Resolve <RemarkResolved>(command.RemarkId);
                await _bus.PublishAsync(new RemarkResolved(command.Request.Id, resource,
                                                           command.UserId, command.RemarkId));
            })
            .OnCustomError(async ex => await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id,
                                                                                         command.UserId, command.RemarkId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while resolving a remark.");
                await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id,
                                                                  command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
            })
            .ExecuteAsync();
        }
コード例 #2
0
        public async Task <IActionResult> Get(Guid id)
        {
            var remark = await _remarkService.GetAsync(id);

            var activities = await _remarkService.GetActivitiesAsync(id);

            var activityTypes = await _activityService.GetAsync();

            var comments = await _remarkService.GetCommentsAsync(id);

            var images = await _remarkService.GetImagessAsync(id);

            var category = await _categorySevice.GetAsync(remark.CategoryId);

            var dto = new RemarkDto
            {
                Id           = remark.Id,
                Name         = remark.Name,
                Description  = remark.Description,
                Latitude     = remark.Latitude,
                Longitude    = remark.Longitude,
                Author       = remark.AuthorId,
                CategoryId   = remark.CategoryId,
                CategoryName = category.Name,
                Status       = remark.Status.ToString(),
                Activities   = activities?
                               .Select(x => new ActivityDto
                {
                    Id   = x.Id,
                    Name = activityTypes.FirstOrDefault(y => y.Id == x.TypeId)?.Name
                })
                               .ToList(),
                Comments = comments
                           .Select(x => new CommentDto
                {
                    Id       = x.Id,
                    AuthorId = x.AuthorId,
                    Status   = x.Status.ToString(),
                    Text     = x.Text
                })
                           .ToList(),
                Images = images
                         .Select(x => new ImageDto
                {
                    Id   = x.Id,
                    Name = x.Name,
                    Url  = x.Url,
                })
                         .ToList()
            };

            return(Ok(dto));
        }
        public async Task HandleAsync(AddPhotosToRemark command)
        {
            await _handler
            .Validate(() =>
            {
                if (command.Photos == null || !command.Photos.Any())
                {
                    throw new ServiceException(OperationCodes.NoFiles,
                                               $"There are no photos to be added to the remark with id: '{command.RemarkId}'.");
                }
                if (command.Photos.Count() > _generalSettings.PhotosLimit)
                {
                    throw new ServiceException(OperationCodes.TooManyFiles);
                }
            })
            .Run(async() =>
            {
                var photos = new List <File>();
                foreach (var file in command.Photos)
                {
                    var resolvedFile = _fileResolver.FromBase64(file.Base64, file.Name, file.ContentType);
                    if (resolvedFile.HasNoValue)
                    {
                        throw new ServiceException(OperationCodes.CannotConvertFile);
                    }
                    var photo = resolvedFile.Value;
                    // var isImage = _fileValidator.IsImage(photo);
                    // if (!isImage)
                    // {

                    //     throw new ServiceException(OperationCodes.InvalidFile);
                    // }
                    photos.Add(photo);
                }
                await _remarkPhotoService.AddPhotosAsync(command.RemarkId, command.UserId, photos.ToArray());
            })
            .OnSuccess(async() =>
            {
                var remark   = await _remarkService.GetAsync(command.RemarkId);
                var resource = _resourceFactory.Resolve <PhotosToRemarkAdded>(command.RemarkId);
                await _bus.PublishAsync(new PhotosToRemarkAdded(command.Request.Id, resource,
                                                                command.UserId, command.RemarkId));
            })
            .OnCustomError(ex => _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id,
                                                                                 command.RemarkId, command.UserId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while adding photos to remark.");
                await _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id,
                                                                      command.RemarkId, command.UserId, OperationCodes.Error, ex.Message));
            })
            .ExecuteAsync();
        }
コード例 #4
0
 public async Task HandleAsync(RenewRemark command)
 {
     await _handler
     .Validate(async() =>
     {
         var remark = await _remarkService.GetAsync(command.RemarkId);
         if (remark.Value.Group == null)
         {
             return;
         }
         await _groupService.ValidateIfRemarkCanBeRenewedOrFailAsync(remark.Value.Group.Id, command.UserId);
     })
     .Run(async() =>
     {
         Location location = null;
         if (command.Latitude != 0 && command.Longitude != 0)
         {
             location = Location.Create(command.Latitude, command.Longitude, command.Address);
         }
         await _remarkStateService.RenewAsync(command.RemarkId, command.UserId, command.Description, location);
     })
     .OnSuccess(async() =>
     {
         var remark   = await _remarkService.GetAsync(command.RemarkId);
         var state    = remark.Value.GetLatestStateOf(RemarkState.Names.Renewed).Value;
         var resource = _resourceFactory.Resolve <RemarkRenewed>(command.RemarkId);
         await _bus.PublishAsync(new RemarkRenewed(command.Request.Id, resource,
                                                   command.UserId, command.RemarkId));
     })
     .OnCustomError(async ex => await _bus.PublishAsync(new RenewRemarkRejected(command.Request.Id,
                                                                                command.UserId, command.RemarkId, ex.Code, ex.Message)))
     .OnError(async(ex, logger) =>
     {
         logger.Error(ex, "Error occured while renewing a remark.");
         await _bus.PublishAsync(new RenewRemarkRejected(command.Request.Id,
                                                         command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
     })
     .ExecuteAsync();
 }
コード例 #5
0
        public async Task HandleAsync(DeleteRemark command)
        {
            await _handler
            .Validate(async() =>
            {
                var remark = await _remarkService.GetAsync(command.RemarkId);
                try
                {
                    await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId);

                    return;
                }
                catch
                {
                    if (remark.Value.Group == null)
                    {
                        throw;
                    }
                }
                await _groupService.ValidateIfRemarkCanBeCanceledOrFailAsync(remark.Value.Group.Id, command.UserId);
            })
            .Run(async() =>
            {
                await _remarkService.DeleteAsync(command.RemarkId);
            })
            .OnSuccess(async() =>
            {
                var resource = _resourceFactory.Resolve <RemarkDeleted>(command.RemarkId);
                await _bus.PublishAsync(new RemarkDeleted(command.Request.Id, resource,
                                                          command.UserId, command.RemarkId));
            })
            .OnCustomError(ex => _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id,
                                                                            command.RemarkId, command.UserId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while deleting a remark.");
                await _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id,
                                                                 command.RemarkId, command.UserId, OperationCodes.Error, ex.Message));
            })
            .ExecuteAsync();
        }
コード例 #6
0
 public async Task HandleAsync(EditRemark command)
 => await _handler
 .Validate(async() =>
           await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId))
 .Run(async() =>
 {
     Location location = null;
     if (command.Latitude.HasValue && command.Latitude != 0 &&
         command.Longitude.HasValue && command.Longitude != 0)
     {
         var locations = await _locationService.GetAsync(command.Latitude.Value, command.Longitude.Value);
         if (locations.HasNoValue || locations.Value.Results == null || !locations.Value.Results.Any())
         {
             throw new ServiceException(OperationCodes.AddressNotFound,
                                        $"Address was not found for remark with id: '{command.RemarkId}' " +
                                        $"latitude: {command.Latitude}, longitude:  {command.Longitude}.");
         }
         var address = locations.Value.Results.First().FormattedAddress;
         location    = Domain.Location.Create(command.Latitude.Value, command.Longitude.Value, address);
     }
     await _remarkService.EditAsync(command.RemarkId, command.UserId,
                                    command.GroupId, command.Category, command.Description, location);
 })
 .OnSuccess(async() =>
 {
     var remark   = await _remarkService.GetAsync(command.RemarkId);
     var resource = _resourceFactory.Resolve <RemarkEdited>(command.RemarkId);
     await _bus.PublishAsync(new RemarkEdited(command.Request.Id, resource,
                                              command.UserId, command.RemarkId));
 })
 .OnCustomError(async ex => await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id,
                                                                           command.UserId, command.RemarkId, ex.Code, ex.Message)))
 .OnError(async(ex, logger) =>
 {
     logger.Error(ex, "Error occured while editing a remark.");
     await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id,
                                                    command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
 })
 .ExecuteAsync();
コード例 #7
0
        public RemarkModule(IRemarkService remarkService, IMapper mapper) : base(mapper, "remarks")
        {
            Get("", async args => await FetchCollection <BrowseRemarks, Remark>
                    (async x => await remarkService.BrowseAsync(x))
                .MapTo <RemarkDto>()
                .HandleAsync());

            Get("categories", async args => await FetchCollection <BrowseCategories, Category>
                    (async x => await remarkService.BrowseCategoriesAsync(x))
                .MapTo <RemarkCategoryDto>()
                .HandleAsync());

            Get("tags", async args => await FetchCollection <BrowseTags, Tag>
                    (async x => await remarkService.BrowseTagsAsync(x))
                .MapTo <TagDto>()
                .HandleAsync());

            Get("{id}", async args => await Fetch <GetRemark, Remark>
                    (async x => await remarkService.GetAsync(x.Id))
                .MapTo <RemarkDto>()
                .HandleAsync());
        }
コード例 #8
0
        public async Task HandleAsync(ProcessRemark command)
        {
            var remarkProcessed = false;
            await _handler
            .Validate(async() =>
            {
                await _policy.ValidateAsync(command.RemarkId, command.UserId);
                var remark = await _remarkService.GetAsync(command.RemarkId);
                if (remark.Value.Group == null)
                {
                    return;
                }
                await _groupService.ValidateIfRemarkCanBeProcessedOrFailAsync(remark.Value.Group.Id, command.UserId);
            })
            .Run(async() =>
            {
                Location location = null;
                if (command.Latitude != 0 && command.Longitude != 0)
                {
                    location = Location.Create(command.Latitude, command.Longitude, command.Address);
                }
                await _remarkStateService.ProcessAsync(command.RemarkId, command.UserId, command.Description, location);
            })
            .OnSuccess(async() =>
            {
                remarkProcessed = true;
                var remark      = await _remarkService.GetAsync(command.RemarkId);
                var state       = remark.Value.GetLatestStateOf(RemarkState.Names.Processing).Value;
                var resource    = _resourceFactory.Resolve <RemarkProcessed>(command.RemarkId);
                await _bus.PublishAsync(new RemarkProcessed(command.Request.Id, resource,
                                                            command.UserId, command.RemarkId));
            })
            .OnCustomError(async ex => await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id,
                                                                                         command.UserId, command.RemarkId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while processing a remark.");
                await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id,
                                                                  command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
            })
            .Next()
            .Run(async() =>
            {
                if (!remarkProcessed)
                {
                    return;
                }

                var participant = await _remarkActionService.GetParticipantAsync(command.RemarkId, command.UserId);
                if (participant.HasValue)
                {
                    return;
                }
                var takeRemarkAction = new TakeRemarkAction
                {
                    Request     = Messages.Commands.Request.From <TakeRemarkAction>(command.Request),
                    UserId      = command.UserId,
                    RemarkId    = command.RemarkId,
                    Description = command.Description
                };
                await _bus.PublishAsync(takeRemarkAction);
            })
            .Next()
            .ExecuteAllAsync();
        }