/// <inheritdoc />
        public override void DeleteItem(string deleteStatement, ColumnValue extid)
        {
            // Retrieve the id of the item.
            int itemId;

            try
            {
                itemId = Int32.Parse(extid.Value as string);
            }
            catch
            {
                throw new ArgumentException("The external Id provided could not be converted to a valid integer", nameof(extid));
            }

            // Create our repository which we will load the data provider from.
            var repository = new DataProviderRepository();

            // Which data type did the connection string say?
            var type = this.DataSource.GetElementType();

            switch (type)
            {
            // Posts?
            case "post":
            case "posts":

                // Delete the post.
                repository
                .GetDataProvider <Post>()
                .Delete(itemId);

                break;

            // Users?
            case "user":
            case "users":

                // Delete the user.
                repository
                .GetDataProvider <User>()
                .Delete(itemId);

                break;

            // Comments?
            case "comment":
            case "comments":

                // Delete the comment.
                repository
                .GetDataProvider <Comment>()
                .Delete(itemId);

                break;

            default:
                // Type name not recognised.
                throw new InvalidOperationException($"The type value ({type}) is not supported.");
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public override void UpdateItem(string updateStatement, string extid, IList <ColumnValue> values, IList <ColumnValue> previousValues)
        {
            // Retrieve the id of the item.
            int itemId;

            try
            {
                itemId = Int32.Parse(extid);
            }
            catch
            {
                throw new ArgumentException("The external Id provided could not be converted to a valid integer", nameof(extid));
            }

            // Create our repository which we will load the data provider from.
            var repository = new DataProviderRepository();

            // Which data type did the connection string say?
            var type = this.DataSource.GetElementType();

            switch (type)
            {
            // Posts?
            case "post":
            case "posts":

                // Create the post and pull the data from the values provided.
                var post = new Post
                {
                    Id   = itemId,
                    Body = values
                           .Where(v => v.Definition.Name == nameof(Post.Body))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Title = values
                            .Where(v => v.Definition.Name == nameof(Post.Title))
                            .Select(v => v.Value as string)
                            .FirstOrDefault() ?? "",
                    UserId = values
                             .Where(v => v.Definition.Name == nameof(Post.UserId))
                             .Select(v => string.IsNullOrWhiteSpace(v.Value as string) ? (int?)null : Int32.Parse(v.Value as string))
                             .FirstOrDefault() ?? 0
                };

                // Update the post.
                repository
                .GetDataProvider <Post>()
                .Update(itemId, post);

                break;

            // Users?
            case "user":
            case "users":

                // Create the user and pull the data from the values provided.
                var user = new User
                {
                    Id           = itemId,
                    EmailAddress = values
                                   .Where(v => v.Definition.Name == nameof(User.EmailAddress))
                                   .Select(v => v.Value as string)
                                   .FirstOrDefault() ?? "",
                    Name = values
                           .Where(v => v.Definition.Name == nameof(User.Name))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Username = values
                               .Where(v => v.Definition.Name == nameof(User.Username))
                               .Select(v => v.Value as string)
                               .FirstOrDefault() ?? "",
                    PhoneNumber = values
                                  .Where(v => v.Definition.Name == nameof(User.PhoneNumber))
                                  .Select(v => v.Value as string)
                                  .FirstOrDefault() ?? "",
                    Website = values
                              .Where(v => v.Definition.Name == nameof(User.Website))
                              .Select(v => v.Value as string)
                              .FirstOrDefault() ?? ""
                };

                // Update the user
                repository
                .GetDataProvider <User>()
                .Update(itemId, user);

                break;

            // Comments?
            case "comment":
            case "comments":

                // Create the comment and pull the data from the values provided.
                var comment = new Comment
                {
                    Id   = itemId,
                    Body = values
                           .Where(v => v.Definition.Name == nameof(Comment.Body))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Name = values
                           .Where(v => v.Definition.Name == nameof(Comment.Name))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    EmailAddress = values
                                   .Where(v => v.Definition.Name == nameof(Comment.EmailAddress))
                                   .Select(v => v.Value as string)
                                   .FirstOrDefault() ?? "",
                    PostId = values
                             .Where(v => v.Definition.Name == nameof(Comment.PostId))
                             .Select(v => string.IsNullOrWhiteSpace(v.Value as string) ? (int?)null : Int32.Parse(v.Value as string))
                             .FirstOrDefault() ?? 0
                };

                // Update the comment.
                repository
                .GetDataProvider <Comment>()
                .Update(itemId, comment);

                break;

            default:
                // Type name not recognised.
                throw new InvalidOperationException($"The type value ({type}) is not supported.");
            }
        }
        /// <inheritdoc />
        public override string InsertItem(string insertStatement, IList <ColumnValue> values)
        {
            // Create a variable to hold our new item Id.
            string newItemId = null;

            // Create our repository which we will load the data provider from.
            var repository = new DataProviderRepository();

            // Which data type did the connection string say?
            var type = this.DataSource.GetElementType();

            switch (type)
            {
            // Posts?
            case "post":
            case "posts":

                // Create the post and pull the data from the values provided.
                var post = new Post
                {
                    Body = values
                           .Where(v => v.Definition.Name == nameof(Post.Body))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Title = values
                            .Where(v => v.Definition.Name == nameof(Post.Title))
                            .Select(v => v.Value as string)
                            .FirstOrDefault() ?? "",
                    UserId = values
                             .Where(v => v.Definition.Name == nameof(Post.UserId))
                             .Select(v => string.IsNullOrWhiteSpace(v.Value as string) ? (int?)null : Int32.Parse(v.Value as string))
                             .FirstOrDefault() ?? 0
                };

                // Insert the item and save the id.
                newItemId = repository
                            .GetDataProvider <Post>()
                            .Insert(post)
                            .ToString();

                break;

            // Users?
            case "user":
            case "users":

                // Create the user and pull the data from the values provided.
                var user = new User
                {
                    EmailAddress = values
                                   .Where(v => v.Definition.Name == nameof(User.EmailAddress))
                                   .Select(v => v.Value as string)
                                   .FirstOrDefault() ?? "",
                    Name = values
                           .Where(v => v.Definition.Name == nameof(User.Name))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Username = values
                               .Where(v => v.Definition.Name == nameof(User.Username))
                               .Select(v => v.Value as string)
                               .FirstOrDefault() ?? "",
                    PhoneNumber = values
                                  .Where(v => v.Definition.Name == nameof(User.PhoneNumber))
                                  .Select(v => v.Value as string)
                                  .FirstOrDefault() ?? "",
                    Website = values
                              .Where(v => v.Definition.Name == nameof(User.Website))
                              .Select(v => v.Value as string)
                              .FirstOrDefault() ?? ""
                };

                // Insert the item and save the id.
                newItemId = repository
                            .GetDataProvider <User>()
                            .Insert(user)
                            .ToString();

                break;

            // Comments?
            case "comment":
            case "comments":

                // Create the comment and pull the data from the values provided.
                var comment = new Comment
                {
                    Body = values
                           .Where(v => v.Definition.Name == nameof(Comment.Body))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    Name = values
                           .Where(v => v.Definition.Name == nameof(Comment.Name))
                           .Select(v => v.Value as string)
                           .FirstOrDefault() ?? "",
                    EmailAddress = values
                                   .Where(v => v.Definition.Name == nameof(Comment.EmailAddress))
                                   .Select(v => v.Value as string)
                                   .FirstOrDefault() ?? "",
                    PostId = values
                             .Where(v => v.Definition.Name == nameof(Comment.PostId))
                             .Select(v => string.IsNullOrWhiteSpace(v.Value as string) ? (int?)null : Int32.Parse(v.Value as string))
                             .FirstOrDefault() ?? 0
                };

                // Insert the item and save the id.
                newItemId = repository
                            .GetDataProvider <Comment>()
                            .Insert(comment)
                            .ToString();

                break;

            default:
                // Type name not recognised.
                throw new InvalidOperationException($"The type value ({type}) is not supported.");
            }

            // Return the id of the new item.
            return(newItemId);
        }
        /// <inheritdoc />
        public override IEnumerable <DataItem> GetItems()
        {
            // Create our repository which we will load the data provider from.
            var repository = new DataProviderRepository();

            // Which data type did the connection string say?
            var type = this.DataSource.GetElementType();

            switch (type)
            {
            // Posts?
            case "post":
            case "posts":

                // Load all posts from the data provider.
                foreach (var item in repository
                         .GetDataProvider <Post>()
                         .GetAll())
                {
                    // Iterate over the column definitions we expect and produce a dictionary of
                    // properties for provision to the M-Files client.
                    var properties = new Dictionary <int, object>();
                    foreach (var column in this.GetAvailableColumns())
                    {
                        // Retrieve the value from the object via reflection.
                        properties.Add(column.Ordinal, this.GetValue(item, column.Name));
                    }

                    // Return the specific object in the JSON file, with its populated set of properties.
                    yield return(new DataItemSimple(properties));
                }

                break;

            // Users?
            case "user":
            case "users":

                // Load all posts from the data provider.
                foreach (var item in repository
                         .GetDataProvider <User>()
                         .GetAll())
                {
                    // Iterate over the column definitions we expect and produce a dictionary of
                    // properties for provision to the M-Files client.
                    var properties = new Dictionary <int, object>();
                    foreach (var column in this.GetAvailableColumns())
                    {
                        // Retrieve the value from the object via reflection.
                        properties.Add(column.Ordinal, this.GetValue(item, column.Name));
                    }

                    // Return the specific object in the JSON file, with its populated set of properties.
                    yield return(new DataItemSimple(properties));
                }

                break;

            // Comments?
            case "comment":
            case "comments":

                // Load all posts from the data provider.
                foreach (var item in repository
                         .GetDataProvider <Comment>()
                         .GetAll())
                {
                    // Iterate over the column definitions we expect and produce a dictionary of
                    // properties for provision to the M-Files client.
                    var properties = new Dictionary <int, object>();
                    foreach (var column in this.GetAvailableColumns())
                    {
                        // Retrieve the value from the object via reflection.
                        properties.Add(column.Ordinal, this.GetValue(item, column.Name));
                    }

                    // Return the specific object in the JSON file, with its populated set of properties.
                    yield return(new DataItemSimple(properties));
                }

                break;

            default:
                // Type name not recognised.
                throw new InvalidOperationException($"The type value ({type}) is not supported.");
            }
        }