private async void indexNewPublicationsToolStripMenuItem_Click(object sender, EventArgs e) { if (tableNameLable.Text.Equals("Publications")) { List <Model.Shop> shops = dbContext.Shops.ToList(); foreach (Model.Shop shop in shops) { var userMedias = await instaApi.UserProcessor .GetUserMediaAsync(shop.Name, PaginationParameters.MaxPagesToLoad(100)); // Max posts to load var userPosts = userMedias.Value; foreach (var post in userPosts) { if (dbContext.Publications.FirstOrDefault(p => p.Publication_Id.Equals(post.Pk)) == null) { Model.Publication publicationToAdd = new Model.Publication { Publication_Id = post.Pk, Publication_Images = new List <Model.Publication_Image>(), Publication_Videos = new List <Model.Publication_Video>(), Date = post.TakenAt, Shop_Id = shop.Shop_Id }; if (post.MediaType == InstagramApiSharp.Classes.Models.InstaMediaType.Carousel) { foreach (var media in post.Carousel) { if (media.MediaType == InstagramApiSharp.Classes.Models.InstaMediaType.Image) { publicationToAdd.Publication_Images.Add(new Model.Publication_Image { Path = media.Images[0].Uri, Publication_Id = publicationToAdd.Publication_Id }); } else if (media.MediaType == InstagramApiSharp.Classes.Models.InstaMediaType.Video) { publicationToAdd.Publication_Videos.Add(new Model.Publication_Video { Path = media.Videos[0].Uri, Publication_Id = publicationToAdd.Publication_Id }); } } } else if (post.MediaType == InstagramApiSharp.Classes.Models.InstaMediaType.Image) { publicationToAdd.Publication_Images.Add(new Model.Publication_Image { Path = post.Images[0].Uri, Publication_Id = publicationToAdd.Publication_Id }); } else if (post.MediaType == InstagramApiSharp.Classes.Models.InstaMediaType.Video) { publicationToAdd.Publication_Videos.Add(new Model.Publication_Video { Path = post.Videos[0].Uri, Publication_Id = publicationToAdd.Publication_Id }); } dbContext.Publications.Add(publicationToAdd); } else { continue; } } dbContext.SaveChanges(); } } }
private void editToolStripMenuItem_Click(object sender, EventArgs e) { if (tableNameLable.Text.Equals("Goods")) { Model.Good good = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Good)mainFormGridView.SelectedRows[0].DataBoundItem; if (good == null) { MessageBox.Show("You should to select at least one row"); } else { GoodSubForm goodSubForm = new GoodSubForm(ref dbContext, ref good, false); goodSubForm.Show(); } } else if (tableNameLable.Text.Equals("Dimensions")) { Model.Dimension dimension = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Dimension)mainFormGridView.SelectedRows[0].DataBoundItem; if (dimension == null) { MessageBox.Show("You should to select at least one row"); } else { DimensionSubForm dimensionSubForm = new DimensionSubForm(ref dbContext, ref dimension, false); dimensionSubForm.Show(); } } else if (tableNameLable.Text.Equals("Vendor codes")) { Model.Good_Dimension good_dimension = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Good_Dimension)mainFormGridView.SelectedRows[0].DataBoundItem; if (good_dimension == null) { MessageBox.Show("You should to select at least one row"); } else { GoodDimensionEditSubForm good_dimensionSubForm = new GoodDimensionEditSubForm(ref dbContext, ref good_dimension); good_dimensionSubForm.Show(); } } else if (tableNameLable.Text.Equals("Publications")) { Model.Publication publication = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Publication)mainFormGridView.SelectedRows[0].DataBoundItem; if (publication == null) { MessageBox.Show("You should to select at least one row"); } else { PublicationSubForm publicationSubForm = new PublicationSubForm(ref dbContext, ref publication); publicationSubForm.Show(); } } else if (tableNameLable.Text.Equals("Deliveries")) { Model.Delivery delivery = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Delivery)mainFormGridView.SelectedRows[0].DataBoundItem; if (delivery == null) { MessageBox.Show("You should to select at least one row"); } else { DeliverySubForm deliverySubForm = new DeliverySubForm(ref dbContext, ref delivery, false); deliverySubForm.Show(); } } else if (tableNameLable.Text.Equals("Orders")) { Model.Order order = mainFormGridView.SelectedRows.Count == 0 ? null : (Model.Order)mainFormGridView.SelectedRows[0].DataBoundItem; if (order == null) { MessageBox.Show("You should to select at least one row"); } else { OrderSubForm orderSubForm = new OrderSubForm(ref dbContext, ref order, false); orderSubForm.Show(); } } }
public async void CheckInbox() { var inbox = await instaApi.MessagingProcessor .GetDirectInboxAsync(PaginationParameters.MaxPagesToLoad(50)); if (inbox.Succeeded && inbox.Value.Inbox.UnseenCount != 0) { foreach (InstaDirectInboxThread chat in inbox.Value.Inbox.Threads.Where(t => t.HasUnreadMessage)) { List <InstaDirectInboxItem> messagesToWork = new List <InstaDirectInboxItem>(); var threadWithMessages = await instaApi.MessagingProcessor .GetDirectInboxThreadAsync(chat.ThreadId, PaginationParameters.MaxPagesToLoad(1)); if (threadWithMessages.Succeeded) { var myId = instaApi.GetLoggedUser().LoggedInUser.Pk; threadWithMessages.Value.Items.Reverse(); messagesToWork = threadWithMessages.Value.Items.Where(m => m.UserId != myId).Take(1).ToList(); } foreach (InstaDirectInboxItem item in messagesToWork) { if (dbContext.Chats.FirstOrDefault(c => c.Chat_Id == chat.ThreadId) == null) { dbContext.Chats.Add(new Model.Chat { Chat_Id = chat.ThreadId, User_Id = item.UserId, Chat_Status_Id = 1 }); dbContext.SaveChanges(); } if (dbContext.Users.FirstOrDefault(u => u.User_Id == item.UserId) == null) { dbContext.Users.Add(new Model.User { User_Id = item.UserId, User_Name = chat.Title }); dbContext.SaveChanges(); } if (item.ItemType == InstaDirectThreadItemType.MediaShare) { Model.Publication tp = dbContext.Publications.FirstOrDefault(p => p.Publication_Id == item.MediaShare.Pk); List <Model.Good_Dimension> good_Dimensions = new List <Model.Good_Dimension>(); if (tp.Good_Dimension_Publications != null) { good_Dimensions = new List <Model.Good_Dimension>(tp.Good_Dimension_Publications.Select(gdp => gdp.Good_Dimension)); } string text = "Vendor codes in this post\n"; foreach (Model.Good_Dimension gd in good_Dimensions) { text += gd + "\nAvailable count: " + gd.Available + "\nPrice: " + gd.Price + "\nID: " + gd.Good_Dimension_Id + "\n"; } text += "\n\n To make an order write \n/startOrder"; await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, text); } else if (item.ItemType == InstaDirectThreadItemType.Text || item.ItemType == InstaDirectThreadItemType.Link) { await instaApi.MessagingProcessor.MarkDirectThreadAsSeenAsync(chat.ThreadId, item.ItemId); if (item.Text.Trim().ToLower().Equals("/startorder")) { string text = "Available delivery places\n"; List <Model.Delivery> dels = dbContext.Deliveries.OrderBy(d => d.City).ToList(); foreach (Model.Delivery del in dbContext.Deliveries) { text += del.ToString() + "\n DeliveryID: " + del.Delivery_Id + "\n\n"; } text += "\n\nTo make an order write the message in format:\n\nLastname,Firstname,Phone,DeliveryId,Email***Vendor codes that you would like to buy with count through dash, (separated by comma);\n\nExample:\n\nIvanov,Ivan,+380998765432,1,[email protected]***1-7,6-1,32-1,2-3\n\nTo submit your order write\n/stopOrder"; await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, text); dbContext.Chats.Find(chat.ThreadId).Chat_Status_Id = 2; dbContext.SaveChanges(); } else if (item.Text.Trim().ToLower().Equals("/stoporder")) { await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, "Order creating is stopped"); dbContext.Chats.Find(chat.ThreadId).Chat_Status_Id = 0; dbContext.SaveChanges(); } else if (dbContext.Chats.Find(chat.ThreadId).Chat_Status_Id == 1) { dbContext.Chats.Find(chat.ThreadId).Chat_Status_Id = 0; } else if (dbContext.Chats.Find(chat.ThreadId) != null && dbContext.Chats.Find(chat.ThreadId).Chat_Status_Id == 2) { TextOrder textOrder = new TextOrder(item.Text, dbContext); if (textOrder.TryToParse()) { textOrder.Order.User_Id = item.UserId; textOrder.Order.Order_Status_Id = 1; string notEnoughText = ""; foreach (Model.Receipt r in textOrder.Order.Receipts) { if (dbContext.Good_Dimensions.Find(r.Good_Dimension_Id).Available < r.Amount) { notEnoughText += "Not enough " + r.Good_Dimension + "\n" + " Available: " + r.Good_Dimension.Available + "\n In order: " + r.Amount; } else { dbContext.Good_Dimensions.Find(r.Good_Dimension_Id).Available -= r.Amount; } } if (notEnoughText.Length > 0) { await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, notEnoughText); } else { dbContext.Orders.Add(textOrder.Order); dbContext.SaveChanges(); await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, "Order is created!\nFull price: " + textOrder.Order.Full_Price); } } else { await instaApi.MessagingProcessor.SendDirectTextAsync(null, chat.ThreadId, "Wrong data or data format!"); } } } } } } }