Exemplo n.º 1
0
        private void OkeyButton_Click(object sender, EventArgs e)
        {
            Dictionary <string, object> columns = new Dictionary <string, object>();

            columns["UserName"]               = UserNameTextBox.Text;
            columns["Name"]                   = NameTextBox.Text;
            columns["Description"]            = DescriptionTextBox.Text;
            columns["FriendsCount"]           = Convert.ToInt32(FollowingCountTextBox.Text);
            columns["FollowersCount"]         = Convert.ToInt32(FollowersCountTextBox.Text);
            columns["Language"]               = LanguageTextBox.Text;
            columns["Location"]               = LocationTextBox.Text;
            columns["Url"]                    = WebSiteTextBox.Text;
            columns["Protected"]              = ProtectedCheckBox.Checked;
            columns["ProfileImageUrl400x400"] = ProfileImagePictureBox.ImageLocation;
            columns["UserCategoryId"]         = GetComboBoxSelected <long>(UserCategoryComboBox);

            Dac          dac    = new Dac();
            MethodResult result = new MethodResult();

            if (this.showMode == ShowMode.Edit)
            {
                result = dac.Update("User", "Id", Convert.ToInt64(UserIdTextBox.Text), columns);
            }
            else if (this.showMode == ShowMode.Add)
            {
                columns["Id"] = Convert.ToInt64(UserIdTextBox.Text);
                result        = dac.Insert("User", columns);
            }
            else if (this.showMode == ShowMode.Delete)
            {
                columns["Id"] = Convert.ToInt64(UserIdTextBox.Text);
                result        = dac.Delete("User", "Id", Convert.ToInt64(UserIdTextBox.Text));
            }

            if (result.IsSuccessful)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Save Error :-(" + Environment.NewLine + result.Exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 2
0
        public static MethodResult <bool> SaveTweet(ITweet tweet)
        {
            MethodResult <bool> result = new MethodResult <bool>();

            /*
             *  CREATE TABLE `Tweet` (
             *          `TweetId`	NUMERIC NOT NULL UNIQUE,
             *          `FullText`	TEXT NOT NULL,
             *          `Text`	TEXT NOT NULL,
             *          `CreatedByUserId`	INTEGER NOT NULL,
             *          `RetweetCount`	INTEGER,
             *          `FavoriteCount`	INTEGER,
             *          `Language`	TEXT,
             *          `CreatedAt`	TEXT,
             *          `Source`	TEXT,
             *          `IsRetweet`	INTEGER,
             *          `RetweetedTweetId`	NUMERIC,
             *          PRIMARY KEY(`TweetId`)
             *  );
             *
             *  CREATE TABLE `TweetHashtag` (
             *          `TweetId`	NUMERIC,
             *          `Hashtag`	TEXT
             *  );
             *
             *
             *  CREATE TABLE `TweetMedia` (
             *          `TweetId`	NUMERIC,
             *          `Id`	NUMERIC,
             *          `Url`	TEXT,
             *          `MediaType`	TEXT
             *  );
             *
             *  CREATE TABLE `TweetMention` (
             *          `TweetId`	NUMERIC NOT NULL,
             *          `UserId`	NUMERIC,
             *          `Name`	TEXT,
             *          `ScreenName`	TEXT
             *  );
             *
             *  CREATE TABLE `TweetUrl` (
             *          `TweetId`	NUMERIC,
             *          `Url`	TEXT
             *  );
             */


            Dictionary <string, object> columns = new Dictionary <string, object>();

            columns["FullText"]         = tweet.FullText;
            columns["Text"]             = tweet.Text;
            columns["CreatedByUserId"]  = tweet.CreatedBy.Id;
            columns["CreatedAt"]        = tweet.CreatedAt;
            columns["RetweetCount"]     = tweet.RetweetCount;
            columns["FavoriteCount"]    = tweet.FavoriteCount;
            columns["Language"]         = tweet.Language.ToString();
            columns["Source"]           = tweet.Source;
            columns["IsRetweet"]        = tweet.IsRetweet;
            columns["RetweetedTweetId"] = (tweet.IsRetweet ? tweet.RetweetedTweet.Id : 0);


            MethodResult <bool> existsResult = IsTweetSaved(tweet);

            if (!existsResult.IsSuccessful)
            {
                result.Exception    = existsResult.Exception;
                result.IsSuccessful = false;
                return(result);
            }

            Dac dac = new Dac();

            if (!existsResult.Result)
            {
                columns["TweetId"] = tweet.Id;
                MethodResult resultTweet = dac.Insert("Tweet", columns);
                result.Result = true;

                if (!resultTweet.IsSuccessful)
                {
                    throw resultTweet.Exception;
                }

                foreach (var hashtag in tweet.Hashtags)
                {
                    Dictionary <string, object> hashtagColumns = new Dictionary <string, object>();
                    hashtagColumns.Add("TweetId", tweet.Id);
                    hashtagColumns.Add("Hashtag", hashtag.Text);

                    MethodResult resultHashtag = dac.Insert("TweetHashtag", hashtagColumns);

                    if (!resultHashtag.IsSuccessful)
                    {
                        result.Exception    = resultHashtag.Exception;
                        result.IsSuccessful = false;
                        return(result);
                    }
                }

                foreach (var media in tweet.Media)
                {
                    Dictionary <string, object> mediaColumns = new Dictionary <string, object>();
                    mediaColumns.Add("TweetId", tweet.Id);
                    mediaColumns.Add("Id", media.Id);
                    mediaColumns.Add("Url", media.MediaURL);
                    mediaColumns.Add("MediaType", media.MediaType);

                    MethodResult resulMedia = dac.Insert("TweetMedia", mediaColumns);

                    if (!resulMedia.IsSuccessful)
                    {
                        result.Exception    = resulMedia.Exception;
                        result.IsSuccessful = false;
                        return(result);
                    }
                }

                foreach (var mention in tweet.UserMentions)
                {
                    Dictionary <string, object> mentionColumns = new Dictionary <string, object>();
                    mentionColumns.Add("TweetId", tweet.Id);
                    mentionColumns.Add("UserId", mention.Id);
                    mentionColumns.Add("Name", mention.Name);
                    mentionColumns.Add("ScreenName", mention.ScreenName);

                    MethodResult resulMention = dac.Insert("TweetMention", mentionColumns);

                    if (!resulMention.IsSuccessful)
                    {
                        result.Exception    = resulMention.Exception;
                        result.IsSuccessful = false;
                        return(result);
                    }
                }

                foreach (var url in tweet.Urls)
                {
                    Dictionary <string, object> urlColumns = new Dictionary <string, object>();
                    urlColumns.Add("TweetId", tweet.Id);
                    urlColumns.Add("Url", url.URL);

                    MethodResult resultUrl = dac.Insert("TweetUrl", urlColumns);

                    if (!resultUrl.IsSuccessful)
                    {
                        result.Exception    = resultUrl.Exception;
                        result.IsSuccessful = false;
                        return(result);
                    }
                }
            }

            result.IsSuccessful = true;
            return(result);
        }