/// <summary>
        /// Creates a new RhinoPageModelCollection under context.
        /// </summary>
        /// <param name="authentication">Authentication object by which to create RhinoPageModelCollection.</param>
        /// <param name="data">RhinoPageModelCollection data to create.</param>
        /// <returns>The <see cref="RhinoPageModelCollection.Id"/> of the newly created entity.</returns>
        public string Post(Authentication authentication, RhinoPageModelCollection data)
        {
            // validate
            CreateCollection(authentication);

            // get collection
            var collection = LiteDb.GetCollection <RhinoPageModelCollection>(name: Collection);

            // get models
            var namesToExclude = collection.FindAll().SelectMany(i => i.Models).Select(i => i.Name);

            data.Models = data.Models.Where(i => !namesToExclude.Contains(i.Name)).ToList();

            // insert
            if (data.Models.Count == 0)
            {
                return(string.Empty);
            }
            collection.Insert(entity: data);

            // exit conditions
            if (data.Configurations == null || data.Configurations.Count == 0)
            {
                return($"{data.Id}");
            }

            // cascade
            foreach (var configuration in data.Configurations)
            {
                ApplyToConfiguration(authentication, configuration, collection: data);
            }

            // response
            return($"{data.Id}");
        }
        private async Task <IActionResult> DoPost(string configuration)
        {
            // read test case from request body
            var models = await Request.ReadAsAsync <RhinoPageModel[]>().ConfigureAwait(false);

            // exit conditions
            if (models.Length == 0)
            {
                return(await this
                       .ErrorResultAsync("At least one model must be provided.", HttpStatusCode.BadRequest)
                       .ConfigureAwait(false));
            }
            if (!models.SelectMany(i => i.Entries).Any())
            {
                return(await this
                       .ErrorResultAsync("At least one model entry must be provided.", HttpStatusCode.BadRequest)
                       .ConfigureAwait(false));
            }

            // setup
            var collection = new RhinoPageModelCollection();

            collection.Configurations ??= new List <string>();
            collection.Models = models;
            if (!string.IsNullOrEmpty(configuration))
            {
                collection.Configurations.Add(configuration);
            }

            // get credentials
            var credentials = Request.GetAuthentication();

            // response
            var responseBody = new { Data = new { Id = repository.Post(credentials, collection) } };

            // exit conditions
            if (string.IsNullOrEmpty(responseBody.Data.Id))
            {
                return(await this
                       .ErrorResultAsync("All the provided Models Collections already exists. Please provide a unique Collection or clean old ones.")
                       .ConfigureAwait(false));
            }

            // results
            return(this.ContentResult(responseBody, HttpStatusCode.Created));
        }
示例#3
0
        // TODO: clean
        private async Task <IActionResult> DoPost(string configuration)
        {
            // read test case from request body
            var models = await Request.ReadAsAsync <RhinoPageModel[]>().ConfigureAwait(false);

            // exit conditions
            if (models.Length == 0)
            {
                return(GetErrorResults(message: "At least one model must be provided."));
            }
            if (!models.SelectMany(i => i.Entries).Any())
            {
                return(GetErrorResults(message: "At least one model entry must be provided."));
            }

            // setup
            var collection = new RhinoPageModelCollection();

            collection.Configurations ??= new List <string>();
            collection.Models = models;
            if (!string.IsNullOrEmpty(configuration))
            {
                collection.Configurations.Add(configuration);
            }

            // get credentials
            var credentials = Request.GetAuthentication();

            // response
            var data = new { Data = new { Id = repository.Post(credentials, collection) } };

            // exit conditions
            if (string.IsNullOrEmpty(data.Data.Id))
            {
                return(GetErrorResults(
                           message: "All the provided Models Collections already exists. Please provide a unique Collection."));
            }

            // results
            return(new ContentResult
            {
                Content = JsonConvert.SerializeObject(data, jsonSettings),
                ContentType = MediaTypeNames.Application.Json,
                StatusCode = HttpStatusCode.Created.ToInt32()
            });
        }