public async Task Handle(RemoveClientServiceCommand message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var record = await _repository.Get(message.AnnouncementId);

            if (record == null || (record.Subject != message.Subject))
            {
                return;
            }

            if (!await _repository.Delete(record.Id))
            {
                return;
            }

            _eventPublisher.Publish(new ClientServiceRemovedEvent
            {
                AnnouncementId = message.AnnouncementId,
                Subject        = message.Subject,
                CommonId       = message.CommonId
            });
        }
        public async Task <IActionResult> Execute(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            var product = await _repository.Get(id);

            if (product == null)
            {
                return(new NotFoundResult());
            }

            _halResponseBuilder.AddLinks(l => l.AddSelf(GetAnnouncementLink(id)));
            _enricher.Enrich(_halResponseBuilder, product);
            return(new OkObjectResult(_halResponseBuilder.Build()));
        }
Esempio n. 3
0
        public async Task <RemoveClientServiceValidationResult> Validate(RemoveClientServiceCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var announcement = await _repository.Get(command.AnnouncementId);

            if (announcement == null)
            {
                return(new RemoveClientServiceValidationResult(ErrorDescriptions.TheAnnouncementDoesntExist));
            }

            if (announcement.Subject != command.Subject)
            {
                return(new RemoveClientServiceValidationResult(ErrorDescriptions.TheAnnouncementCannotBeRemovedByYou));
            }

            return(new RemoveClientServiceValidationResult());
        }
Esempio n. 4
0
        public async Task <AddMessageValidationResult> Validate(AddMessageCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (command.From == command.To)
            {
                return(new AddMessageValidationResult(ErrorDescriptions.TheMessageCannotBeSentToYourself));
            }

            if (string.IsNullOrWhiteSpace(command.ServiceId) && string.IsNullOrWhiteSpace(command.ProductId) &&
                string.IsNullOrWhiteSpace(command.To) && string.IsNullOrWhiteSpace(command.ClientServiceId))    // Check mandatories parameters.
            {
                return(new AddMessageValidationResult(string.Format(ErrorDescriptions.TheParametersAreMandatories,
                                                                    Constants.DtoNames.UserMessage.ServiceId + "," + Constants.DtoNames.UserMessage.ProductId + "," + Constants.DtoNames.UserMessage.To + "," + Constants.DtoNames.UserMessage.ClientServiceId)));
            }

            if (string.IsNullOrWhiteSpace(command.Content))
            {
                return(new AddMessageValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.UserMessage.Content)));
            }

            if (command.Content.Length > 255)
            {
                return(new AddMessageValidationResult(string.Format(ErrorDescriptions.TheParameterLengthCannotExceedNbCharacters, Constants.DtoNames.UserMessage.Content, "255")));
            }

            if ((!string.IsNullOrWhiteSpace(command.ProductId) && !string.IsNullOrWhiteSpace(command.ServiceId)) ||
                (!string.IsNullOrWhiteSpace(command.ProductId) && !string.IsNullOrWhiteSpace(command.ClientServiceId)) ||
                (!string.IsNullOrWhiteSpace(command.ServiceId) && !string.IsNullOrWhiteSpace(command.ClientServiceId))) // Check parameters are not specified at same time.
            {
                return(new AddMessageValidationResult(ErrorDescriptions.TheProductAndServiceCannotBeSpecifiedAtSameTime));
            }

            if (!string.IsNullOrWhiteSpace(command.ServiceId))
            {
                var service = await _serviceRepository.Get(command.ServiceId);

                if (service == null)
                {
                    return(new AddMessageValidationResult(ErrorDescriptions.TheServiceDoesntExist));
                }
            }

            if (!string.IsNullOrWhiteSpace(command.ProductId))
            {
                var product = await _productRepository.Get(command.ProductId);

                if (product == null)
                {
                    return(new AddMessageValidationResult(ErrorDescriptions.TheProductDoesntExist));
                }
            }

            if (!string.IsNullOrWhiteSpace(command.ClientServiceId))
            {
                var clientService = await _clientServiceRepository.Get(command.ClientServiceId);

                if (clientService == null)
                {
                    return(new AddMessageValidationResult(ErrorDescriptions.TheClientServiceDoesntExist));
                }
            }

            if (string.IsNullOrWhiteSpace(command.ParentId)) // Check messageSubject contains a correct subject.
            {
                if (string.IsNullOrWhiteSpace(command.Subject))
                {
                    return(new AddMessageValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.UserMessage.Subject)));
                }

                if (command.Subject.Length > 100)
                {
                    return(new AddMessageValidationResult(string.Format(ErrorDescriptions.TheParameterLengthCannotExceedNbCharacters, Constants.DtoNames.UserMessage.Subject, "100")));
                }
            }
            else
            {
                var parent = await _messageRepository.Get(command.ParentId);

                if (parent == null)
                {
                    return(new AddMessageValidationResult(ErrorDescriptions.TheParentMessageDoesntExist));
                }
            }

            return(new AddMessageValidationResult());
        }