/// <summary>
 ///     Create a entity
 /// </summary>
 /// <param name="manager"></param>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static ValidatorResult Create <TEntity, TKey>(this IEntityManager <TEntity, TKey> manager, TEntity entity)
     where TKey : IEquatable <TKey>
     where TEntity : class, IEntity <TKey>
 {
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     return(AsyncHelper.RunSync(() => manager.CreateAsync(entity)));
 }
Exemple #2
0
        public async Task <ICommandResult <Article> > CreateAsync(Article model)
        {
            if (model.FeatureId == 0)
            {
                var feature = await _featureFacade.GetFeatureByIdAsync("Plato.Articles");

                if (feature != null)
                {
                    model.FeatureId = feature.Id;
                }
            }

            return(await _entityManager.CreateAsync(model));
        }
Exemple #3
0
        public async Task <ICommandResult <Doc> > CreateAsync(Doc model)
        {
            if (model.FeatureId == 0)
            {
                var feature = await _featureFacade.GetFeatureByIdAsync("Plato.Docs");

                if (feature != null)
                {
                    model.FeatureId = feature.Id;
                }
            }

            var result = await _entityManager.CreateAsync(model);

            if (result.Succeeded)
            {
                // Expire simple entity cache for docs
                _simpleEntityStore.CancelTokens(null);
            }

            return(result);
        }
Exemple #4
0
        async Task <ICommandResultBase> InstallEntityInternalAsync(SampleDataDescriptor descriptor, IList <User> users, int sortOrder = 0)
        {
            // Validate

            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (string.IsNullOrEmpty(descriptor.ModuleId))
            {
                throw new ArgumentNullException(nameof(descriptor.ModuleId));
            }

            // Our result
            var result = new CommandResultBase();

            // Ensure the feature is enabled
            var feature = await _featureFacade.GetFeatureByIdAsync(descriptor.ModuleId);

            if (feature == null)
            {
                return(result.Failed($"The feature {descriptor.ModuleId} is not enabled!"));
            }

            // Get a random user for the post
            var randomUser = users[_random.Next(0, users.Count)];

            // Capitalize the first character of our entity type
            var entityTypeCapitalized = char.ToUpper(descriptor.EntityType[0]).ToString() + descriptor.EntityType.Substring(1);

            // Create the post
            var entity = new Entity()
            {
                Title         = $"Example {entityTypeCapitalized} {_random.Next(0, 2000).ToString()}",
                Message       = GetEntityText(descriptor),
                FeatureId     = feature?.Id ?? 0,
                SortOrder     = sortOrder + 1,
                CreatedUserId = randomUser?.Id ?? 0,
                CreatedDate   = DateTimeOffset.UtcNow
            };

            // Create entity
            var entityResult = await _entityManager.CreateAsync(entity);

            if (entityResult.Succeeded)
            {
                var lastReplyId       = string.Empty;
                var lastReplyUserName = string.Empty;
                var lastReplyMessage  = string.Empty;

                // Create entity replies
                for (var i = 0; i < descriptor.EntityRepliesToCreate; i++)
                {
                    randomUser = users[_random.Next(0, users.Count - 1)];

                    var message = GetReplyText(descriptor);

                    message = message.Replace("{replyUserName}", randomUser?.UserName ?? "");

                    message = message.Replace("{lastReplyId}", lastReplyId ?? "");
                    message = message.Replace("{lastReplyMessage}", lastReplyMessage ?? "");
                    message = message.Replace("{lastReplyQuotedMessage}", lastReplyMessage.Replace(System.Environment.NewLine, System.Environment.NewLine + "> ") ?? "");
                    message = message.Replace("{lastReplyUserName}", lastReplyUserName ?? "");

                    message = message.Replace("{entityId}", entityResult.Response.Id.ToString() ?? "");
                    message = message.Replace("{entityTitle}", entityResult.Response.Title ?? "");
                    message = message.Replace("{entityUserName}", entityResult.Response.CreatedBy.UserName);

                    message = message.Replace("{mentionSample}", BuildMentionSampleMarkUp());

                    message = message.Replace("{lastReplyUrl}", _contextFacade.GetRouteUrl(new RouteValueDictionary()
                    {
                        ["area"]         = descriptor.ModuleId,
                        ["controller"]   = "Home",
                        ["action"]       = "Reply",
                        ["opts.id"]      = entityResult.Response.Id.ToString() ?? "",
                        ["opts.alias"]   = entityResult.Response.Alias.ToString() ?? "",
                        ["opts.replyId"] = lastReplyId ?? ""
                    }));

                    // Create reply
                    var reply = new EntityReply()
                    {
                        EntityId      = entityResult.Response.Id,
                        Message       = message,
                        CreatedUserId = randomUser?.Id ?? 0,
                        CreatedDate   = DateTimeOffset.UtcNow
                    };

                    // Add reply
                    var replyResult = await _entityReplyManager.CreateAsync(reply);

                    if (!replyResult.Succeeded)
                    {
                        return(result.Failed());
                    }

                    lastReplyId       = replyResult.Response.Id.ToString();
                    lastReplyMessage  = replyResult.Response.Message;
                    lastReplyUserName = replyResult.Response.CreatedBy.UserName;
                }
            }
            else
            {
                return(result.Failed(result.Errors.ToArray()));
            }

            return(result.Success());
        }